Want more features on Pastebin? Sign Up, it's FREE!
Guest

Client

By: a guest on Oct 10th, 2012  |  syntax: None  |  size: 2.66 KB  |  views: 118  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /* A simple client using TCP. Server name and port number are passed as an argument */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <sys/uio.h>
  10. #include <netinet/in.h>
  11. #include <netdb.h>
  12. #include <errno.h>
  13.  
  14. void error(const char *msg)
  15. {
  16.     perror(msg);
  17.     exit(0);
  18. }
  19.  
  20. int main(int argc, char *argv[])
  21. {
  22.     int sockfd, portno, n;
  23.     struct sockaddr_in serv_addr; /*server adress*/
  24.     struct hostent *server; /*server name*/
  25.         struct iovec iov[1]; /*points to the segments of the (noncontiguous)outgoing message.*/
  26.         struct msghdr mh; /*contains parameter information for sendmsg.*/
  27.        
  28.         char *string = "random string";
  29.         char primo[256];
  30.         strcpy(primo, string);
  31.        
  32.         /* Specify the components of the message in an "iovec".*/
  33.         iov[0].iov_base = (caddr_t)primo;
  34.         iov[0].iov_len = sizeof(primo);
  35.  
  36.     char buffer[256];
  37.     if (argc < 3) {
  38.        fprintf(stderr,"usage %s hostname port\n", argv[0]);
  39.        exit(0);
  40.     }
  41.     portno = atoi(argv[2]); /*port number*/
  42.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  43.     if (sockfd < 0)
  44.         error("ERROR opening socket");
  45.     server = gethostbyname(argv[1]);
  46.     if (server == NULL) {
  47.         fprintf(stderr,"ERROR, no such host\n");
  48.         exit(0);
  49.     }
  50.     bzero((char *) &serv_addr, sizeof(serv_addr));
  51.     serv_addr.sin_family = AF_INET;
  52.     bcopy((char *)server->h_addr,
  53.          (char *)&serv_addr.sin_addr.s_addr,
  54.          server->h_length);
  55.     serv_addr.sin_port = htons(portno);
  56.    
  57.     /* The message header contains parameters for sendmsg.    */
  58.         mh.msg_name = (caddr_t) &server->h_addr;
  59.         mh.msg_namelen = sizeof(server->h_addr);
  60.         mh.msg_iov = iov;
  61.         mh.msg_iovlen = sizeof(iov);
  62.        
  63.         printf("mh structure initialized \n");
  64.    
  65.         if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) {
  66.                 error("ERROR connecting");
  67.         }
  68.         else printf("Connect() successful \n");
  69.    
  70.         n = sendmsg(sockfd, &mh, 0);            /* no flags used*/
  71.         if (n == -1) {
  72.                 perror("sendmsg failed ");
  73.                 strerror(errno);
  74.                 return -1;
  75.                 /* The  output queue for a network interface was full.  This gener‐
  76.                  * ally indicates that the interface has stopped sending,  but  may
  77.                  * be  caused  by  transient  congestion.  (Normally, this does not
  78.                  * occur in Linux.  Packets are just silently dropped when a device
  79.                  * queue overflows.) */
  80.         }
  81.         else printf("Sendmsg successfully executed \n");
  82.        
  83.     bzero(buffer,256); /*azzera il buffer*/
  84.     n = read(sockfd,buffer,255);
  85.     if (n < 0)
  86.          error("ERROR reading from socket");
  87.     printf("%s\n",buffer);
  88.     close(sockfd);
  89.     return 0;
  90. }
clone this paste RAW Paste Data