Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Sample code The sample code is an example of a networked TCP server and associated client. The server opens a passive socket and listens for

Sample code

The sample code is an example of a networked TCP server and associated client. The server opens a passive socket and listens for connection requests from clients. This is a very simple server: it accepts at most one connection request. Once a client connects, the server sends the message of the system time to the client and shuts down.

SAMPLE SERVER:

#include  #include  #include  #include  #include  #include  #include  #include  #include  #include  int main(int argc, char *argv[]) { int listenfd = 0, connfd = 0; struct sockaddr_in serv_addr; char sendBuff[1025]; time_t ticks; listenfd = socket(AF_INET, SOCK_STREAM, 0); memset(&serv_addr, '0', sizeof(serv_addr)); memset(sendBuff, '0', sizeof(sendBuff)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(20000); bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); listen(listenfd, 10); while(1) { connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); ticks = time(NULL); snprintf(sendBuff, sizeof(sendBuff), "%.24s ", ctime(&ticks)); write(connfd, sendBuff, strlen(sendBuff)); close(connfd); sleep(1); } }

SAMPLE CLIENT:

#include  #include  #include  #include  #include  #include  #include  #include  #include  #include  int main(int argc, char *argv[]) { int sockfd = 0, n = 0; char recvBuff[1024]; struct sockaddr_in serv_addr; if(argc != 2) { printf(" Usage: %s  ",argv[0]); return 1; } memset(recvBuff, '0',sizeof(recvBuff)); if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf(" Error : Could not create socket "); return 1; } memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(20000); if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0) { printf(" inet_pton error occured "); return 1; } if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { printf(" Error : Connect Failed "); return 1; } while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0) { recvBuff[n] = 0; if(fputs(recvBuff, stdout) == EOF) { printf(" Error : Fputs error "); } } if(n < 0) { printf(" Read error "); } return 0; }

Lab Assignment

Modify the server and client above so that the system meets the following requirements:

The server can accept multiple clients and assign each of them a unique ID. When connected, the server sends back this ID to the client.

The server can accept several different commands:

list : The server sends back all the active client IDs.

msg ID string : The server should be able to understand that this client wants to send the msg(string) to the other client with the ID that listed the command. The server should be able to forward the message to the target as the following format source ID: message_content

history ID : The server should send back the chatting history between the requested client and the client with ID listed in the command.

exit : The The server should send back a message "Goodbye" and close the connection.

The client should be able to communicate with both the server and other active clients.

Grading Rubric

Your code should be elegant and well-documented (with comments).

1. The submission can not compile successfully. (0 points) 2. The submission works for 1-1 connection. (10%) 3. The submission can accept multiple connections from clients. (20%) 4. The commands "list" and "exit" work. (10%)

5. The command "msg ID string" works. (25%) 6. The command "history" works. (25%) 7. The submitted code is in a clear logic and easy to read. (10%)

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

Step: 3

blur-text-image

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 M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago