Computer Science using C++

 

To get started, please write two programs which run on the CSE machines (ie cse01, cse02, etc).  One will be a server, one will be a client.

This probably sounds very complicated, but your client and server are not going to do much more than send a string over the network and print it out.  It is about as basic of an assignment as we can have.

The purpose is to familiarize you with the Unix network API.  You have seen the steps needed to perform these tasks, now you can carry them out.

The server:

The server needs to create a TCP/IP socket.   This means a connected Internet socket.  Look at the socket(2) system call and study the parameters.  If you still don’t see how to create the socket, please ask.  I just want everyone to try on their own first.

Servers need to bind addresses.  What address will the server respond to?  You have the option of having the system fill in your local IP address.  You need to select a port to operate on.  A port is a way of indicating the application-level protocol.  Please pick a random number between 10K and 64K and use that for both your client and server.  Both programs need to agree on this port number.  If you get an error about “address already in use” then pick another port number.

After binding, you need to setup a connection queue to hold incoming clients.  It is possible for multiple clients to arrive at the same time, and you don’t want to miss them.  While your server should be quick about connecting to these clients, in practice one creates a small queue.  The listen(2) system call is used to create that queue.

The server should wait in a loop for new connections.  Servers usually have infinite loops for this, with some other way of exiting.  Terminating the program with ^C is an option.  Inside of the loop, the server should call accept(2).  Note that this will put the server to sleep until a client arrives.  When a client tries to connect to the server, the server will be woke and the accept() call completes.  Accept provides the server with the address of the new client.

The server should process any data sent from the client.  Real servers can have extended conversations with a client.  You just need to read one string.  You may assume that the string will fit in a 1500 byte packet.

Print that string out, along with the IP address of the client (in dotted decimal notation).  See inet_ntoa(3).

When the conversation is done, the server should use close(2) or shutdown(2) to end the conversation.  Accept will open a new connection for the next client.  You do not want to leave sockets open when the client disconnects, or you could fill up your open file table.

The client:

This is much simpler.  The client needs to create a socket, just like the server.  The client needs to create a struct sockaddr structure with the server’s address.  You may hard-code this address.  Later we will consider how you could discover that at runtime.

The client should connect(2) to the server.  Once connect returns successfully, you may write to the socket.  Be sure to close the socket when you are done.

The client only needs to send one string per run, and it may then exit.  The server sticks around until you shut it down.