4.2.6 The Exercise

In this exercise, you'll be guided through opening datagram sockets, and in constructing client and server programs. You'll need to consult the documentation of the java.net package extensively, and in particular DatagramSocket, DatagramPacket and InetAddress.

If you want to learn more, then the Java Tutorial at
http://java.sun.com/docs/books/tutorial/networking/datagrams/index.html
is a good place to start.

  1. Complete the following SimpleServer code, filling in the requested lines

    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    public class SimpleServer extends Thread {
    
        protected DatagramSocket socket = null;
    
        public SimpleServer() throws IOException {
            this("SimpleServer");
        }
    
        public SimpleServer(String name) throws IOException {
            super(name);
    
            // **********************************************
            // Add a line here to create a the DatagramSocket for the
            // socket field on a random number chosen by you and which is
            // transferable to the client program
            // between 8000 and 8001
            //***********************************************
        }
    
        public void run() {
            try {
                
                while (true) {
                    byte[] buf = new byte[256];
    
                    //**************************************
                    // Add the lines here to create a DatagramPacket
                    // called packet based
                    // on the data buffer above, and then wait to receive
                    // a packet.
                    //**************************************
                    
                    // figure out response
                    String dString = null;
                    dString = new Date().toString();
                    buf = dString.getBytes();
                    
                    // send the response to the client at "address" 
                    InetAddress address = packet.getAddress();
    
                    //****************************************
                    // Extract the port number from the received packet
                    // for use in the outgoing packet
                    //****************************************
    
    
                    packet = new DatagramPacket(buf, buf.length, address, port);
                    //*****************************************
                    // Add a line here to send the packet
                    //*****************************************
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            socket.close();
        }
        
        public static void main(String[] args) throws IOException {
            new SimpleServer().start();
        }
    }
    
  2. Complete the following client code to interact with the above Server. Remember to use the same port number.
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    public class SimpleClient {
        public static void main(String[] args) throws IOException {
    
            if (args.length != 1) {
                 System.out.println("Usage: java SimpleClient <hostname>");
                 return;
            }
    
            //*********************************************
                // add a line to create a datagram socket
            //**********************************************
    
            // send request
            byte[] buf = new byte[256];
            //*************************************************
            // Add a line to get the address from args[0], the argument
            // handed in
            //*************************************************
    
    
            //*************************************************
            // Add a line to create a packet from buf above and send to
            // the port selected in the SimpleServer
            //*************************************************
            socket.send(packet);
    
            // get response
            packet = new DatagramPacket(buf, buf.length);
    
            //**************************************************
            // Receive the packet
            //**************************************************
            
            // display response
            String received = new String(packet.getData());
            System.out.println("Today's date: " + received);
    
            socket.close();
        }
    }
    
Ian Wakeman 2005-02-22