Advertisement
tftrgi11

tcpclient

Jun 20th, 2020
677
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.38 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/types.h>
  8. #include <unistd.h>
  9. #include <getopt.h>
  10.  
  11.  
  12. #define SADDR struct sockaddr
  13. #define SIZE sizeof(struct sockaddr_in)
  14.  
  15. int main(int argc, char *argv[]) {
  16.   int fd;
  17.   int nread;
  18.   char ip[20];
  19.   int port = 0;
  20.   int buffsize = 0;
  21.   struct sockaddr_in servaddr;
  22.   while (1) {
  23.     int current_optind = optind ? optind : 1;
  24.  
  25.     static struct option options[] = {{"ip", required_argument, 0, 0},
  26.                                       {"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.         memcpy(ip, optarg, strlen(optarg));
  41.         break;
  42.       case 1:
  43.         port = atoi(optarg);
  44.         if (port < 1024 || port > 49151)
  45.         {
  46.             printf("Invalid port\n");
  47.             return 1;
  48.         }
  49.         break;
  50.       case 2:
  51.         buffsize = atoi(optarg);
  52.         if (buffsize < 1)
  53.         {
  54.             printf("Buffsize must be positive\n");
  55.             return 1;
  56.         }
  57.         break;
  58.       default:
  59.         printf("Index %d is out of options\n", option_index);
  60.       }
  61.     } break;
  62.  
  63.     case '?':
  64.       printf("Arguments error\n");
  65.       break;
  66.     default:
  67.       fprintf(stderr, "getopt returned character code 0%o?\n", c);
  68.     }
  69.   }
  70.  
  71.   if (argc != 4) {
  72.     printf("usage: client <IPaddress of server>\n");
  73.     exit(1);
  74.   }
  75.  
  76.  
  77.   if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  78.     perror("socket creating");
  79.     exit(1);
  80.   }
  81.  
  82.     char buf[buffsize];
  83.  
  84.   memset(&servaddr, 0, SIZE);
  85.   servaddr.sin_family = AF_INET;
  86.  
  87.   if (inet_pton(AF_INET, ip, &servaddr.sin_addr) <= 0) {
  88.     perror("bad address");
  89.     exit(1);
  90.   }
  91.  
  92.  
  93.   servaddr.sin_port = htons(port);
  94.  
  95.   if (connect(fd, (SADDR *)&servaddr, SIZE) < 0) {
  96.     perror("connect");
  97.     exit(1);
  98.   }
  99.  
  100.   write(1, "Input message to send\n", 22);
  101.   while ((nread = read(0, buf, buffsize)) > 0) {
  102.     if (write(fd, buf, nread) < 0) {
  103.       perror("write");
  104.       exit(1);
  105.     }
  106.   }
  107.  
  108.   close(fd);
  109.   exit(0);
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement