import java.net.*;
import java.io.*;
//import java.util.*;
public class ChatServeThread extends Thread
{
ServerSocket serverSocket = null;
Socket clientSocket = null;
PrintWriter clientOut = null;
BufferedReader clientIn = null;
public ChatServeThread() throws IOException
{
this("ChatServeThread");
}
public ChatServeThread(String name) throws IOException
{
super(name); //creates a thread with the same name
//do not use 0-1023 aka known ports
serverSocket = new ServerSocket(4444);
}
public void run()
{
//the server accepts the client's request to logon
try{
clientSocket = serverSocket.accept();
}catch (IOException e){
System.err.println("clientSocket not accepted");
System.exit(1);
}
try{
clientOut = new PrintWriter(clientSocket.getOutputStream(), true);
clientIn = new BufferedReader(
new InputStreamReader( clientSocket.getInputStream() )
);
} catch (IOException e) {
System.err.println("couldn't get I/O from clientSocket");
System.exit(1);
}
try {
String inputLine, outputLine;
ChatPt3 cp = new ChatPt3(); //the chat protocol
outputLine = cp.processInput(null);
clientOut.println(outputLine);
while ((inputLine = clientIn.readLine()) != null) {
outputLine = cp.processInput(inputLine);
clientOut.println(outputLine);
if (outputLine.equals("Bye."))
break;
}
clientOut.close();
clientIn.close();
clientSocket.close();
serverSocket.close();
} catch (IOException e) {
System.err.println("I/O problems");
System.exit(1);
}
}
}//class close brace