
/* simpletalk server, UDP version */
/* newline is to be included at client side */

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

public class stalks {
    public stalks() {}

    static public int destport = 5432;
    static public int bufsize = 512;

    static public void main(String args[]) {
        DatagramSocket s;

        try {
                s = new DatagramSocket(destport);
        }
        catch (SocketException se) {
                System.err.println("no socket available");
                return;
        }
        try {
                s.setSoTimeout(5000);       // time in milliseconds
        } catch (SocketException se) {
                System.err.println("socket exception: timeout not set!");
        }

        DatagramPacket msg = new DatagramPacket(new byte[bufsize], bufsize);

        while(true) { // read loop
            try {
                        msg.setLength(bufsize);
                        s.receive(msg);
                        System.err.println("message from <" +
                                msg.getAddress().toString() +
                                "," +
                                msg.getPort() +
                                ">");
            } catch (InterruptedIOException iioe) {
            //} catch (SocketTimeoutException ste) {
                    System.err.println("Response timed out!");
                    continue;
            } catch (IOException ioe) {
                    System.err.println("Bad receive");
                    // continue;
                    break;
            }

            String str = new String(msg.getData(), 0, msg.getLength());
            System.out.print(str);
        }
        s.close();
    } // end of main

}
