lu.se

Datavetenskap

Lunds Tekniska Högskola

Denna sida på svenska This page in English

UDP and Multicast Servers

Assignment #5: UDP and Multicast Servers

Objectives

The objectives of the exercises are to:

  • Understand the structure of UDP socket communications
  • Write a UDP server and client
  • Write a multicast server and client.

Summary of the Work

Each group will have to:

  • Write UDP client and server programs that will provide the current date and time to a client using the UDP protocol;
  • Implement a multicast server and client. In its basic form, the client needs to know the server address before it sends a request. In this exercise, you will use multicast sockets so that the client discovers the active servers of a local network instead of using an address given in advance.
  • Comment briefly on the results.

In the text below, the pointers to the Java classes and methods refer to the Java version: 1.8.0. You are free to use earlier versions.

Reading Assignments

Read the following chapters from the book Java Network Programming by Elliotte Rusty Harold:

  • Chapter 12 on UDP programming, and
  • Chapter 13 on multicast.

Programming

Printing the Date and the Time

  1. Read the Date and DateFormat classes from the Java API documentation.
  2. Write a Java program that displays the date and the time either in German, French, Spanish, or another language. See the Locale class. Proceed in two steps: Create a date object from the Date class and format it using the DateFormat class.

Designing a Service and a Protocol

In this exercise, you will design a simple service and you will implement a nonnetworked version of it.

A service generally consists of actions a server can carry out or information it can provide. A server understands commands: a set of predetermined requests together with their parameters. The core of a server is a loop that processes commands:

        while (true) {
          receive(client, command, parameters);
          switch (command) {
          case commandA:
            result = doCommandA(parameters);
            break;
          case commandB:
            result = doCommandB(parameters);
            break;
          case commandC:
            result = doCommandC(parameters);
            break;
          default:
          }
          send(client, result);
        }
        
  1. Imagine a nonconnected protocol that returns either the date or the time over the network. Determine a set of necessary requests i.e. command words.
  2. Before you write the networked server, you will write a standalone program that will output either the date or the time. Proceed in two steps:
    • Write a Java program (TimeServer1.java) that returns either the date or the time from a request passed as a command line argument. Use the Date and DateFormat classes from the Java API.
    • Instead of passing the request as an argument, write a second program with no argument (TimeServer2.java) where the main loop reads inputs (receives requests) and writes outputs (sends a response). Use the int read() method of the System.in object to read the input.

Datagram Communications Using UDP: The Server Side

In this exercise, you will implement a nonconnected server using the UDP protocol (TimeServerUDP.java). The server will provide either the time or the date and you will reuse the programs from the previous exercise. Your server will create a network stream – a socket – and a packet to handle incoming and outgoing data. It will receive the request and send a response.

  1. Use the DatagramSocket class of the java.net package to create a nonconnected service and bind it to a port of your computer. You will pass the port number as an argument. By default, set it to 30000.
  2. Create a datagram packet using the DatagramPacket class to receive the data from the client.
  3. Write a loop that that reads the content of the packet
    • Use the receive() method of the DatagramSocket class to receive messages from the client in the DatagramPacket object.
    • Read the message data, the message length, the client address, and the client port from the DatagramPacket using the getData(), getLength(), getAddress(), getPort() methods and display them on the screen.
    • Create a new DatagramPacket with parameters describing the destination address and port, and data. The data will depend on the request and consist of the time, the date, or an error message.
    • Use the send() method of the DatagramSocket class to send the response to the client.
  4. Run your server and check that the port is bound using netstat.
  5. Try to launch a second server. Observe the result and explain it.

Datagram Communications Using UDP: The Client Side

In this exercise, you will implement the nonconnected client using the UDP protocol (SendUDP.java). The synopsis of the client command is:
java SendUDP machine port command.
This program sends the command string to a server identified by machine and port. The machine address will be either a DNS name or dotted numbers.

  1. Write the code that parses the command line.
  2. Create two datagram packets to send and receive respectively the data using the DatagramPacket class.
  3. When you create the send packet, fill the arguments with the command and the server address and port.
  4. Create a DatagramSocket and send this packet using the send() method.
  5. Receive the data from the server in the receive packet using the receive() method.
  6. Extract the data using the getData() method and print them to the user.

Multicast Communications and Service Discovery

In this exercise, the client will initially ignore the address of the time server. This means that instead of starting the client with the command:
java SendUDP machine port command
you will write
java SendUDP2 port command
Your new client will discover a server using multicast communications. To make the experiment more realistic, you will start multiple servers that will be running simultaneously and be able to provide a same time service.

Outline

The outline of this exercise is:

  1. You will write the time servers so that they identify the name of the machine they are running on.
  2. To discover the active time servers in the local network, you will modify your client and include a piece of code that sends a multicast query to all the machines of the local network.
  3. The active servers will respond to this client with something like: I am offering a time service and my name is: .
  4. The client will select one of the servers to which it will send its time request.

Server discovery

Programming

  1. Download the programs MCReader.java and MCSender.java. Compile them and run them. Run two instances of MCReader and observe the results.
  2. Modify MCReader.java so that it finds the name of the machine it is running on and returns it to the client (call this new program MCServerOffer.java):
    • Use the InetAddress class to determine the local address. Use the methods getLocalHost() and getHostName() or getHostAddress() to print it.
    • Create a DatagramSocket and DatagramPacket to send back the machine name to the client.
  3. Extend your time server with MCServerOffer using a threaded design.
  4. Modify your time client so that it:
    • first sends a broadcast the network to find the time servers,
    • then receives offers from the available servers,
    • and finally sends a command to one of the servers.
    You will call this new client SendUDP2.java. Start from SendUDP.java and add an appropriate method (discoverServer()) to receive the name of the first server that makes an offer. Your client will send its command to this server. You can reuse code from MCSender.java.
  5. Launch two or more multicast time servers. Each server program consists of two threads: the time server itself and the multicast name server.
  6. Run your client. Then, stop your servers and run the client again. Observe the results.

Further Reading

As a complement, read the description of the following protocols:

  • DHCP, notably the protocol sequence: discovery, offers, requests, and acknowledgement
  • ARP and run the arp -a command (/usr/sbin/arp).