aspose file tools
The moose likes Sockets and Internet Protocols and the fly likes Sending a message to multiple clients with one call Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "Sending a message to multiple clients with one call" Watch "Sending a message to multiple clients with one call" New topic
Author

Sending a message to multiple clients with one call

Vega Zimur
Greenhorn

Joined: Jul 14, 2010
Posts: 9
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

Joined: Oct 27, 2005
Posts: 19216

TCP doesn't allow for multicast. UDP (using DataGramSocket) does, but it's far from reliable. Data you send may not arrive.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Vega Zimur
Greenhorn

Joined: Jul 14, 2010
Posts: 9
:/
I've got an idea how to do it in TCP but I haven't tested it yet:
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

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

Joined: Jul 14, 2010
Posts: 9
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

Joined: Jul 14, 2010
Posts: 9
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

Joined: Jul 14, 2010
Posts: 9
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending a message to multiple clients with one call
 
Similar Threads
Sockets and Threads
Sockets and Threads
simultaneosly running server class and JFrame, the JFrame wont show the components.
Sockets using threads
Threads and global variable prolems