Recent Changes - Search:

Network Programming

This website demonstrates using wikis as teaching and learning tool.

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

Tutorial - Daytime Client and Server by UDP

Overview: The Internet daytime service was specified in an RFC standard called the Daytime Protocol (RFC 867). According to this standard, a daytime service can be implemented as a datagram-based application. A server has to listen on UDP port 13. When a datagram is received, a reply is sent containing the current date and time in ASCII format. The daytime protocol is simple and easy to implement using Java. This tutorial provides you with a sample source for a daytime server and another for a daytime client.


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

Compile, run, and test the Quote client and Quote server according to the following tutorial.

Activity 3

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.
Send your Java source code to Steven.

Activity 4

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. List the three methods to set properties of a DatagramSocket object.
5. List the five methods to return properties of a DatagramSocket object.
6. What happens if you create a DatagramSocket without specifying a port number?
7. Is it possible to use a single DatagramSocket for both sending and receiving purposes?
8. 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?
9. 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?
10. In the process of receiving UDP packets, which step involves blocking I/O functions?
11. What is the function of the method setSoTimeout(int timeout) (of DatagramSocket object)?
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);
    }
  }
}

Edit - History - Print - Recent Changes - Search
Page last modified on November 10, 2011, at 06:07 PM