Advertisement
tftrgi11

tcpserver

Jun 20th, 2020
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.41 KB | None | 0 0
  1. #include <netinet/in.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <getopt.h>
  6.  
  7. #include <sys/socket.h>
  8. #include <sys/types.h>
  9. #include <unistd.h>
  10.  
  11. #define SADDR struct sockaddr
  12.  
  13. int main(int argc, char *argv[]) {
  14.   const size_t kSize = sizeof(struct sockaddr_in);
  15.  
  16.   int lfd, cfd;
  17.   int nread;
  18.   int port = 0;
  19.   int buffsize = 0;
  20.   struct sockaddr_in servaddr;
  21.   struct sockaddr_in cliaddr;
  22.  
  23. while (1) {
  24.     int current_optind = optind ? optind : 1;
  25.  
  26.     static struct option options[] = {{"port", required_argument, 0, 0},
  27.                                       {"buffsize", required_argument, 0, 0},
  28.                                       {0, 0, 0, 0}};
  29.  
  30.     int option_index = 0;
  31.     int c = getopt_long(argc, argv, "", options, &option_index);
  32.  
  33.     if (c == -1)
  34.       break;
  35.  
  36.     switch (c) {
  37.     case 0: {
  38.       switch (option_index) {
  39.       case 0:
  40.         port = atoi(optarg);
  41.         if (port < 1024 || port > 49151)
  42.         {
  43.             printf("Invalid port\n");
  44.             return 1;
  45.         }
  46.         break;
  47.       case 1:
  48.         buffsize = atoi(optarg);
  49.         if (buffsize < 1)
  50.         {
  51.             printf("Buffsize must be positive\n");
  52.             return 1;
  53.         }
  54.         break;
  55.       default:
  56.         printf("Index %d is out of options\n", option_index);
  57.       }
  58.     } break;
  59.  
  60.     case '?':
  61.       printf("Arguments error\n");
  62.       break;
  63.     default:
  64.       fprintf(stderr, "getopt returned character code 0%o?\n", c);
  65.     }
  66.   }
  67.  
  68.   if (argc != 3) {
  69.     printf("usage: client <IPaddress of server>\n");
  70.     exit(1);
  71.   }
  72.  
  73.   if ((lfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  74.     perror("socket");
  75.     exit(1);
  76.   }
  77.   char buf[buffsize];
  78.   memset(&servaddr, 0, kSize);
  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(lfd, (SADDR *)&servaddr, kSize) < 0) {
  84.     perror("bind");
  85.     exit(1);
  86.   }
  87.  
  88.   if (listen(lfd, 5) < 0) {
  89.     perror("listen");
  90.     exit(1);
  91.   }
  92.  
  93.   while (1) {
  94.     unsigned int clilen = kSize;
  95.  
  96.     if ((cfd = accept(lfd, (SADDR *)&cliaddr, &clilen)) < 0) {
  97.       perror("accept");
  98.       exit(1);
  99.     }
  100.     printf("connection established\n");
  101.  
  102.     while ((nread = read(cfd, buf, buffsize)) > 0) {
  103.       write(1, &buf, nread);
  104.     }
  105.  
  106.     if (nread == -1) {
  107.       perror("read");
  108.       exit(1);
  109.     }
  110.     close(cfd);
  111.   }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement