SHARE
TWEET

server.c

maxlagerz Feb 23rd, 2019 (edited) 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. ** listener.c -- a datagram sockets "server" demo
  3. ** taken from beej
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <sys/types.h>
  12. #include <sys/socket.h>
  13. #include <netinet/in.h>
  14. #include <arpa/inet.h>
  15. #include <netdb.h>
  16. #include <pthread.h>
  17. #include "common.h"
  18.  
  19. #define MYPORT "4950"   // the port users will be connecting to
  20. #define MAXBUFLEN 256
  21. #define MAX_THREADS 10
  22.  
  23. struct sockaddr_in server_addr, client_addr;
  24. thread_no = 0;
  25.  
  26. // get sockaddr, IPv4 or IPv6:
  27. void *get_in_addr(struct sockaddr *sa)
  28. {
  29.     if (sa->sa_family == AF_INET) {
  30.         return &(((struct sockaddr_in*)sa)->sin_addr);
  31.     }
  32.  
  33.     return &(((struct sockaddr_in6*)sa)->sin6_addr);
  34. }
  35.  
  36.  
  37. void* handle_request(void *buf){
  38.     char s[INET6_ADDRSTRLEN];
  39.  
  40.     test_struct_t *client_data = (test_struct_t *) buf;
  41.  
  42.     result_struct_t result;
  43.     int k = 0;
  44.     for (int i = 0; i < strlen(client_data->name); i++) {
  45.         result.c[i] = client_data->name[i];
  46.         k++;
  47.     }
  48.     result.c[k] = ' ';
  49.     k += 1;
  50.     for (int i = 0; i < strlen(client_data->age); i++) {
  51.         result.c[k] = client_data->age[i];
  52.         k++;
  53.     }
  54.     result.c[k] = ' ';
  55.     k += 1;
  56.     for (int i = 0; i < strlen(client_data->group); i++) {
  57.         result.c[k] = client_data->group[i];
  58.         k++;
  59.     }
  60.  
  61.     printf("listener: got packet from %s\n",
  62.            inet_ntop(client_addr.sin_family,
  63.                      get_in_addr((struct sockaddr *)&client_addr),
  64.                      s, sizeof s));
  65.    
  66.     printf("Thread id: %d", thread_no);
  67.    
  68.     sleep(10);
  69. }
  70.  
  71.  
  72. int main(void)
  73. {
  74.     int sockfd, sent_recv_bytes = 0;
  75.     struct addrinfo hints, *servinfo, *p;
  76.     int rv;
  77.     int numbytes;
  78.     struct sockaddr_storage their_addr;
  79.     char buf[MAXBUFLEN];
  80.     socklen_t addr_len;
  81.     char s[INET6_ADDRSTRLEN];
  82.  
  83.  
  84.     // Создаем структуру для указания семейства, типа сокета и режима автозаполнения IP адресса
  85.     memset(&hints, 0, sizeof hints);
  86.     hints.ai_family = AF_INET; // AF_INET to force IPv4
  87.     hints.ai_socktype = SOCK_DGRAM;
  88.     hints.ai_flags = AI_PASSIVE; // auto set my IP
  89.  
  90.     if ((rv = getaddrinfo(NULL, MYPORT, &hints, &servinfo)) != 0) {
  91.         fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
  92.         return 1;
  93.     }
  94.  
  95.     // loop through all the results and bind to the first we can
  96.     for(p = servinfo; p != NULL; p = p->ai_next) {
  97.         if ((sockfd = socket(p->ai_family, p->ai_socktype,
  98.                              p->ai_protocol)) == -1) {
  99.             perror("listener: socket");
  100.             continue;
  101.         }
  102.  
  103.         if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
  104.             close(sockfd);
  105.             perror("listener: bind");
  106.             continue;
  107.         }
  108.  
  109.         break;
  110.     }
  111.  
  112.     if (p == NULL) {
  113.         fprintf(stderr, "listener: failed to bind socket\n");
  114.         return 2;
  115.     }
  116.  
  117.     freeaddrinfo(servinfo);
  118.  
  119.     printf("listener: waiting to recvfrom...\n");
  120.  
  121.     addr_len = sizeof client_addr;
  122.  
  123.     pthread_t threads[MAX_THREADS];
  124.     int rc = 0;
  125.  
  126.     while (1) {
  127.         /*Step 6 : Wait for client connection*/
  128.         /*state Machine state 1 */
  129.  
  130.         if ((numbytes = recvfrom(sockfd, buf, MAXBUFLEN-1 , 0,
  131.                                  (struct sockaddr *)&client_addr, &addr_len)) == -1) {
  132.             perror("recvfrom");
  133.             exit(1);
  134.         }
  135.  
  136.         printf("listener: packet is %d bytes long\n", numbytes);
  137.  
  138.         rc = pthread_create(&threads[thread_no], NULL, handle_request, (void*)buf);
  139.         if(rc){
  140.             printf("A request could not be processed\n");
  141.         }
  142.         else{
  143.             thread_no++;
  144.         }
  145.  
  146.  
  147.     }
  148.     return 0;
  149. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top