Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | None | 0 0
  1. /*
  2.  * udpclient.c - A simple UDP client
  3.  * usage: udpclient <host> <port>
  4.  */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <netinet/in.h>
  12. #include <netdb.h>
  13. #include <arpa/inet.h>
  14. #include <sys/ioctl.h>
  15. #include <net/if.h>
  16. #include <errno.h>
  17.  
  18. #define BUFSIZE 1024
  19.  
  20. /*
  21.  * error - wrapper for perror
  22.  */
  23. void error(char *msg)
  24. {
  25.     perror(msg);
  26.     exit(0);
  27. }
  28.  
  29. int main(int argc, char **argv)
  30. {
  31.     int sockfd, portno, n;
  32.     int serverlen;
  33.     struct sockaddr_in serveraddr;
  34.     struct sockaddr_in bindaddr;
  35.  
  36.     char *bindaddr_str;
  37.  
  38.     struct hostent *server;
  39.     char *hostname;
  40.  
  41.  
  42.     char buf[BUFSIZE];
  43.  
  44.     /* check command line arguments */
  45.     if (argc != 4) {
  46.         fprintf(stderr,"usage: %s <hostname> <port> <bind iface>\n", argv[0]);
  47.         exit(0);
  48.     }
  49.     hostname = argv[1];
  50.     portno = atoi(argv[2]);
  51.     bindaddr_str = argv[3];
  52.  
  53.     /* socket: create the socket */
  54.     sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  55.     if (sockfd < 0)
  56.         error("ERROR opening socket");
  57.  
  58.  
  59.     struct ifreq ifr;
  60.  
  61.     snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), bindaddr_str);
  62.  
  63. /*  if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)) < 0) {
  64.         fprintf(stderr, "ERROR: can't bind to device %s,\n%s",
  65.             bindaddr_str, strerror(errno));
  66.         exit(1);
  67.     }*/
  68.    
  69.  
  70.     /* gethostbyname: get the server's DNS entry */
  71.     server = gethostbyname(hostname);
  72.     if (server == NULL) {
  73.         fprintf(stderr,"ERROR, no such host as %s\n", hostname);
  74.         exit(0);
  75.     }
  76.  
  77.     /* build the server's Internet address */
  78.     bzero((char *) &serveraddr, sizeof(serveraddr));
  79.     serveraddr.sin_family = AF_INET;
  80.     bcopy((char *)server->h_addr,
  81.             (char *)&serveraddr.sin_addr.s_addr, server->h_length);
  82.     serveraddr.sin_port = htons(portno);
  83.  
  84.     /*memset((void *) &bindaddr, 0, sizeof(bindaddr));
  85.     bindaddr.sin_family = AF_INET;
  86.     inet_pton(AF_INET, bindaddr_str, &(bindaddr.sin_addr));
  87.  
  88.     if (bind(sockfd, (struct sockaddr *) &bindaddr, sizeof(bindaddr)) < 0) {
  89.         perror("Error in binding");
  90.         exit(1);
  91.     }*/
  92.  
  93.    
  94.  
  95.     while (1) {
  96.         /* get a message from the user */
  97.         bzero(buf, BUFSIZE);
  98.         printf("Please enter msg: ");
  99.         fgets(buf, BUFSIZE, stdin);
  100.         /* send the message to the server */
  101.         serverlen = sizeof(serveraddr);
  102.         n = sendto(sockfd, buf, strlen(buf), 0,
  103.                (struct sockaddr *)&serveraddr, serverlen);
  104.         printf("sent %s", buf);
  105.         if (n < 0)
  106.             error("ERROR in sendto");
  107.  
  108.         /* print the server's reply */
  109.         n = recvfrom(sockfd, buf, strlen(buf), 0,
  110.                  (struct sockaddr *)&serveraddr, &serverlen);
  111.         if (n < 0)
  112.             error("ERROR in recvfrom");
  113.         printf("Echo from server: %s", buf);
  114.     }
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement