Overview

In this programming assignment, we will learn to use the TCP/IP API to create and bind sockets that will pass network traffic between two machines.

Instructions

You will utilize TCP to connect a client with a server and send a text message of arbitrary size. Upon completion of the transaction, the server will send back a text acknowledgement and then both machines will terminate the connection. The server will then return to listening for additional connections.

Requirements

1. You will implement a simple command-line oriented messaging client and server (no GUIs, no dialog boxes, no graphics). The server will be invoked (from the command line) thus:

server < port-number >

Where < port-number > is any numeric port number that the user provides at run time via the command line. The server attempts to bind a TCP socket to this port and listen for connections (if the server cannot bind to this port, the server will exit after printing an informative error message for the user). The client will be invoked as follows:

client < server-address > < server-port-number >

Here, < server-address > is the IP address of the server and is the port number for the TCP connection on the server. The IP address and port number are provided at run time via the command line.

2. The client will read from standard input and transmit all characters, up to the < EOF > indicator (in Linux, this would be control-d, in windows, it is usually control-z).

3. When the server receives the text message, it will display the message to standard output (including the IP address of the machine that sent the message), and send an acknowledgement back to the client in the form of a text message: "Message received."

4. The client and server will then drop the TCP connection and the server will continue listening for new connections.

5. You must implement your assignment such that it meets the following requirements:

  • Implemented in the C programming language
  • Compiles with the GCC or Clang C compiler
  • Is POSIX compliant code, and uses the sys/socket (and perhaps the sys/types) libraries

6. Your client/server must be able to handle a message of arbitrary size, from small to rather large (somewhere around 100KB).

7. Make sure you read from standard input and display the message result to standard output. There is no need for any file I/O in this assignment.

8. If your server can not bind to the port given on the command line, it should display an appropriate message to standard error and then gracefully cleanup/exit

9. If the client is unable to bind/connect with the server, it should also display an appropriate message to standard error and then gracefully cleanup/exit.

10. In the example below, the server is running on a machine with IP address 192.168.1.2 and the client is on a machine with IP address 192.168.1.4. You will provide your own message (test with messages of various length), IP address, and port number. You will not type the five characters "< EOF >" but type the single < EOF > character.

Example input/output for the client is shown below. Output is in blue and input is in red (the format of your input/output must comply with the example below):

client 192.168.1.2 62637
Some interesting and exciting message.< EOF >
Message received.

Example input/output for the server is shown below. Output is in blue and input is in red (the format of your input/output must comply with the example below):

server 62637
Message from 192.168.1.4:
Some interesting and exciting message.

Extra Credit

You can score some extra credit by adding the following functionality to your program:

1. After the user enters the message into the client, the client computes a SHA-256 hash. This hash value is displayed to the user on the client machine.

2. When the server receives the message from the client, it computes a hash value of the message content. The hash value is displayed on the server console (standard out). The server-computed hash value is returned to the client along with the acknowledgement message, "Message received".

3. When the client receives the acknowledgement from the server, the client displays the server acknowledgement and the server-computed hash value. If the hash values disagree, then the client also displays the message "Message integrity check failed".

Successful Example: Example input/output for the client with successful communication is shown below. Output is in blue and input is in red (the format of your input/output must comply with the example below):

client 192.168.1.2 62637
Some interesting and exciting message.< EOF >
fc9120c68584e9714212cfa890574c655b5b659768b370d71ee63580f71a761a
Message received.
Server hash: fc9120c68584e9714212cfa890574c655b5b659768b370d71ee63580f71a761a

Example input/output for the server with successful communication is shown below.

server 62637
Message from 192.168.1.4:
Some interesting and exciting message.
fc9120c68584e9714212cfa890574c655b5b659768b370d71ee63580f71a761a

Example with Errors: Example input/output for the client with a communication error is shown below. Output is in blue and input is in red (the format of your input/output must comply with the example below):

client 192.168.1.2 62637
Some interesting and exciting message.< EOF >
fc9120c68584e9714212cfa890574c655b5b659768b370d71ee63580f71a761a
Message received.
Server hash: 07e44d96fa1430a8e1fe38850a4ab46518490da7ae94080479cbae78901ad4e1
Message integrity check failed.

Example input/output for the server with a communication error is shown below.

server 62637
Message from 192.168.1.4:
Some interesting and exciting message.
07e44d96fa1430a8e1fe38850a4ab46518490da7ae94080479cbae78901ad4e1
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.