* The C language must be used * UDP sockets must be used * The client must initiate the “handshake” with a multicast message * Port numbers may be hard-coded, as may a multicast address * Aside from that, no address information may be hard-coded or input from the user * The server should listen for multicast messages on the proper address * The server should then initiate a unicast connection with the client * The client should accept this connection * The client will send 1 string to the server and then exit * The server will receive the string, print the message along with the IP address of the client
You are to demonstrate discovery of a server using multicasting.
Both the client and server will agree on a port number (above 10000), which may be hard-coded into the programs.
The client will need to discover the address of the server, which might change. The client is to send a message to a multicast address, and the server should respond point-to-point.
What is Multicast Discovery?
A client sends a message to an address which does not represent a machine, but rather a service. The server listens to this address (in addition to its own address) and is able to learn that a new client is interested in communicating, and in particular it learns the IP address of this client. The server responds directly to this IP address, and the client is therefore able to learn the address of the server.
The benefit of this scheme is that hard‐coded machine addresses are not required. The multicast address can be hardcoded, but this address will work on any network. The server may be using one IP address on one day, and another machine may take over the next. Despite this, the client does not need to be modified, since all servers listen to the same multicast address.
How do you send a multicast message?
To send a multicast message you will address the message to an IP address in the range reserved for multicasting. The choice of address is up to you. Beware that it is possible (but unlikely) for another program to be using this address, so you should be prepared to change the address in this case.
You will not be able to use a connection‐based session for multicast discovery, since multicasting requires UDP (SOCK_DGRAM) and is therefore connectionless.
One caveat: if you are using a single machine for testing the client and the server, you will need to allow your traffic to be “looped back” to the host. Otherwise the outbound messages will be delivered to every other machine in the group, but not the sending machine. Loopback is enabled by default on some distributions, but if you need to explicitly turn it on you will need to write:
char enable = 1; setsockopt (socket, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof (loop);
How do you receive a multicast message?
Receiving multicast traffic is a little more work (perhaps 4 lines). The lecture slides provided code which will join a multicast group, so you should familiarize yourself with those examples. Specifically, you will want to call setsockop() and add the multicast address to the list of addresses the socket will listen to.
After the multicast group is joined, you may perform a recvfrom() and wait for clients to connect. Note that this blocks the thread until a datagram arrives.
Note that you will provide recvfrom() with a socket address and length (uninitialized). These values will be filled in when a datagram arrives, so you will know where the packet came from. This means that future communication with the client can be directly to its address, as opposed to continuing to use multicasting.
How do you unicast a response?
Unicasting is a just a fancy word to indicate that a packet is sent from one machine to another (point to point) as opposed to multicasting or broadcasting (one machine to many).
After a multicast message is received (only new clients should send these), the server will need to send a UDP response back to the client in order for the client to learn that server’s IP address. In a nutshell you will call sendto() and provide the address of the client obtained from the recvfrom().
What does the Client do when it receives a response?
Apart from the multicast discovery which was completed when the server responded, all traffic should be TCP/IP. This means a connected socket will need to be created between the server and the client. Once the client learns the server’s IP address, it can open socket and connect to the server’s address as we have previously done.
When the server learns of a new connection attempt and accepts the new connection, the server will gain a new connected socket. From that point on, both the client and the server may communicate directly using this socket pair.