Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I need help modifying this code with the following instructions: here are the codes: TCPEchoserver4.c #include #include #include #include #include #include #include #include Practical.h

Hello, I need help modifying this code with the following instructions:

image text in transcribed

here are the codes:

TCPEchoserver4.c

#include

#include

#include

#include

#include

#include

#include

#include "Practical.h"

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))

DieWithSystemMessage("socket() failed");

// 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))

DieWithSystemMessage("bind() failed");

// Mark the socket so it will listen for incoming connections

if (listen(servSock, MAXPENDING)

DieWithSystemMessage("listen() failed");

else

printf("Listening %s/%d ", inet_ntoa(servAddr.sin_addr), ntohs(servAddr.sin_port));

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

DieWithSystemMessage("accept() failed");

// 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

// Construct the server address structure struct sockaddr_in servAddr; // Server address memset(&servAddr, 0, sizeof(servAddr)); // Zero out structure servAddr.sin_family = AF_INET; // IPv4 address family // Convert address int rtnVal = inet_pton(AF_INET, servIP, &servAddr.sin_addr.s_addr); if (rtnVal == 0) DieWithUserMessage("inet_pton() failed", "invalid address string"); else if (rtnVal

// Establish the connection to the echo server if (connect(sock, (struct sockaddr *) &servAddr, sizeof(servAddr))

size_t echoStringLen = strlen(echoString); // Determine input length

// Send the string to the server ssize_t numBytes = send(sock, echoString, echoStringLen, 0); if (numBytes

// Receive the same string back from the server unsigned int totalBytesRcvd = 0; // Count of total bytes received fputs("Received: ", stdout); // Setup to print the echoed string while (totalBytesRcvd

fputc(' ', stdout); // Print a final linefeed

close(sock); exit(0); }

Thank you in advance!

In this assignment, you will modify the client and server programs from Lab 4. Server Code Modify TCPEchoServer4.c as follows: Make the server display its name or address and its listening socket identifier Make the server display the phrases received from the client. It should display all phrases from all clients that use the port number that you run your server on. This server will run on coca (134.53.141.68) Continue to run the server on coca, with the single command line argument of the port. Client Code Modify TCPEchoClient4.c as follows: Modify the client so that it sends a message of more than one word to the server, Prompt the user for the messages. You will need to store the message in a String or char array and then send it. Be careful to maintain the format of the data that is sent to the server. Write the client so that it will run with the command line arguments of host address and port number, but not the message, since you are getting the message interactively from the user In this assignment, you will modify the client and server programs from Lab 4. Server Code Modify TCPEchoServer4.c as follows: Make the server display its name or address and its listening socket identifier Make the server display the phrases received from the client. It should display all phrases from all clients that use the port number that you run your server on. This server will run on coca (134.53.141.68) Continue to run the server on coca, with the single command line argument of the port. Client Code Modify TCPEchoClient4.c as follows: Modify the client so that it sends a message of more than one word to the server, Prompt the user for the messages. You will need to store the message in a String or char array and then send it. Be careful to maintain the format of the data that is sent to the server. Write the client so that it will run with the command line arguments of host address and port number, but not the message, since you are getting the message interactively from the user

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke

4th Edition

0136086535, 9780136086536

More Books

Students also viewed these Databases questions