简体   繁体   中英

Java Client Server Programming: How to pass message from server to All Client threads?

I am creating a program with a server A and multiple clients B, C, D.

BC & D will all message the client with a number X, and I would like to know how it is possible for the server to message ALL clients simultaneously with the latest value for X?

As it stands, it will update only the client who has last passed number X.

Here is the code I have for run()

public void run(){
        String number;
        do
        {
            //Accept message from client on
            //the socket's input stream...
            received = in.readLine();

            //Echo message back to client on
            //the socket's output stream...
            out.println("Number recieved: " + number);      
        }
}

Google up JMS Publish and Subscribe.

Basically: The server publishes to a topic and the clients subscribe to a topic.

The best way to notify clients about something is to use JMX. If you're not supposed to use this technology, then you should keep clients list somewhere in your code (say in static field) and then iterate over this list and send received number

I'm not sure what you're trying to do...but you could try broadcasting a message using socket programming. Check this out

You can add all the sockets to a collection. Send the same message to every socket in the collection. Remove sockets from the collection when they are closed.

eg

final List<Socket> sockets = new CopyOnWriteArrayList<Socket>();

// when you have a new socket
sockets.add(socket);

// when you have a dead socket.
sockets.remove(socket);

// to send the same message to multiple sockets.

public static void sendToAll(byte[] bytes) {
    for(Socket s: sockets)
      try {
       s.getOutputStream().write(bytes);
      } catch (IOException ioe) {
       // handle exception, close the socket.
       sockets.remove(s);
      }
}

I agree the real solution is JMS, but if you want to "roll your own" a simple solution I would suggest is making your own simplified version using the same idea of JMS. Create a class that will receive events from your client. Create an interface that your clients can implement and then add themselves as a listener to this new class. Some simple code:

class MyEventPublisher {
  Collection<EventListener> listeners;

  int number;

  public void addListener(EventListener listener) {
    listeners.add(listener);
  }

  public void setNumber(int newNumber) {
    int oldNumber = this.number;
    this.number = newNumber;
    for (EventListener listener : listeners) {
      listener.numberChanged(newNumber, oldNumber);
    }
  }
}

interface EventListener {
  void numberChanged(int newNumber, int oldNumber);
}

class MyClientSocket implements EventListener {
  MyEventPublisher publisher;

  public MyClientSocket(MyEventPublisher publisher) {
    this.publisher = publisher;
    publisher.addListener(this);
  }

  public recieveNumberFromSocket() {
    int numberFromSocket = readNumber();
    publisher.setNumber(numberFromSocket);
  }

  public void numberChanged(int newNumber, int oldNumber) {
    //someone else changed the number
    //do something interesting with it
  }
}

You are looking for a multicast protocol, based on your descriptions.
So, I'll guess you'll be better of looking this:
Multicast (JDK 6)
Multicast (JDK 7)
Previous versions starting from JDK version 1.4.2 include multicast but you'll be better off if you use JDK version 6 or greater ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM