ChatThread.java
import java.io.PrintWriter;
import java.net.*;
import java.util.*;
public class ChatThread extends Thread {
private Socket socket;
private ServerSocket s_socket;
private Scanner in;
public PrintWriter out;
private String input;
public ChatThread(ServerSocket ss)
{ s_socket = ss;}
@Override
public void run()
{
try {
while (true) {//While ServerFrame is running
socket=s_socket.accept();
System.out.println("\n listening on port "+socket.getLocalPort()+
"\n sending on port "+socket.getPort()+"\n\n");
in = new Scanner(socket.getInputStream()); //From Client
out = new PrintWriter(socket.getOutputStream()); //To Client
while (true) {
System.out.println("Waiting");
if (in.hasNext()) {
System.out.println("Got A Line");
input = in.nextLine();
if (input.equals("start_up123")) {
//What if a Client is already connected on this port?
System.out.println("Connected");
out.println("Connected");
out.flush();
continue;
}
System.out.println("Client Said: " + input);
if (input.equals("shut_down123")) {
in.close();
out.close();
socket.close();
break;
}
ServerFrame.broadcast(input);
// out.println("The Server Heard you Say: " + input);
// out.flush();
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}