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

public class srv2 {

static String port = "5194";

public static void main(String[] argv) {
    if (argv.length == 0)
        srv2(port);
    else
        srv2(argv[0]);
}

public static void srv2(String port) {    // tcp/ip version
    try {
        ServerSocket server = new ServerSocket(Integer.parseInt(port));
        while (true) {
            Socket sock = server.accept();
System.err.println("srv2 " + sock);
            new echo(sock);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}


class echo extends Thread {
    Socket sock;

echo(Socket sock) {
    this.sock = sock;
    start();
}

public void run() {
    try {
        DataInputStream in = 
            new DataInputStream(sock.getInputStream());
        DataOutputStream out = 
            new DataOutputStream(sock.getOutputStream());
        String s;
        while ((s = in.readLine()) != null) {
            out.writeBytes(s);
            out.writeByte('\n');
            out.flush();
System.err.println(sock.getInetAddress() + " " + s);
            if (s.equals("exit"))
                break;
            if (s.equals("die!"))
		System.exit(0);
        }
        sock.close();
    } catch (IOException e) {
        System.err.println("server exception " + e);
    }
}

}
