Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Sending a message to multiple clients with one call  RSS feed

 
Vega Zimur
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the moment it's something like this:


This code works fine.
But in line 16 you can see that the content of the stream has to be generated again and again for every Client. -> is there a way to send it to all streams at once?
 
Rob Spoor
Sheriff
Posts: 21047
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TCP doesn't allow for multicast. UDP (using DataGramSocket) does, but it's far from reliable. Data you send may not arrive.
 
Vega Zimur
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
:/
I've got an idea how to do it in TCP but I haven't tested it yet:
 
Rob Spoor
Sheriff
Posts: 21047
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll have to overwrite all OutputStream methods, including flush and close, and use a loop to call that same method on all contained output streams. But that basic code you have there should work.
 
Vega Zimur
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, you're right, so I'll also overwrite the flush method.

But the good thing is that you can add new clients by just adding them to the Vector:

 
Vega Zimur
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I've tested it and it nearly works.
The only problem is that the ObjectOutputStream sends the typeName of java.util.Date only once, so when there's a client which connnects later doesn't know what to do with the incoming data.
How do I force the ObjectOutputStream to print the classDescriptors.

here's the stdout("..." is for the data sections):
Initializing
Configuring streams
Starting server
Server is running
Sending time
...java.util.Date...
Sending time
......
Sending time
......
 
Vega Zimur
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yippee

When I change the server and the client a bit it works:


now I can also attach a File using FileOutputStream or f.e. the stream System.out
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!