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

/**
This program implements a multithreaded server that listens to
port 8189 and echoes back all client input.
*/
public class ThreadedEchoServer
{  
public static void main(String[] args )
{  
   try
   {  
      int i = 1;
      ServerSocket s = new ServerSocket(7777);

      for (;;)
      {  
         Socket incoming = s.accept( );
         System.out.println("Cliente  " + i);
         Thread t = new ThreadedEchoHandler(incoming, i);
         t.start();
         i++;
      }
   }
   catch (Exception e)
   {  
      e.printStackTrace();
   }
}
}

/**
This class handles the client input for one server socket
connection. 
*/
class ThreadedEchoHandler extends Thread {
	
	private Socket incoming;
	private int counter;
	/**
   		Constructs a handler.
   		@param i the incoming socket
   		@param c the counter for the handlers (used in prompts)
	 */

	public ThreadedEchoHandler(Socket i, int c) {
		incoming = i;
		counter = c;
	}

	public void run() {
		
		try {
				BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
				PrintWriter out2 = new PrintWriter(incoming.getOutputStream(), true /* autoFlush */);
				System.out.println( ""+incoming.getInetAddress() );
				incoming.close();
			}
		catch (Exception e)
		{
			e.printStackTrace(); 
			}
		}
}
