• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Creating inputstream from MulticastSocket

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic