Guest User

Untitled

a guest
Jul 20th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. #include <stdio.h> /* for printf() and fprintf() */
  2. #include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
  3. #include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
  4. #include <netdb.h> /* for getHostByName() */
  5. #include <stdlib.h> /* for atoi() and exit() */
  6. #include <string.h> /* for memset() */
  7. #include <unistd.h> /* for close() */
  8.  
  9. #define RCVBUFSIZE 32 /* Size of receive buffer */
  10.  
  11. void DieWithError(char *errorMessage); /* Error handling function */
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15. int sock; /* Socket descriptor */
  16. struct sockaddr_in echoServAddr; /* Echo server address */
  17. struct hostent *thehost; /* Hostent from gethostbyname() */
  18. unsigned short echoServPort; /* Echo server port */
  19. char *servIP; /* Server IP address (dotted quad) */
  20. char *echoString; /* String to send to echo server */
  21. char echoBuffer[RCVBUFSIZE]; /* Buffer for echo string */
  22. unsigned int echoStringLen; /* Length of string to echo */
  23. int bytesRcvd, totalBytesRcvd; /* Bytes read in single recv()
  24. and total bytes read */
  25.  
  26. if ((argc < 2) || (argc > 3)) /* Test for correct number of arguments */
  27. {
  28. fprintf(stderr, "Usage: %s <Server IP> [<Echo Port>]\n",
  29. argv[0]);
  30. exit(1);
  31. }
  32.  
  33. servIP = argv[1]; /* First arg: server IP address (dotted quad) */
  34.  
  35. if (argc == 3)
  36. echoServPort = atoi(argv[2]); /* Use given port, if any */
  37. else
  38. echoServPort = 7; /* 7 is the well-known port for the echo service */
  39.  
  40. printf("Connecting to Server: %s at Port: %d...\n", servIP, echoServPort);
  41.  
  42.  
  43. while(1) { // INFINITE WHILE
  44.  
  45. printf("%s", "Enter a string (Press CTRL+C to quit): ");
  46. scanf("%s", echoString);
  47.  
  48. /* Create a reliable, stream socket using TCP */
  49. if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
  50. DieWithError("socket() failed");
  51.  
  52. printf("Sent to TCP Server: %s\n", echoString);
  53.  
  54. /* Construct the server address structure */
  55. memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
  56. echoServAddr.sin_family = AF_INET; /* Internet address family */
  57. echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
  58. echoServAddr.sin_port = htons(echoServPort); /* Server port */
  59.  
  60. /* If user gave a dotted decimal address, we need to resolve it */
  61. if (echoServAddr.sin_addr.s_addr == -1) {
  62. thehost = gethostbyname(servIP);
  63. echoServAddr.sin_addr.s_addr = *((unsigned long *) thehost->h_addr_list[0]);
  64. }
  65.  
  66.  
  67. /* Establish the connection to the echo server */
  68. if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
  69. DieWithError("connect() failed");
  70.  
  71. echoStringLen = strlen(echoString); /* Determine input length */
  72.  
  73. /* Send the string to the server */
  74. if (send(sock, echoString, echoStringLen, 0) != echoStringLen)
  75. DieWithError("send() sent a different number of bytes than expected");
  76.  
  77. /* Receive the same string back from the server */
  78. totalBytesRcvd = 0;
  79. printf("Received from TCP Server: "); /* Setup to print the echoed string */
  80. while (totalBytesRcvd < echoStringLen) {
  81. /* Receive up to the buffer size (minus 1 to leave space for
  82. a null terminator) bytes from the sender */
  83. if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0)
  84. DieWithError("recv() failed or connection closed prematurely");
  85. totalBytesRcvd += bytesRcvd; /* Keep tally of total bytes */
  86. echoBuffer[bytesRcvd] = '\0'; /* Terminate the string! */
  87. printf(echoBuffer); /* Print the echo buffer */
  88. }
  89.  
  90. printf("\n"); /* Print a final linefeed */
  91.  
  92. close(sock);
  93.  
  94. } // End infinite while loop
  95.  
  96. exit(0);
  97. }
Add Comment
Please, Sign In to add comment