Introduction

For this assignment, you are going to be programming a chat server. You will be using the sockets programming interface to build your client and server. Sockets are a standard way to handle communications between processes--in our case a server and multiple client processes.

Part 1 - TCP Server

For the first part of this assignment, you will be implementing a TCP Server in tcpserver.c. TCP is an internet protocol that ensures reliable transmission of data between two packets. The servers job is to wait passively for tcp clients to connect to it. In general a TCP server goes through the following steps:

  • Creation with a socket()
  • bind() the server to an addrses
  • listen() for client connections
  • accept() client connections
  • terminate()

Your Task:

For the tcpserver, you must implement the following features:

  • Be able to accept any number of client connections--however, only one client will be able to interact with the server at a given time.
    • i.e. There is only a 1:1 connection.
  • When client that connects to the server should be sent the message "You have reached the server"
  • When a client joins the server, the server prompt should print the socket descriptor that has joined.
    • e.g. "Client has joined: 5" (i.e. you are printing the socket descriptor out)
  • The server should print out all messages it receives from each of the clients.
  • The server will execute each message that it receives from a client.
    • i.e. When a client receives a message, it will fork() a new process and run the 'system' to try to run that command on the server.
    • (Note: In a security class, you may learn why this is unsafe).

Part 2 - TCP Client

The second part of this assignment is implementing a TCP Client in tcpclient.c.

In general a TCP client goes through the following steps:

  • Creation with socket()
  • Connect to the server with connect()
  • Communicate with the server with send() and recv()
  • Finally close the connection with close()

A helpful analogy may be to think of using a client like a telephone. We first have to have a phone(step 1), then dial a number (step 2), communicate (step 3), and then hang up the phone (step 4).

Your Task:

For the tcpclient, you must implement the following features:

  • A client should have a 'username'
  • A client may send as many messages to the server as it likes once it has connected.
    • The client should have a prompt client> to enter each message.
  • Messages that are sent from a client, should send data in the exact size of the buffer needed.
    • The maximum size message sent from the client can be 128 characters.
    • Good: "Hello" should send over 5 bytes of data.
    • Bad: "Hello" should not send over 100 bytes of data in some fixed size buffer--this is wasteful!
  • A client socket should leave the server when the user types 'exit' in the prompt.
    • When a client leaves the server, the server should still persist waiting on other clients.

Example output

In this example below, the following happens:

  • ./tcpserver is executed on the top pane
  • The server waits for a first client to join.
  • In the second pane, ./tcpclient executes, a user name is prompted, then the user can run commands on the server.
  • The user 'Mike' promptly exits after running two commands. (You can see 'ls' output on the server, which executed on the server as a forked process).
  • While 'Mike' was executing, in window pane 3, 'Joe' had also tried to login. Joe is blocked by the server, but is able to connect as soon as 'Mike' exits.
  • Joe then runs a series of two commands ('ls' again being run on the server), and then Joe exits.
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.