//System.out.println("HelloThread: running");
if (serverSocket == null)
return;
try {
System.out.println("Address of the Server :" + InetAddress.getLocalHost().toString()) ;
}
catch (Exception e) {
System.out.println("HelloThread: " + e);
System.exit(1);
}
while (true) {
System.out.println("HelloThread: Waiting for someone to talk to me: " );
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
}
catch (IOException e) {
System.out.println("Accept failed: " + e);
System.exit(1);
}
try {
System.out.println("HelloThread: set up data streams");
DataInputStream is = new DataInputStream(
new BufferedInputStream(clientSocket.getInputStream()));
PrintWriter os = new PrintWriter(clientSocket.getOutputStream());
// new BufferedOutputStream(clientSocket.getOutputStream(), 1024), false);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String inputLine, outputLine;
String name;
//System.out.println("HelloThread: Someone to talk to! " );
if ((inputLine = br.readLine()) != null) {
System.out.println("Hello Thread: Received:" + inputLine);
outputLine = "";
//someone wants to talk ; tell them the number of their
//private port;
if (inputLine.startsWith("Hello")) {
int portNumber = ChessServer.getPortNumber();
new SendThread(portNumber).start();
outputLine = String.valueOf(portNumber);
System.out.println("Assign " + clientSocket.getInetAddress().toString() + " to port " + outputLine) ;
}
os.println(outputLine);
os.flush();
}
os.close();
is.close();
clientSocket.close();
}
catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
System.out.println("HelloThread:" + e);
}
}