Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <netdb.h>
  6. #include <string.h>
  7.  
  8. #define PORT 0x1234
  9. /* REPLACE with your server machine name*/
  10. #define HOST "Unix-machine"
  11. #define DIRSIZE 8192
  12.  
  13. main(argc, argv)
  14. int argc; char **argv;
  15. {
  16. char hostname[100];
  17. char dir[DIRSIZE];
  18. int sd;
  19. struct sockaddr_in sin;
  20. struct sockaddr_in pin;
  21. struct hostent *hp;
  22.  
  23. strcpy(hostname,HOST);
  24. if (argc>2)
  25. { strcpy(hostname,argv[2]); }
  26.  
  27. /* go find out about the desired host machine */
  28. if ((hp = gethostbyname(hostname)) == 0) {
  29. perror("gethostbyname");
  30. return 1 ;
  31. }
  32.  
  33. /* fill in the socket structure with host information */
  34. memset(&pin, 0, sizeof(pin));
  35. pin.sin_family = AF_INET;
  36. pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
  37. pin.sin_port = htons(PORT);
  38.  
  39. /* Client-A */
  40. if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  41. perror("socket");
  42. return 1;
  43. }//if socket sd is equal to -1, print out an error for "socket"
  44.  
  45. /* Client-B */
  46. if (connect(sd,(struct sockaddr *) &pin, sizeof(pin)) == -1) {
  47. perror("connect");
  48. return 1;
  49. }//if connect is -1, print error for "connect"
  50.  
  51. /* Client-C */
  52. if (send(sd, argv[1], strlen(argv[1]), 0) == -1) {
  53. perror("send");
  54. return 1;
  55. }//if send = -1 print error "send"
  56.  
  57. /* Client-D */
  58. if (recv(sd, dir, DIRSIZE, 0) == -1) {
  59. perror("recv");
  60. return 1;
  61. }//if recv = -1 print error message "recv"
  62.  
  63. /* Client-E */
  64. printf("%s\n", dir);
  65. //print name of dir
  66.  
  67. close(sd);
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement