tftrgi11

udpserver

Jun 20th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.53 KB | None | 0 0
  1. #include <arpa/inet.h>
  2. #include <netinet/in.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/socket.h>
  7. #include <sys/stat.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10. #include <getopt.h>
  11.  
  12. #define SADDR struct sockaddr
  13. #define SLEN sizeof(struct sockaddr_in)
  14.  
  15. int main(int argc, char *argv[]) {
  16.     int sockfd, n;
  17.     int port = 0;
  18.     int  buffsize = 0;
  19.     while (1) {
  20.     int current_optind = optind ? optind : 1;
  21.  
  22.     static struct option options[] = {{"port", required_argument, 0, 0},
  23.                                       {"buffsize", required_argument, 0, 0},
  24.                                       {0, 0, 0, 0}};
  25.  
  26.     int option_index = 0;
  27.     int c = getopt_long(argc, argv, "", options, &option_index);
  28.  
  29.     if (c == -1)
  30.       break;
  31.  
  32.     switch (c) {
  33.     case 0: {
  34.       switch (option_index) {
  35.       case 0:
  36.         port = atoi(optarg);
  37.         if (port < 1024 || port > 49151)
  38.         {
  39.             printf("Invalid port\n");
  40.             return 1;
  41.         }
  42.         break;
  43.       case 1:
  44.         buffsize = atoi(optarg);
  45.         if (buffsize < 1)
  46.         {
  47.             printf("Buffsize must be positive\n");
  48.             return 1;
  49.         }
  50.         break;
  51.       default:
  52.         printf("Index %d is out of options\n", option_index);
  53.       }
  54.     } break;
  55.  
  56.     case '?':
  57.       printf("Arguments error\n");
  58.       break;
  59.     default:
  60.       fprintf(stderr, "getopt returned character code 0%o?\n", c);
  61.     }
  62.   }
  63.  
  64.   if (argc != 3) {
  65.     printf("usage: client <IPaddress of server>\n");
  66.     exit(1);
  67.   }
  68.  
  69.   if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  70.     perror("socket problem");
  71.     exit(1);
  72.   }
  73.  
  74.   char mesg[buffsize], ipadr[16];
  75.   struct sockaddr_in servaddr;
  76.   struct sockaddr_in cliaddr;
  77.  
  78.   memset(&servaddr, 0, SLEN);
  79.   servaddr.sin_family = AF_INET;
  80.   servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  81.   servaddr.sin_port = htons(port);
  82.  
  83.   if (bind(sockfd, (SADDR *)&servaddr, SLEN) < 0) {
  84.     perror("bind problem");
  85.     exit(1);
  86.   }
  87.   printf("SERVER starts...\n");
  88.  
  89.   while (1) {
  90.     unsigned int len = SLEN;
  91.  
  92.     if ((n = recvfrom(sockfd, mesg, buffsize, 0, (SADDR *)&cliaddr, &len)) < 0) {
  93.       perror("recvfrom");
  94.       exit(1);
  95.     }
  96.     mesg[n] = 0;
  97.  
  98.     printf("REQUEST %s      FROM %s : %d\n", mesg,
  99.            inet_ntop(AF_INET, (void *)&cliaddr.sin_addr.s_addr, ipadr, 16),
  100.            ntohs(cliaddr.sin_port));
  101.  
  102.     if (sendto(sockfd, mesg, n, 0, (SADDR *)&cliaddr, len) < 0) {
  103.       perror("sendto");
  104.       exit(1);
  105.     }
  106.   }
  107. }
Add Comment
Please, Sign In to add comment