lu.se

Datavetenskap

Lunds Tekniska Högskola

Denna sida på svenska This page in English

Networking Tools and UDP Servers

Assignment #1: Networking Tools and UDP Servers

Objectives

The objectives of the exercises are to:

  • Use and understand basic networking tools
  • Understand the structure of socket communications
  • Understand the structure of a client-server application
  • 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;
  • Comment briefly on the results.

In the text below, the pointers to the Java classes and methods refer to the Java version available on the student computers: 1.6.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 6, pp. 150-173, on Internet addresses,
  • Chapter 13, pp. 423-457, on UDP programming, and
  • Chapter 14, pp. 470-492 on multicast.

Using Networking Tools

Looking up Machines Using nslookup

The Internet protocol IPv4 identifies machines using a 4-byte address. Machines can also have names organized using hierarchical domains. A collection of distributed databases (Domain Name System, DNS) ensures the name-to-address mapping. The nslookup command is an interactive tool to find an address from a name and the reverse.

  1. Run nslookup and find the address of www.lth.se.
  2. Find the name corresponding to the IP address 130.235.16.34

Looking up Routes Using traceroute

The Internet interconnects thousands of networks: The Lund campus network to the Swedish university computer network SUNET, SUNET to Telia, etc. To find their way in the network, data use routers: devices that link a network to another one and that orient the data.

  1. Have a look at the SUNET map and predict the routers between Lund and Umeå. The network topology has been upgraded in March 2007. You may have a look at older ones here.
  2. Run traceroute with the www.umu.se and then www.hgo.se parameter.
  3. Run traceroute and find the routers between your machines and www.stanford.edu and www.tu-berlin.de.
  4. You may also have a look at the NORDUnet, Géant, and Abilene maps (Internet2 network operations center).

Looking up Ports Using netstat

The netstat command shows information on the network status. It comes with a dozen options. Here we examine the interface, routing, and all socket options.

  1. The interface corresponds to the network hardware attachment, Ethernet most of the time on the campus. A connected machine has at least two device entries. The first one is the loop-back interface that prevents self-addressed data to go out and the second one is a real piece of equipment. Run netstat -i. What is its name on your machine?
  2. A routing table contains the final destinations together with their first intermediate gateway. As for the loop-back interface, a machine designates itself using a specific name: localhost. Run netstat -r. Use the options n and a. Where do all your packets go when they are not destined for the local network? What is the localhost address?
  3. Most network communications involve a remote and a distant party. Both parties have IP addresses. Machines usually provide many services: telnet, ftp, chat, etc. A distinct port number identifies each TCP/UDP service. Hence, a communication consists of four numbers <remote_addr, remote_port, local_addr, local_port> . Run netstat -a. What are the transport protocols on your machine? List some services, their IP address, and their TCP/UDP ports.
  4. Finite-state machines are convenient devices to model data transmission. They consist of a set of states linked by arcs. When the protocol is in a state, an event can trigger a transition to another state and the machine puts a symbol out. A TCP event corresponds to the reception of a packet and an output corresponds to a transmission. Have a look at a TCP state machine at http://www.texample.net/tikz/examples/tcp-state-machine/ and identify the server and client path. Run netstat -a again and identify the state of some connections.

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. 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.
  3. 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. The program 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. It will discover it 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. You will modify your client and include a piece of code that will send a multicast query to all the active time servers of the local network.
  3. All the servers will respond to this client something like: I am active and this is my name.
  4. The client will select one of the servers to which it will send its query.

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 MCAddressServer.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. Modify your time client so that it first queries the network on the available servers and then sends a command to one of the servers. You will call this new client, SendUDP2.java. Use the code in MCSender.java and add a call to receive the name of the first server that answers. Send your command to this server.
  4. Launch two or more multicast time servers. Each server consists of two programs, the multicast name server and the time server.
  5. 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).