• 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

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: 22791
131
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic