Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <netdb.h>
  4. #include <arpa/inet.h>
  5. #include <sys/socket.h>
  6. #include <sys/types.h>
  7. #include <string.h>
  8. #include <arpa/inet.h>
  9. #include <stdlib.h>
  10.  
  11. #define MAX_CLIENT 5
  12. #define MAX_BUFFER 512
  13.  
  14. int main(void)
  15. {
  16.     struct addrinfo hints;
  17.     struct addrinfo* result;
  18.  
  19.     memset(&hints, 0, sizeof hints);
  20.  
  21.     hints.ai_family = AF_UNSPEC;
  22.     hints.ai_socktype = SOCK_STREAM;
  23.     hints.ai_flags = AI_PASSIVE;
  24.  
  25.     getaddrinfo(NULL, "1111", &hints, &result);
  26.  
  27.     int listener_socket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
  28.     int yes = 1;
  29.     if (setsockopt(listener_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == 1) {
  30.         perror("setsockopt");
  31.         exit(1);
  32.     }
  33.     bind(listener_socket, result->ai_addr, result->ai_addrlen);
  34.  
  35.     listen(listener_socket, MAX_CLIENT);
  36.  
  37.     fd_set master;
  38.     fd_set readFds;
  39.     FD_ZERO(&master);
  40.     FD_SET(listener_socket, &master);
  41.     int fdmax = listener_socket;
  42.     int received_bytes = 0;
  43.     struct addrinfo new_client_address;
  44.     socklen_t new_client_address_length = sizeof(new_client_address);
  45.     char address[MAX_BUFFER];
  46.     char buffer[MAX_BUFFER];
  47.  
  48.     struct addrinfo client_hints;
  49.     memset(&client_hints, 0, sizeof client_hints);
  50.  
  51.     client_hints.ai_family = AF_UNSPEC;
  52.     client_hints.ai_socktype = SOCK_STREAM;
  53.  
  54.     struct addrinfo *client_res;
  55.  
  56.     int i,j;
  57.     int new_client;
  58.     while (1) {
  59.         memset(&buffer, 0, MAX_BUFFER);
  60.         readFds = master;
  61.  
  62.         select(fdmax+1, &readFds, NULL, NULL, NULL);
  63.  
  64.         for (i = 0; i <= fdmax; i++) {
  65.             if (FD_ISSET(i, &readFds)) {
  66.                 if (i == listener_socket) {
  67.  
  68.                     new_client = accept(listener_socket, &new_client_address, &new_client_address_length);
  69.  
  70.                     FD_SET(new_client, &master);
  71.                     if (new_client > fdmax) {
  72.                         fdmax = new_client;
  73.                     }
  74.  
  75.                     int dsa = getaddrinfo(new_client_address.ai_addr, NULL, &client_hints, &client_res);
  76.                     printf("asd: %d\n", dsa);
  77.                     if (new_client_address.ai_family == AF_INET) {
  78.                         struct sockaddr_in* ipv4 = (struct sockaddr_in*)new_client_address.ai_addr;
  79.                         inet_ntop(AF_INET,&(ipv4->sin_addr), &address, sizeof address );
  80.                     } else {
  81.                         struct sockaddr_in6* ipv6 = (struct sockaddr_in6*) new_client_address.ai_addr;
  82.                         inet_ntop(AF_INET6,&(ipv6->sin6_addr), &address, sizeof address );
  83.                     }
  84.                     puts(address);
  85.                 }
  86.             }
  87.         }
  88.     }
  89.  
  90.  
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement