


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

public class PortListener extends Thread
{
	ServerSocket listen_socket;
	
        public PortListener(String portString)
	{
                int port = Integer.parseInt(portString);
		try{
                        listen_socket = new ServerSocket (port);
			
		}
		catch(IOException e) {System.err.println(e);}
		this.start();
                System.out.println("Server started");
	}
	public void run()
	{
		try{
			while(true)
			{
				Socket client_socket = listen_socket.accept();
                                SocketConnection c = new SocketConnection (client_socket);
			}
		}
		catch(IOException e) {System.err.println(e);}
	}
	public static void main(String[] argv)
	{
                if (argv.length != 1)
                        {
                        System.out.println("usage: PortListener <port number>");
                        }
                PortListener pl = new PortListener(argv[0]);
	}
}

class SocketConnection extends Thread
{
	protected Socket client;
        protected DataInputStream in;
        protected OutputStream out;
        protected String query;
        protected String webServer;
        protected Socket webSocket;

        public SocketConnection (Socket client_socket)
	{
		client = client_socket;


		try{
                        in = new DataInputStream(client.getInputStream());
                        out = client.getOutputStream();
		}
		catch(IOException e) 
		{
		 	System.err.println(e);
			try {client.close();} 
			catch (IOException e2) {};
			return;
		}
		this.start();

	}

	public void run()
	{
                String line;
                int myByte;
		try{
                        //print data to stdout
                        line = in.readLine();
                        while (line != null)
                                {
                                System.out.println(line);
                                line = in.readLine();

                                }

		
		}
		catch (IOException e) {}
		finally
		{
			try {
                                client.close();
                                }
			catch (IOException e)
                                {
                                System.out.println("Exception while closing socket:" + e);
                                }

		}
        System.out.println("Connection completed: closed socket");


	}


}
