?
Solved

Server programming question

Posted on 2003-02-23
8
Medium Priority
?
208 Views
Last Modified: 2010-03-31
I'm writing a server in java that uses threads to have multiple clients connect to the same serversocket.  My question is this:  How can I exchange info between the connected clients?

For instance, if there are 3 clients connected, and clientA sends some information to the server; how can I have the server send that info to ALL the connected clients?

Here is what my server looks like:
ServerSocket server_socket;
try{
    server_socket = new ServerSocket(1000);
  } catch (IOException e) {
    System.out.println("Could not listen on port 1000");
    System.exit(-1);
  }
  while(true){
    ServerThread newConnection;
    try{
      newConnection = new serverThread(server_socket.accept());
      newConnection.start();
    } catch (IOException e) {
      System.out.println("Accept failed: 4444");
      System.exit(-1);
    }
  }
0
Comment
Question by:lwinkenb
  • 3
  • 3
  • 2
8 Comments
 

Expert Comment

by:fivesigmaevent
ID: 8006178
You could try to pass a shared buffer to the thread constructor.

newConnection = new serverThread(server_socket.accept(), sharedBuffer );

Use the buffer to communicate between threads. You probably should synchronize access to the buffer so not to corrupt any data.
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 8006380
Im not really sure how a shared buffer works.  Could you point me to an example?
0
 
LVL 2

Expert Comment

by:warsql
ID: 8006424
Sounds like an event model is what you are looking for.  The server could keep a list of ServerThread connections and notify each whenever a message comes in.  Just make sure to remove the ServerThread from the list when closed to avoid a memory leak.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:lwinkenb
ID: 8006488
could you post an example to get me started warsql?
0
 
LVL 2

Accepted Solution

by:
warsql earned 800 total points
ID: 8006532
ServerSocket server_socket;
Set connections = new HashSet();
try{
   server_socket = new ServerSocket(1000);
 } catch (IOException e) {
   System.out.println("Could not listen on port 1000");
   System.exit(-1);
 }
 while(true){
   ServerThread newConnection;
   try{
     newConnection = new serverThread(server_socket.accept(), this);
     newConnection.start();
     connections.add(newConnection);
   } catch (IOException e) {
     System.out.println("Accept failed: 4444");
     System.exit(-1);
   }
 }
 
public void notify(String message) {
  // Iterate through connections and send message
  Iterator iterator = connections.iterator();
  while (iterator.next()) {
    ((serverThread) iterator.next()).callback(message);
  }
}


In class serverThread, change constructor to have a reference to server.  Also, have a method callback for server to send a message to that sends message through socket back to client.

When a serverThread gets a message, call notify in server.  Note that the client that sends the message will get it back.  Avoiding that will be a little more work.  You could notify method to:
public void notify(String message, serverThread sender) {
  // Iterate through connections and send message skipping sender
}

0
 

Expert Comment

by:fivesigmaevent
ID: 8006617
I was going to change what I said about passing a buffer to passing "this" and invoking a callback for each thread. Looks like warsql beat me to it!

This community is awesome.
0
 
LVL 5

Author Comment

by:lwinkenb
ID: 8007416
Thanks a lot, perfect answer.
0
 
LVL 2

Expert Comment

by:warsql
ID: 8013270
After further thought, the connections variable will be accessed by different threads, so you will want to synchronize around its access.

synchronized(connections) {
// add to connections
}

and

syncronized(connections) {
// iterate through connections
}

just in case a client connects while a message is being broadcast.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Don’ts and Dos are two important end products of software testing basics that a tester needs to regard. This article attempts to explain the principles of both.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month9 days, 20 hours left to enroll

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question