This lab session covers usage of the C standard library to write client/server binaries interacting via sockets. It is split into three sections:
fork
.select
API.
After the server is refactored to use select
, it will be possible to change the message handling
logic to make a chatting app.
The lab isn't graded but the exam may contain questions about sockets.
Sources are available in this archive. You can use sh build.sh
to run the following commands:
mkdir -p out gcc --std=c11 src/client.c -O3 -o out/client gcc --std=c11 src/server.c -O3 -o out/server gcc --std=c11 src/server_select.c -O3 -o out/server_select
If you need to use the debugger, you'll want to disable optimizations & link debug information (using flags that you should know by now).
If the compiler is too old to support the C11 standard, just remove the option. It is meant as an upper bound anyway.
man 1 $ENTRY
, man 2 $ENTRY
, etc.AF_*
and PF_*
prefixes are found in docs, both work, choose one.
The Files listing refer to files you should be working with in the section,
not necessarily files to edit. Instructions here are roughly equivalent to TODO
s in the files.
Both programs are shut down using CTRL+C
, there are no proper exits.
To test your implementation, you'll need to open two terminals or a split view, similar to what you did in the Rust lab session. You have to run the server first since clients immediately exit on connection failure.
A good time to commit your code would be when you successfully complete each section. You can then come back to a correct code by checking out to the correct commit.
Files: client.c
, server.c
This is a simple setup where the server echo back the message to the connected client. The server
code is already complete, you must complete the code in client.c
:
Test your implementation. The server should echo back your messages like so:
Enter message > Hi > [You]: Hi > [Server]: Hi > Echo... > [You]: Echo... > [Server]: Echo... > Another message > [You]: Another message > [Server]: Another message > ^C
You can also add a print statement on the server side to help you identify issues.
Note: the duplicated line on the client-side will make sense in the last section.
fork
-based setup
Files: client.c
, server.c
If you try to connect multiple clients to your single-threaded server, you will notice issues.
The goal of this section is to use fork
to handle multiple client connections in parallel.
Modify the server.c
file so that:
join
ing).
Test your implementation. You should be able to connect multiple clients and obtain a similar
output to the one specified in section 1 for each client. If you did not modify the call to
listen
, check the man page of the function and make the change you think is appropriate.
select
-based setup
Files: client.c
, server_select.c
Creating an entire process to handle a single client, that will send a message every now and then
is extremely wasteful. Instead of using fork
, we can use multiplexing via the select
function.
Complete the server_select.c
file:
select
,select
to detect activity of the descriptors,Test your implementation. It should work exactly like it did in the previous section.
Notes:
TODO
s and variables are setup for two
sets, a main one and a working copy. If you use a single one, you need to rebuild the entire set
at each iteration of the main loop. This is a tradeoff between operation done & memory used.select
does not introduce any parallelism to the code, large scale (real) services have
to in order to function properly. select
and parallelism primitives (e.g. threads) are different
tools for different needs.
Files: client.c
, server_select.c
To turn the multiplexed echo machine into a proper chatting application, you'll have to modify
client.c
and server_select.c
:
There are no TODO
s for this section. Changes required from the last section should be minimal.
You can test your implementation like in the previous section, or using different machines. To connect to other machines, you'll need to choose a machine to run the server on, and modify the target IP of the client to match the server machine. Server and clients should be connected to the same local network to simplify the setup.