In this homework, we will learn how to write basic networking applications using sockets.
Write a socket client and socket server. The programs should be called "sclient" and "sserver".
sserver supports a flag -p with which you can specify the port on which it listens for incoming connections and prints "waiting for clients". When it receives a connection, it sends the text "hello from server" to the client, prints "client has connected" to stdout and closes the client connection. The server itself should not terminate and should wait for additional clients that might connect to it later.
./sserver -p 5000 waiting for clients client has connected
sclient supports the flags -p and -h with which you can specify the port and hostname for the server to which you want to connect. Upon connecting to the server, the client prints "connected to the server at port xxx host xxx", reads the text sent by the server, prints that text to stdout and terminates.
./sclient -p 5000 -h bayou.cs.uh.edu connected to the server at port 5000 host bayou.cs.uh.edu hello from server
The best way to test these two programs is by running them in two different terminals and looking at the output from each program to make sure they are working correctly.
Now we will extend the sserver and sclient to add some application logic. We will imagine that the sserver is on a server that can use its large computational resources to perform computations requested by the clients. We will illustrate this concept by making the server do simple summation of numbers.
Extend sserver so it supports the flag -o with which you can specify the operations your server supports. Possible operations are sum, concat, and product. You can specify multiple -o flags to enable multiple operations by your server.
./sserver -p 5000 -o sum -o concat -l json lauching server supports sum concat
Now the sserver should send "capabilities: " followed by the list of operations this server supports instead of "hello from server" when a client connects to this server. For example, in the above example, the server will send "capabilities: sum concat" to the clients when they connect to this server.
Extend sclient so it supports the flag -f with which you can specify a text file and -o with which you can specify the operation you want to request. The file has one integer per line. When sclient connects to the server, it sends the integers in the file as a string where the integers are separated by a single space character.
Your sserver and sclient should also support an optional "-l" flag with which we specify the protocol they use to communicate with each other. By default, sclient and sserver use json.
cat ./myfile.txt 3 4 5 6 ./sclient -p 5000 -h bayou.cs.uh.edu -o sum -f myfile.txt -l json connected to the server at port 5000 host bayou.cs.uh.edu capabilities: sum concat sending request for sum data: 3 4 5 6 received response: 18
If the user requests an operation that is not supported by the server, sclient should report an appropriate error:
./sclient -p 5000 -h bayou.cs.uh.edu -o product -f myfile.txt connected to the server at port 5000 host bayou.cs.uh.edu capabilities: sum concat the server does not support the requested operation: product
Many times the server operations might take a long time or the data the client is requesting may be available after a long wait. In such circumstances, we may consider an architecture that uses callbacks rather than a long connection to the server. A client connects to the server, tells the server the data it is interested in, and registers a callback, i.e., provides information about where the server should connect once the data is ready.
Write a "callback" program that accepts -p flag to specify the port on which the callback program will listen for results coming from the server.
Modify the sclient program so it accepts -cp, -ch, and -cl flags to specify the port and host on which the server can connect to the callback program once the result is ready using the protocol specified by -cl.
Modify sserver so it accepts a flag -w to specify the length of time it waits (in seconds) before sending the response to the callback program across the network.
You will have to extend your protocol so the client and the server can communicate with each other about the callback information.
Your submission should be a .tar.gz of the directory where you have your source code and a Makefile. Also include a textfile called README with a short description of how to run the programs, what limitations exist (if any) and a description of your protocol in the format used by IETF documents. You will submit your .tar.gz on Moodle.
To grade your submission, we will copy your .tar.gz to bayou, untar, change to that directory and type "make". It should generate three executables sclient, sserver, and callback.