package SimpleChatOne;
import java.net.Socket;
import java.net.ServerSocket;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
public class Server {
private int serverPort = 0;
private ServerSocket serverSock = null;
private Socket sock = null;
private String messageOfTheDay = null;
private Switchboard switchboard = null;
public Server(int serverPort, String messageOfTheDay) throws IOException {
this.serverPort = serverPort;
this.messageOfTheDay = messageOfTheDay;
switchboard = new Switchboard();
serverSock = new ServerSocket(serverPort);
}
public void waitForConnections() {
while (true) {
try {
sock = serverSock.accept();
System.err.println(this.getClass().getName()+":Accepted new socket, creating new handler for it.");
Handler handler = new Handler(sock, switchboard, messageOfTheDay);
switchboard.add(handler);
handler.start();
System.err.println(this.getClass().getName()+":Finished with socket, waiting for next connection.");
}
catch (IOException e){
e.printStackTrace(System.err);
}
}
}
public static void main(String argv[]) {
int port = 54321;
String messageOfTheDay = "Welcome to the SimpleChatOne server.\n";
Server server = null;
try {
server = new Server(port, messageOfTheDay);
}
catch (IOException e){
e.printStackTrace(System.err);
}
server.waitForConnections();
}
}