In this homework, you'll implement a simple Chat Server. Your server will start listening for connections. When a connection arrives, it will create a new thread to handle that connection (maintain a list of connections). When a string (line of text, your sockets are reading lines of text) arrives on any connection, the server will forward that string to all other connections. Pseudo code might look something like this:
List clients;
ServerSocket server = createserversocket;
for(;;) {
Socket client = server.acceptconnection();
addclienttolist(client);
createthread(client);
}
// place that handles the thread
void run() {
while(;;) {
String line = readlinefromclient();
sendlinetoeverytone(line);
}
}
// some other function
void sendlinetoeveryone(string line) {
forall(clients) {
sendtothatclient(line);
}
}
The code isn't much longer than the pseudo code, but Id like you all to figure it out on your own. Now, when you run the server, you should be able to telnet into that server. If more than one person telnets into that server, then everything one person types will be seen by everyone else. You can test it by running multiple Telnet instances (in different terminals), and seeing whether everything you type in one window appears in the other window. (you can google on how to install telnet client for your operating system).