Advertisement
Guest User

Untitled

a guest
May 25th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <netdb.h>
  7. #include <sys/types.h>
  8. #include <netinet/in.h>
  9. #include <sys/socket.h>
  10.  
  11. #define PORT 5000
  12.  
  13. #define MAXDATASIZE 100
  14.  
  15. int main(int argc, char *argv[])
  16. {
  17. int sockfd, numbytes;
  18. char buf[MAXDATASIZE];
  19. struct hostent *he;
  20. struct sockaddr_in their_addr;
  21.  
  22. if (argc != 2) {
  23. fprintf(stderr,"utilizare: client host\n");
  24. exit(1);
  25. }
  26.  
  27. if ((he=gethostbyname(argv[1])) == NULL) {
  28. perror("gethostbyname");
  29. exit(1);
  30. }
  31.  
  32. if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  33. perror("socket");
  34. exit(1);
  35. }
  36.  
  37. their_addr.sin_family = AF_INET;
  38. their_addr.sin_port = htons(PORT);
  39. their_addr.sin_addr = *((struct in_addr *)he->h_addr);
  40. memset(&(their_addr.sin_zero), '\0', 8);
  41.  
  42. if (connect(sockfd, (struct sockaddr *)&their_addr,
  43. sizeof(struct sockaddr)) == -1) {
  44. perror("connect");
  45. exit(1);
  46. }
  47.  
  48. if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
  49. perror("recv");
  50. exit(1);
  51. }
  52.  
  53. buf[numbytes] = '\0';
  54.  
  55. printf("Primit: %s",buf);
  56.  
  57. close(sockfd);
  58.  
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement