?
Solved

Server programming question

Posted on 2003-02-23
8
Medium Priority
?
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 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