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.E.g.
Final List sockets = new CopyOnWriteArrayList(); // 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); } }.
This approach suits my problem more so than the use of JMX. – MindDrip Nov 9 at 19:55 @MindDrip, I have added examples. – Peter Lawrey Nov 9 at 20:02.
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 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 ;).
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.