Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2005

Tip: Looking for answers? Try searching our database.

Threading Client's connected to a Server using Sockets

Thread view: 
Dundonald - 15 Mar 2005 00:34 GMT
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.
Phil Staite - 15 Mar 2005 05:00 GMT
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.
Steve Horsley - 15 Mar 2005 19:10 GMT
> My question is really related to how best use threads but here's the
> scenario:
[quoted text clipped - 24 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.