CS代考 COMP30023: Computer Systems

School of Computing and Information Systems
COMP30023: Computer Systems

Practical Week 10

Copyright By PowCoder代写 加微信 powcoder

1 I/O Multiplexing
In a previous lab, you have learned about the basics of Socket Programming in C. You have used a very simple
server and client to exchange a message back and forth. You have added more functionalities to both the
client and the server. However, our communications always involved only a single server and a single client. In
reality, this is not the case, and a server needs to handle incoming requests from multiple clients. For example,
when a DNS Server is running, multiple clients will send requests to the server.

You have used the function accept() in the server side to handle the initial incoming connection request from
the client. After that, you have used the function read() to fetch the client message from the socket and
responded accordingly. Note that, both of these functions are blocking calls (the server has to wait until the
function returns). Therefore, it is not possible to handle multiple connections through these functions. You
might think adding multiple accept() and read() calls will be enough, but what if the server needs to read()
something when it is waiting for an accept()? Hence, to handle multiple connections, we want to be notified
if one or more I/O conditions are ready (i.e., input is ready to be read, or the descriptor is capable of taking
more output). This capability is called I/O multiplexing and is provided by the select() and poll() functions in
C. I/O multiplexing is typically used in networking applications in the following scenarios 1.

• When a client is handling multiple descriptors (normally interactive input and a network socket);

• When a client wants to handle multiple sockets at the same time;

• If a TCP server handles both a listening socket and its connected sockets;

• If a server handles both TCP and UDP protocols;

• If a server handles multiple services and perhaps multiple protocols;

In this lab, we will learn how to use the select() function in C to support I/O multiplexing. Some other necessary
functions that need to be used can be studied from the example source codes. Please also search for the man
pages to learn about any specific function.

1.1 Multi-person Chat Server
Now, we will run a multi-person chat server which can be used to broadcast a message sent by any client to all
the connected clients with the server. Study the source code of select-server.c . A further reading on how
I/O multiplexing works and the explanation of the given source code can be found here2.

1. Compile and run select-server.c on your VM.
Command: $ gcc select-server.c -o server
$ ./server

2. Create another new tab/window in your SSH client program (such as MobaXterm, or open another
terminal in Ubuntu/OSX) and connect via SSH to your VM. Now, you have two ssh sessions running in
your VM where the first one is running the server program. Use the new session to run telnet.
Command: $ telnet localhost 9034

3. Create more ssh sessions and run telnet from these sessions to connect to the server. Now, whenever you
type something in one of your telnet sessions, it should appear in all the other sessions.

1https://notes.shichao.io/unp/ch6/
2https://beej.us/guide/bgnet/html/#slightly-advanced-techniques

https://notes.shichao.io/unp/ch6/
https://beej.us/guide/bgnet/html/#slightly-advanced-techniques

1.2 Coding Tasks
Add the following features in the multi-person chat server.

1. Make both the IP and the port of the server configurable which should be passed as command-line
arguments. Note that, by default, the server will run on localhost and the port of the server is hard-coded
to be 9034. As you are now configuring both your IP and port, you can pass your VM IP when running
the server code. Therefore, you will be able you to connect to your server even from your local machine.
Note that, you have to choose any port from 8000 to 10000, which are the only open ports in your VM.
Command: $ telnet VM-IP Server-Port . Please replace VM-IP and Server-Port with real
values before running.

* Have some fun by using one of your lab-mate’s server as the only host. Now, multiple people can connect
to the same server to have a group chat!

2. Instead of sending the message from one telnet session to all the others, send the message back to the
sender only. Therefore, each client should only get the message they typed as a response from the server.

2 Summation Protocol
In this exercise, we will be working with a simple made-up request/response protocol.
Completing this exercise may help you with your project.
The protocol works over TCP, with the packet format being as follows:

0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
+–+–+–+–+–+–+–+–+–+–+–+–+–+–+–+–+
| Number of numbers (prefix) |
+–+–+–+–+–+–+–+–+–+–+–+–+–+–+–+–+
| Number 1 |
+–+–+–+–+–+–+–+–+–+–+–+–+–+–+–+–+
| Number 2 (optional) |
+–+–+–+–+–+–+–+–+–+–+–+–+–+–+–+–+

To describe the above diagram in written form:
Packets have a 4-byte prefix header, which indicate the number of 4-byte numbers which follow the header.
Numbers are presented in big endian (network byte order).

Both requests and responses follow the same packet format.
A request contains one or more numbers.
Servers should send a response containing the prefix and 1 number (the sum of numbers in the request).
For simplicity, we classify integer overflow to be undefined behaviour and don’t specify what happens when a
packet is malformed.

Note: these tasks build upon previous exercises (last week and this week).

1. Read both the server and client code.
Once you understand how the code works, use the client to send a request to the server.
If you repeatedly encounter the “Haven’t received…” error, move on to the next step.

2. Uncomment the sleep statement in the client code, and observe the behaviour of the server.
Fix this behaviour by modifying the server code (Hint: TCP is stream oriented).

3. Modify the server to handle multiple client requests.

4. (Recommended if doing blocking option in project 2): Now increase the duration of the sleep statement
in the client code. Assume that this delay is necessary and cannot be removed.
Modify the server code to use pthreads/select()/epoll() to handle multiple requests simultaneously.
Print the number of requests received so far to stdout (bearing in mind race conditions).

I/O Multiplexing
Multi-person Chat Server
Coding Tasks

Summation Protocol

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com