Programming Assignment on Socket Programming

Back to CS 348M home page


Goal of the assignment

In this assignment, we will build a simple client-server system, where you use the client to chat with a dummy server. The protocol between the client and server is as follows.

You are provided with the client (source code). You will write the server code to communicate with the client. You are encouraged to solve the assignment in C.


The client

Here is a copy of the client source code. Below is a sample run of the client. For this example, the client code is first compiled. Then a server is run on port 5000 in another terminal. The client program is then given the server IP (127.0.0.1 in this case, which is a special IP address that always points to the local machine) and port (5000) as commandline inputs. When the user enters messages like Hello or Hi, the server replies with OK. When the user says Bye, the server says Goodbye. The client program will exit after user enters "Bye" and server replies "Goodbye".
$ gcc client.c -o client
$ ./client 127.0.0.1 5000
Connected to server
Please enter the message to the server: Hello
Server replied: OK 
Please enter the message to the server: Hi
Server replied: OK 
Please enter the message to the server: Bye
Server replied: Goodbye 
$

In another run of the client, we show what happens when the server sends a wrong reply, say, "NO" instead of "OK". Here, the client exits with error message. This behavior should not happen with your server code.

$ ./client 127.0.0.1 5000
Connected to server
Please enter the message to the server: Hello
Server replied: NO
ERROR wrong reply from server

The server

Now, you will write a simple server in a file called "server.c". The server program should take the port number from the commandline, and start a listening socket on that commandline. Whenever a client request arrives on that socket, the server should accept the connection, and store the client socket. The server must then read from the socket. When the client has sent a message, the server should reply "OK" or "Goodbye" based on what the client has sent. After replying to one message, the server should then wait to read the next message from the client.

Note that your simple server will not handle multiple client concurrently. That is, when the server is engaged with a client, another client that tries to chat with the same server will see an error message. You may verify this during your implementation and testing.

Below is sample output from our version of server (corresponding to the client output shown above). Your server should produce similar output (the exact words printed in the statements may differ). Note that our server is printing out the client socket file descriptor number for debugging.

$ gcc server.c -o server
$ ./server 5000
Connected with client socket number 4
Client socket 4 sent message: Hello
Replied to client 4
Client socket 4 sent message: Hi
Replied to client 4
Client socket 4 sent message: Bye
Replied to client 4
Client said bye; finishing


Good luck!

Back to CS 348M home page