Advertisement
xerpi

server linux

Jan 6th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <strings.h>
  4. #include <errno.h>
  5. #include <ifaddrs.h>
  6. #include <netdb.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <netinet/in.h>
  10. #include <arpa/inet.h>
  11.  
  12. #define PORT 3550
  13. #define BACKLOG 2 /* Max allowed connections */
  14.  
  15. int main(int argc, char *argv[])
  16. {
  17.    /* Get local address */
  18.    struct ifaddrs *ifaddr, *ifa;
  19.    char host[512] = {0};
  20.    
  21.    if (getifaddrs(&ifaddr) == -1) {
  22.          printf("error getting local IP address: %i\n", strerror(errno));
  23.    } else {
  24.        for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
  25.             getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
  26.            
  27.             if((strcmp(ifa->ifa_name,"lo")!=0) && (ifa->ifa_addr->sa_family == AF_INET) ) {
  28.                printf(" Interface : <%s>\n", ifa->ifa_name );
  29.                printf("   Address : <%s>\n", host);
  30.             }      
  31.       }
  32.       freeifaddrs(ifaddr);
  33.    }
  34.    
  35.    
  36.    int fd, fd2; /* file descriptors */
  37.  
  38.    struct sockaddr_in server;
  39.    /* server address info */
  40.  
  41.    struct sockaddr_in client;
  42.    /* client address info */
  43.  
  44.    int sin_size;
  45.  
  46.    if ((fd=socket(AF_INET, SOCK_STREAM, 0)) == -1 ) {  
  47.       printf("socket() error\n");
  48.       exit(-1);
  49.    }
  50.  
  51.    server.sin_family = AF_INET;        
  52.  
  53.    server.sin_port = htons(PORT);
  54.    
  55.    server.sin_addr.s_addr = INADDR_ANY;
  56.    /* connect to any IP*/
  57.  
  58.    bzero(&(server.sin_zero),8);
  59.  
  60.  
  61.    if (bind(fd,(struct sockaddr*)&server,
  62.            sizeof(struct sockaddr))==-1) {
  63.       printf("bind() error\n");
  64.       exit(-1);
  65.    }
  66.    
  67.  
  68.    if (listen(fd,BACKLOG) == -1) {
  69.       printf("listen() error\n");
  70.       exit(-1);
  71.    }
  72.  
  73.    while(1) {
  74.       sin_size = sizeof(struct sockaddr_in);
  75.      
  76.       printf ("\nWaiting for a 3DS...\n");
  77.      
  78.       if ((fd2 = accept(fd,(struct sockaddr *)&client,
  79.                         &sin_size))==-1) {
  80.          printf("error en accept()\n");
  81.          exit(-1);
  82.       }
  83.  
  84.       printf("We got connection from: %s\n",
  85.              inet_ntoa(client.sin_addr) );
  86.       /* show client IP */
  87.  
  88.       send(fd2,"Welcome to my server.\n",22,0);
  89.       /* send a message to the client */
  90.  
  91.       close(fd2); /* close fd2 */
  92.    }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement