Threading Client's connected to a Server using Sockets

D

Dundonald

My question is really related to how best use threads but here's the
scenario:

Have a Server class creating a new ServerSocket and listens for any
number of Clients to connect, at which point a Socket is created. A
ClientHandler class (extends thread) is created and is passed the
Socket received from the Client class. The ClientHandler class created
for each new connection is stored in an array in the Server class (to
keep a record of all clients connected).

The Client class simply accepts keyboard input from System.in and
writes to its Socket's output stream.

The ClientHandler's run method simply waits for any thing written to
its input stream.

Question is - what is the best way for the ClientHandler thread to
inform the Server class it has received a message in order for the
Server class to then iterate through its vector of ClientHandler's and
send the message to all Clients?

Is it best to create a very simple 'message' class, with a variable to
hold the message and synchronized accessors to allow a thread to take
control and another boolean variable to indicate if a new message has
been received and is ready to pick up by all ClientHandler threads (to
send to their Socket)? HOw can this be done?

Cheers.
 
P

Phil Staite

Sounds like a simple chat system?

Look up the Observer and Observable interfaces... You could make the
Server an Observer of all the Clients, and then have them notify the
Server when they have something. In fact, they could package the
String/message up and send it to the server in the update call. The
server could then distribute it to all the known clients.
 
S

Steve Horsley

Dundonald said:
My question is really related to how best use threads but here's the
scenario:

Have a Server class creating a new ServerSocket and listens for any
number of Clients to connect, at which point a Socket is created. A
ClientHandler class (extends thread) is created and is passed the
Socket received from the Client class. The ClientHandler class created
for each new connection is stored in an array in the Server class (to
keep a record of all clients connected).

The Client class simply accepts keyboard input from System.in and
writes to its Socket's output stream.

The ClientHandler's run method simply waits for any thing written to
its input stream.

Question is - what is the best way for the ClientHandler thread to
inform the Server class it has received a message in order for the
Server class to then iterate through its vector of ClientHandler's and
send the message to all Clients?

Is it best to create a very simple 'message' class, with a variable to
hold the message and synchronized accessors to allow a thread to take
control and another boolean variable to indicate if a new message has
been received and is ready to pick up by all ClientHandler threads (to
send to their Socket)? HOw can this be done?

Cheers.

My inclination would be to give the server a method that does the
iteration, and give the ClientHandler a method that sends the
message, something like this (I'm using a List called clientHandlers
rather than an array of ClientHandlers - do your own thing though):

// class ClientHandler
/** Send a message string to this client. */
public void sendMessage(String msg) {
out.write(msg);
out.flush();
}

// class Server
/** Send a message to all clients except the one named. */
public void broadcastMessage(String msg, ClientHandler except) {
synchronized (clientHandlers) {
Iterator it = clientHandlers.iterator();
while(it.hasNext()) {
ClientHandler ch = (ClientHandler) it.next();
if(ch != except) {
ch.sendMessage(msg);
}
}
}
}
/** Let the server know a client has gone away.
* This is called by a ClientHandler when the connection breaks.
*/
public void remove(ClientHandler ch) {
synchronized (clinetHandlers) {
clientHandlers.remove(ch);
}
}

HTH
Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top