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.
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.