/*
* Gary Cornell and Cay S. Horstmann, Core Java (Book/CD-ROM)
* Published By SunSoft Press/Prentice-Hall
* Copyright (C) 1996 Sun Microsystems Inc.
* All Rights Reserved. ISBN 0-13-565755-5
*
* Permission to use, copy, modify, and distribute this
* software and its documentation for NON-COMMERCIAL purposes
* and without fee is hereby granted provided that this
* copyright notice appears in all copies.
*
* THE AUTHORS AND PUBLISHER MAKE NO REPRESENTATIONS OR
* WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE, EITHER
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. THE AUTHORS
* AND PUBLISHER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED
* BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
* THIS SOFTWARE OR ITS DERIVATIVES.
*/
/**
* @version 1.00 07 Feb 1996
* @author Cay Horstmann
*/
import java.io.*;
import java.net.*;
class ThreadedEchoHandler extends Thread
{ Socket incoming;
int counter;
ThreadedEchoHandler(Socket i, int c)
{ incoming = i; counter = c; }
public void run()
{ try
{ DataInputStream in = new DataInputStream(incoming.getInputStream());
PrintStream out = new PrintStream(incoming.getOutputStream());
out.println( "Hello! Enter BYE to exit.\r" );
boolean done = false;
while (!done)
{ String str = in.readLine();
if (str == null) done = true;
else
{ out.println("Echo (" + counter + "): " + str + "\r");
if (str.trim().equals("BYE"))
done = true;
}
}
incoming.close();
}
catch (Exception e)
{ System.out.println(e);
}
}
}
class ThreadedEchoServer
{ public static void main(String[] args )
{ int i = 1;
try
{ ServerSocket s = new ServerSocket(8189);
for (;;)
{ Socket incoming = s.accept( );
System.out.println("Spawning " + i);
new ThreadedEchoHandler(incoming, i).start();
i++;
}
}
catch (Exception e)
{ System.out.println(e);
}
}
}