/* Program MyServer.java */

import java.net.*;
import java.io.*;

public class MyServer {
    public static void main(String[] args) throws IOException {


			// creates a new server type socket
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(2000);
        } catch (IOException e) {
            System.err.println("Could not listen on port: 4444.");
            System.exit(1);
        }


	// Wait for the client connection. The accept() method will
	// block until a connection comes to port 2000.
	// A new socket created accept() is used for transactions.
	// The original one i only for waiting for connections.
	// For more info read on unix TCP/IP sockets.

        Socket clientSocket = null;
        try {
            clientSocket = serverSocket.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.exit(1);
        }

	// create input and output streams based on the newly
	// created connection.

        PrintWriter out = 
		new PrintWriter(clientSocket.getOutputStream(), true);
        BufferedReader in = new BufferedReader(
				new InputStreamReader(
				clientSocket.getInputStream()));
        String inputLine, outputLine;


	// We will echo the input to the sending client.
        while ((inputLine = in.readLine()) != null) {
             outputLine = inputLine;	// echo only
             out.println(outputLine);
             if (outputLine.equals("quit"))
                break;
        }
        out.close();
        in.close();
        clientSocket.close();
        serverSocket.close();
    }
}

