Index


1 Lab on network programming


1.1 Introduction

This lab session covers usage of the C standard library to write client/server binaries interacting via sockets. It is split into three sections:

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.


1.2 Compilation

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.


1.3 Resources


1.4 Tasks

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 TODOs 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.

1.4.1 Single-threaded setup

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.

1.4.2 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:

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.

1.4.3 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:

Test your implementation. It should work exactly like it did in the previous section.

Notes:

1.4.4 Make a chat room

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 TODOs 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.