

import java.net.*;
import java.io.*;

public class EchoServer
{
    public static void main (String[] args) throws IOException
    {
        ServerSocket echoServer = null;
        try
        {
           String portNum = args[0];
           echoServer = new ServerSocket(Integer.parseInt(args[0])); 
           //creat a server socket at port from argument[0]
        }
        catch(IOException e)
        {
            System.err.println("Couldn't listen on port");
            System.exit(-1);
        }

        Socket echoClient = null;
        try
        {
            echoClient = echoServer.accept(); //accept a connection
        }
        catch(IOException e)
        {
            System.err.println("IO error:" + e.getMessage());
            System.exit(-1);
        }
		
        PrintWriter out = new PrintWriter(echoClient.getOutputStream(),true);
        	//gets the socket's output stream and opens a PrintWriter on it
        //if you remove the argument true,what will happen?
        BufferedReader in = new BufferedReader(
                            new InputStreamReader(echoClient.getInputStream()));
     		//gets the socket's input stream and opens a BufferedReader on it
        String strFromClient;

        while((strFromClient = in.readLine()) != null)
        {
            out.println("result is :" + strFromClient);
        }

        in.close();
        out.close();
        echoClient.close();
        echoServer.close();
    }
}