• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating inputstream from MulticastSocket  RSS feed

 
night mare
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a method like this:

public void readData(java.io.InputStream aStream);

and my data will come from MulticastSocket(http://download.oracle.com/javase/1.4.2/docs/api/java/net/MulticastSocket.html)

For using readData method I have to create an InputStream using MulticastSocket.

So my question is: "How can i create an InputStream using MulticastSocket ?"

Thanks for help.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MulticastSocket works with datagram packets, not streams. The reason is because the underlying protocol, UDP, does not guarantee any of these packets to arrive, or for them to arrive in the order they were sent.

You can create a new InputStream sub class around the MulticastSocket that reads the packets and let its read methods return the data from these packets. That's not going to be easy, for two reasons:
1) you will need two threads - one for receiving packets and one for reading the data from them. You'll need to cache the received packets. If you try to do it from one thread your chances of missing a packet will increase if the reading code is not fast enough.

2) how do you detect the end of the stream? Receiving a packet doesn't return null, -1 or anything else to make a distinction to find out there won't be any more packets.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!