Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Client

By: a guest on Oct 10th, 2012  |  syntax: None  |  size: 2.29 KB  |  hits: 93  |  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. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <sys/uio.h>
  9. #include <netinet/in.h>
  10. #include <netdb.h>
  11. #include <errno.h>
  12.  
  13. void error(const char *msg)
  14. {
  15.     perror(msg);
  16.     exit(0);
  17. }
  18.  
  19. int main(int argc, char *argv[])
  20. {
  21.     int sockfd, portno, n;
  22.     struct sockaddr_in serv_addr; /*server adress*/
  23.     struct hostent *server; /*server name*/
  24.         struct iovec iov[1]; /*points to the segments of the (noncontiguous)outgoing message.*/
  25.         struct msghdr mh; /*contains parameter information for sendmsg.*/
  26.        
  27.         char *string = "random string";
  28.         char primo[256];
  29.         strcpy(primo, string);
  30.        
  31.         /* Specify the components of the message in an "iovec".*/
  32.         iov[0].iov_base = (caddr_t)primo;
  33.         iov[0].iov_len = sizeof(primo);
  34.  
  35.     char buffer[256];
  36.     if (argc < 3) {
  37.        fprintf(stderr,"usage %s hostname port\n", argv[0]);
  38.        exit(0);
  39.     }
  40.     portno = atoi(argv[2]); /*port number*/
  41.     sockfd = socket(AF_INET, SOCK_STREAM, 0);
  42.     if (sockfd < 0)
  43.         error("ERROR opening socket");
  44.     server = gethostbyname(argv[1]);
  45.     if (server == NULL) {
  46.         fprintf(stderr,"ERROR, no such host\n");
  47.         exit(0);
  48.     }
  49.     bzero((char *) &serv_addr, sizeof(serv_addr));
  50.     serv_addr.sin_family = AF_INET;
  51.     bcopy((char *)server->h_addr,
  52.          (char *)&serv_addr.sin_addr.s_addr,
  53.          server->h_length);
  54.     serv_addr.sin_port = htons(portno);
  55.    
  56.     /* The message header contains parameters for sendmsg.    */
  57.     memset(&mh, 0, sizeof(mh));
  58.         mh.msg_iov = iov;
  59.         mh.msg_iovlen = 1;
  60.        
  61.         printf("mh structure initialized \n");
  62.    
  63.         if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) {
  64.                 error("ERROR connecting");
  65.         }
  66.         else printf("Connect() successful \n");
  67.    
  68.         n = sendmsg(sockfd, &mh, 0);            /* no flags used*/
  69.         if (n == -1) {
  70.                 perror("sendmsg failed ");
  71.                 strerror(errno);
  72.                 return -1;
  73.         }
  74.         else printf("Sendmsg successfully executed \n");
  75.        
  76.     bzero(buffer,256);
  77.     n = read(sockfd,buffer,255);
  78.     if (n < 0)
  79.          error("ERROR reading from socket");
  80.     printf("Server says: %s\n",buffer);
  81.     close(sockfd);
  82.     return 0;
  83. }