// simpletalk CLIENT in java
import java.lang.*;     //pld
import java.net.*;      //pld
import java.io.*;
//import java.io.Externalizable;

public class stalkc {
        public stalkc () {}
    static public BufferedReader bin;
    static public int destport = 5432;
    static public int bufsize = 512;

    //============================================================

    static public void main(String args[]) {
        bin = new BufferedReader(new InputStreamReader(System.in));
        // String desthost = args[0]; //"ulam.math.luc.edu";
        //String desthost = "ulam.math.luc.edu";
        String desthost = "localhost";

        InetAddress dest;
        System.err.print("Looking up address of " + desthost + "...");
        try {
            dest = InetAddress.getByName(desthost);
        }
        catch (UnknownHostException uhe) {
            System.err.println("unknown host: " + desthost);
            return;
        }
        System.err.println(" got it!");

        DatagramSocket s;
        try {
            s = new DatagramSocket();
        }
        catch(IOException ioe) {
                    System.err.println("no socket available");
            return;
        }

	System.err.println("port=" + s.getLocalPort());

        DatagramPacket msg = new DatagramPacket(
                    new byte[0], 0, dest, destport);

        //============================================================

        while (true) {
            String buf;
            int slen;
            try { buf = bin.readLine();}
            catch (IOException ioe) {
                System.err.println("readLine() failed");
                return;
            }

            if (buf == null) break;

            buf = buf.concat("\n"); // protocol says client supplies newline
            slen = buf.length();
            byte[] bbuf = buf.getBytes();

            msg.setData(bbuf);
            msg.setLength(slen);

            try {
                s.send(msg);
            }
            catch (IOException ioe) {
                System.err.println("send() failed");
                return;
            }
        } // while
        s.close();
    }
}
