SHARE
TWEET

udpclient

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