Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.50 KB | None | 0 0
  1. int sockfd, numbytes;  
  2.     // char buf[MAXDATASIZE];
  3.     struct hostent *he;
  4.     struct sockaddr_in their_addr; /* connector's address information */
  5.  
  6.     printf("getting host\n");
  7.     if ((he=gethostbyname("147.175.180.56")) == NULL) {  /* get the host info */
  8.         herror("gethostbyname");
  9.         exit(1);
  10.     }
  11.  
  12.     printf("creating socket\n");
  13.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  14.         perror("socket");
  15.         exit(1);
  16.     }
  17.  
  18.     their_addr.sin_family = AF_INET;      /* host byte order */
  19.     their_addr.sin_port = htons(PORT);    /* short, network byte order */
  20.     their_addr.sin_addr = *((struct in_addr *)he->h_addr);
  21.     bzero(&(their_addr.sin_zero), 8);     /* zero the rest of the struct */
  22.  
  23.     printf("connecting\n");
  24.  
  25.     if (connect(sockfd, (struct sockaddr *)&their_addr, \
  26.                                             sizeof(struct sockaddr)) == -1) {
  27.         printf("connect fail");
  28.         exit(1);
  29.     }
  30.  
  31.     char *buf = (char*)"This is turing";
  32.  
  33.     printf("sending\n");
  34.     // while (1)
  35.     // {
  36.     if (send(sockfd, buf, (int)strlen(buf), 0) == -1)
  37.     {
  38.         perror("send");
  39.         exit (1);
  40.     }
  41.         // printf("After the send function \n");
  42.  
  43.         //  if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {
  44.         //          perror("recv");
  45.         //          exit(1);
  46.         // }   
  47.  
  48.     //     buf[numbytes] = '\0';
  49.  
  50.     //     printf("Received in pid=%d, text=: %s \n",getpid(), buf);
  51.     //  sleep(1);
  52.     // }
  53.  
  54.     close(sockfd);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement