: I have been given an assignment to write a simple socket based chat server/client application. The client applet needs to connect to the server app on startup, the server needs to be able to deal with multiple clients, with no limit on the number of clients, and the hostname/IP address of the server must not be hardcoded into the client.
: My main problem is that I am not sure where to start on the server!!
: Any links or help that you could provide me with would be extermely helpful...thanks in advance!!
Where are you supposed to get the IP and port? Enter it through command line arguments?
I think the easiest way is to have one class that accepts connections and then another class that either extends Thread or implements Runnable, and start a new thread for each user, and also have a vector that holds each socket object, and make it static and create a static method that will send out the message to everyone connected.
Something like this:
public class Chat implements Runnable
private static Vector sockets;
//whatever else you need, probably a string to hold the users name
public Chat(Socket sock, ...)
//add socket to vector
public void run()
//recieve messages, and disconnect handling here
//pass message to broadcast
public static void broadcast(String str)
//loop through vector and send message to everyone
On second thought, all you would need is a vector of whatever output stream class you are using in the vector, and not the socket objects. You might still need to pass the socket objects so you can close it properly and let the thread terminate, whether the cleint disconnects properly or unexpectedly.
Just my 2 bits