Author(s) Maxime Piraux
Deadline No deadline
Submission limit No limitation
Category tags socket


Sign in

Sockets - Creating a listening socket

In this exercise, you have to create a IPv6 socket, set it to listening mode and read an incoming message. Then you have to perform some computation using the received message and send back the result to the message sender.

To create the socket, you have to use the socket() call and provide it with the correct parameters. Make sure to configure it for the IPv6 domain and set its type to support datagrams. Datagrams are simple messages abstractions.

Setting the socket to listen for incoming messages is achieved using the bind() call.

The recvfrom() call allows waiting for the receipt of a message, receiving this message and the associated sender address. A 1024-byte buffer should be sufficient to hold a message. Use a struct sockaddr_storage to receive the message sender address. For this exercice, recvfrom()'s flags argument will not be used.

The message you will receive is a sequence of numbers that are network-ordered integers. You have to sum all the numbers and send the result back to the message sender in network order. Use sendto() to send back the result.

For each of the calls that you use, if one of them were to encounter an error, immediately return -1. All C required headers are already included.

Manpages of interest:

Creating a listening socket

Copy the function signature and fill in its body

int recv_and_handle_message(const struct sockaddr *src_addr, socklen_t addrlen) {
    // TODO: Create a IPv6 socket supporting datagrams
    // TODO: Bind it to the source
    // TODO: Receive a message through the socket
    // TODO: Perform the computation
    // TODO: Send back the result
    return 0;