Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a C program to implement a TCP protocol between a client and a server. Your program should have 2 files: client.c and server.c YOUR

Write a C program to implement a TCP protocol between a client and a server. Your program should have 2 files: client.c and server.c

YOUR PROGRAM MUST COMPILE AND RUN ON LINUX MACHINES.

Your program needs to accept user input for two things, first the port number and second the users ID.

Your program should have the following folders:

Server directory: this folder contains the server code file (server.c) and a text file named data.txt. This text file can contain any texts.

Client directory: this folder contains the client code file (client.c)

Your program should be run by typing the following commands (where port_x is the command line argument for the port number):

gcc server.c -o server

./server port_x

gcc client.c -o client

./client port_x

When client.c is run, it should ask the user to enter their 8-digit ID.

Here is a high level description of the expected functionality:

When the client connects to the server, the client application should prompt the user to enter their 8-digit ID (example: 12345359). Afterwards, this ID is sent to the server. In response, the server sends 1 the current datetime of the system (i.e., Fri Feb 3 10:59:33 2022). The datetime can be in any format but it is important that both the server and client agree on the same format. Then, the server generates a PASSCODE which works like an authorization code. The PASSCODE is generated by extracting the seconds part of the time (i.e., 33) plus the last four digits of ID (i.e., 5359). So, the PASSCODE in this example is 5392 (5359+33). Please note that the PASSCODE is not sent to the client directly and the client is responsible to also create it in the same way. Client will then send this PASSCODE to the server as an authorization code and if both PASSCODEs (the PASSCODE server generates and the PASSCODE server receives from client) are the same, server will open and read a text file (from its own run directory) and send the content to the client. Client should receive the content and save it in a text file in its own directory. Below are the pseudocodes for both server and client behaviors.

Here is a step-by-step instruction for the protocol:

Server Behavior

1: while TRUE do

2: Listen for incoming requests (from clients using TCP) over a specific port

3: Accept a new connection

4: Receive users ID from the client

5: Send current day and time to the connected client

6: Create a PASSCODE.

//PASSCODE = seconds part of that date time + last four digits of ID

7: Read an integer from the client

//It should be the same passcode but constructed and sent by the client

8: Open the file \data.txt" from the run directory and send its contents to client

9: Close the client socket

Client Behavior

1: Create a TCP connection with the server

2: Get users ID as an input in the client and send it to the server

3: Read a string which is the current time sent by the server

4: Create the PASSCODE

//PASSCODE = seconds part of the current time + last four digits of ID

5: Send the PASSCODE to the server

6: Read the content of the text file sent by the server and save what it reads from the socket into a text file in the run directory

7: Close the socket and file stream

The following are expected output of the program:

First, the received ID should be printed on the server terminal.

Second, the received time from the server should be printed on the client terminal.

Third, the client should print the PASSCODE that it sends to the server.

Fourth, each time the client receives the text content from the server, it should Print: readBytes + Received from the Server! where readBytes is the number of bytes read from the server in each iteration in the loop.

Finally, it should save the text file in its own directory (Client folder). So, you should be able to open the text file from the clients directory and see the exact content from the original text file from the servers directory.

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

Students also viewed these Databases questions

Question

Use lattice multiplication to determine the product. 4 x 327

Answered: 1 week ago