Recent Changes - Search:

Distributed Computing

This website demonstrates using wikis as teaching and learning tool.

The course instructor is also happy to share the teaching materials here with those who find it readable.

Tutorial - Daytime Client and Server (UDP)

A Distributed Computing Tutorial by Steven Choy

Activity 1

Compile, run, and test the daytime client and server according to the following steps.
1. Compile the two class, DaytimeServer and DaytimeClient.
2. Execute the server by typing
      java DaytimeServer
3. Execute the client and connect it to the localhost
      java DaytimeClient localhost
4. Test the daytime client by connecting it to the other host in the network.

Activity 2

Write a Java program called UDPPortScan.class that looks for UDP ports in use on the local host. You can make use of the DatagramSocket constructor to create a socket that listens on a specific port. A SocketException is thrown if the socket can't be created. This means that the port number you want to use is already occupied by another application running on the same machine.
Compile and then execute the UDPPortScan.class to see what UDP port numbers are already in use in your machine.

Questions

1. List the four methods of DatagramPacket to obtain information on the UDP packet that has received.
2. List the four methods to set the DatagramPacket properties for transmission.
3. Which type of exception objects may be thrown by the various constructors of class DatagramSocket?
4. What is the function of the method setSoTimeout(int timeout)?
5. List the three methods to set properties of a DatagramSocket object.
6. List the five methods to return properties of a DatagramSocket object.
7. What happens if you create a DatagramSocket without specifying a port number?
8. Is it possible to use a single DatagramSocket for both sending and receiving purposes?
9. If you intend to create a DatagramSocket for receiving UDP packets first, is it possible to let the operating system assign an unused UDP port to you?
10. If you have no idea about the size of the packet you are going to receive, how do you set the length of the DatagramPacket?
11. In the process of receiving UDP packets, which step involves blocking I/O functions?
12. If you intend to send a number of different UDP packets to a single host by repeatedly calling the send method of the DatagramSocket, what should you do before each calling of the send method?

Java Code: Daytime Client

import java.io.*;
import java.net.*;

public class DaytimeClient {
  public static void main (String[] args) throws IOException {
    if ((args.length != 1))
      throw new IllegalArgumentException
        ("Syntax: DaytimeClient <host>[:<port>]");

    int idx = args[0].indexOf (":");
    int port = (idx > -1) ? Integer.parseInt (args[0].substring (idx + 1))
      : DaytimeServer.DEFAULT_PORT;
    String hostName = (idx > -1) ? args[0].substring (0, idx) : args[0];
    InetAddress host = InetAddress.getByName (hostName);

    DatagramSocket socket = new DatagramSocket ();
    socket.setSoTimeout (5000);
    DatagramPacket packet = new DatagramPacket (new byte[256], 1, host, port);
    socket.send (packet);
    packet.setLength (packet.getData ().length);
    socket.receive (packet);
    socket.close ();

    byte[] data = packet.getData ();
    int length = packet.getLength ();
    System.out.println (new String (data, 0, length, "latin1"));
  }
}

Java Code: Daytime Server

import java.io.*;
import java.net.*;

public class DaytimeServer {
  public static final int DEFAULT_PORT = 13;
  public static void main (String[] args) throws IOException {
    if (args.length > 1)
      throw new IllegalArgumentException ("Syntax: DaytimeServer [<port>]");
    DatagramSocket socket = new DatagramSocket
      (args.length == 0 ? DEFAULT_PORT : Integer.parseInt (args[0]));
    DatagramPacket packet = new DatagramPacket (new byte[1], 1);
    while (true) {
      socket.receive (packet);
      System.out.println
        ("Received from: " + packet.getAddress () + ":" + packet.getPort ());
      byte[] outBuffer = new java.util.Date ().toString ()
        .getBytes ("latin1");
      packet.setData (outBuffer);
      packet.setLength (outBuffer.length);
      socket.send (packet);
    }
  }
}

Suggested answers are available here.

Edit - History - Print - Recent Changes - Search
Page last modified on December 06, 2007, at 04:06 PM