Question: Exercise: You will use the TCP echo client and server code (for IPv4 version) from Donahoo. You will modify the code to do the following.
Exercise: You will use the TCP echo client and server code (for IPv4 version) from Donahoo. You will modify the code to do the following. Instead of transmitting a string (passed from the command line) from client to server, you will pass a text file as a command line argument, and transmit the contents of that file to the sender line by line, including line numbers. (Sample file is attached to Canvas). The rest of the arguments will remain the same. While sending data, client will echo it line by line to the screen, along with line numbers. Please keep in mind that your program should work with any file, not just the one that was provided, so hard-coding will not be a good idea. The server will receive the entire file, buffer it until it has the entire contents, and then reverse it and echo (print) it to the standard output (screen). Each revesed line of text will have the correct line number. The last character of the original file will be the first one in the reversed version except line numbes. After that you will print a goodbye message from both the client and the server and close the connection. It is up to you to figure out the details. There are several different solutions to this problem, and everyone's solution may be different. I do recommend that you review how to read from files in C and go over the code line by line following the Chapter 2 in Donahoo.
Starter Code:
TCPEchoServer4.c:
#include
static const int MAXPENDING = 5; // Maximum outstanding connection requests
int main(int argc, char *argv[]) {
if (argc != 2) // Test for correct number of arguments DieWithUserMessage("Parameter(s)", "
in_port_t servPort = atoi(argv[1]); // First arg: local port
// Create socket for incoming connections int servSock; // Socket descriptor for server if ((servSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))
// Construct local address structure struct sockaddr_in servAddr; // Local address memset(&servAddr, 0, sizeof(servAddr)); // Zero out structure servAddr.sin_family = AF_INET; // IPv4 address family servAddr.sin_addr.s_addr = htonl(INADDR_ANY); // Any incoming interface servAddr.sin_port = htons(servPort); // Local port
// Bind to the local address if (bind(servSock, (struct sockaddr*) &servAddr, sizeof(servAddr))
// Mark the socket so it will listen for incoming connections if (listen(servSock, MAXPENDING)
for (;;) { // Run forever struct sockaddr_in clntAddr; // Client address // Set length of client address structure (in-out parameter) socklen_t clntAddrLen = sizeof(clntAddr);
// Wait for a client to connect int clntSock = accept(servSock, (struct sockaddr *) &clntAddr, &clntAddrLen); if (clntSock
// clntSock is connected to a client!
char clntName[INET_ADDRSTRLEN]; // String to contain client address if (inet_ntop(AF_INET, &clntAddr.sin_addr.s_addr, clntName, sizeof(clntName)) != NULL) printf("Handling client %s/%d ", clntName, ntohs(clntAddr.sin_port)); else puts("Unable to get client address");
HandleTCPClient(clntSock); } // NOT REACHED }
TCPEchoClient4.c:
#include#include #include #include #include #include #include #include #include "Practical.h" int main(int argc, char *argv[]) { if (argc 4) // Test for correct number of arguments DieWithUserMessage("Parameter(s)", " [ ]"); char *servIP = argv[1]; // First arg: server IP address (dotted quad) char *echoString = argv[2]; // Second arg: string to echo // Third arg (optional): server port (numeric). 7 is well-known echo port in_port_t servPort = (argc == 4) ? atoi(argv[3]) : 7; // Create a reliable, stream socket using TCP int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (sock Here's the sample txt file for input:
Example output Client output: Sending data to
: 1. Hotel California 2. .... (the rest of the file line by line including blank lines and line numbers) .... Goodbye!!!!! Server output: Received data from
Hotel California On a dark desert highway, cool wind in my hair Warm smell of colitas, rising up through the air Up ahead in the distance, I saw a shimmering light My head grew heavy and my sight grew dim I had to stop for the night There she stood in the doorway I heard the mission bell And I was thinking to myself "This could be Heaven or this could be Hell" Then she lit up a candle and she showed me the way There were voices down the corridor I thought I heard them say Welcome to the Hotel California Such a lovely place (Such a lovely place) Such a lovely face Plenty of room at the Hotel California Any time of year (Any time of year) You can find it here Her mind is Tiffany-twisted, she got the Mercedes bends She got a lot of pretty, pretty boys she calls friends How they dance in the courtyard, sweet summer sweat Some dance to remember, some dance to forget: 34. 33. ... Goodbye!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts

