• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sending a message to multiple clients with one call

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
catch it before it slithers away! Oh wait, it's a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic