Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> /* for printf() and fprintf() */
- #include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
- #include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
- #include <netdb.h> /* for getHostByName() */
- #include <stdlib.h> /* for atoi() and exit() */
- #include <string.h> /* for memset() */
- #include <unistd.h> /* for close() */
- #define RCVBUFSIZE 32 /* Size of receive buffer */
- void DieWithError(char *errorMessage); /* Error handling function */
- int main(int argc, char *argv[])
- {
- int sock; /* Socket descriptor */
- struct sockaddr_in echoServAddr; /* Echo server address */
- struct hostent *thehost; /* Hostent from gethostbyname() */
- unsigned short echoServPort; /* Echo server port */
- char *servIP; /* Server IP address (dotted quad) */
- char *echoString; /* String to send to echo server */
- char echoBuffer[RCVBUFSIZE]; /* Buffer for echo string */
- unsigned int echoStringLen; /* Length of string to echo */
- int bytesRcvd, totalBytesRcvd; /* Bytes read in single recv()
- and total bytes read */
- if ((argc < 2) || (argc > 3)) /* Test for correct number of arguments */
- {
- fprintf(stderr, "Usage: %s <Server IP> [<Echo Port>]\n",
- argv[0]);
- exit(1);
- }
- servIP = argv[1]; /* First arg: server IP address (dotted quad) */
- if (argc == 3)
- echoServPort = atoi(argv[2]); /* Use given port, if any */
- else
- echoServPort = 7; /* 7 is the well-known port for the echo service */
- printf("Connecting to Server: %s at Port: %d...\n", servIP, echoServPort);
- while(1) { // INFINITE WHILE
- printf("%s", "Enter a string (Press CTRL+C to quit): ");
- scanf("%s", echoString);
- /* Create a reliable, stream socket using TCP */
- if ((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
- DieWithError("socket() failed");
- printf("Sent to TCP Server: %s\n", echoString);
- /* Construct the server address structure */
- memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */
- echoServAddr.sin_family = AF_INET; /* Internet address family */
- echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */
- echoServAddr.sin_port = htons(echoServPort); /* Server port */
- /* If user gave a dotted decimal address, we need to resolve it */
- if (echoServAddr.sin_addr.s_addr == -1) {
- thehost = gethostbyname(servIP);
- echoServAddr.sin_addr.s_addr = *((unsigned long *) thehost->h_addr_list[0]);
- }
- /* Establish the connection to the echo server */
- if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0)
- DieWithError("connect() failed");
- echoStringLen = strlen(echoString); /* Determine input length */
- /* Send the string to the server */
- if (send(sock, echoString, echoStringLen, 0) != echoStringLen)
- DieWithError("send() sent a different number of bytes than expected");
- /* Receive the same string back from the server */
- totalBytesRcvd = 0;
- printf("Received from TCP Server: "); /* Setup to print the echoed string */
- while (totalBytesRcvd < echoStringLen) {
- /* Receive up to the buffer size (minus 1 to leave space for
- a null terminator) bytes from the sender */
- if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0)
- DieWithError("recv() failed or connection closed prematurely");
- totalBytesRcvd += bytesRcvd; /* Keep tally of total bytes */
- echoBuffer[bytesRcvd] = '\0'; /* Terminate the string! */
- printf(echoBuffer); /* Print the echo buffer */
- }
- printf("\n"); /* Print a final linefeed */
- close(sock);
- } // End infinite while loop
- exit(0);
- }
Add Comment
Please, Sign In to add comment