Non-Blocking receive on MulticastSocket

Discussion in 'Java' started by Ale, Jan 14, 2007.

  1. Ale

    Ale Guest

    Hi all!

    How can I implement a non-blocking receive on a MulticastSocket object?

    With socket objects it was simple:

    socket = new Socket(hostIP, port);
    in = new BufferedReader(new
    InputStreamReader(socket.getInputStream()));
    and so...


    if (in.ready())
    {// Receive data}

    Thanks.

    Ale
     
    Ale, Jan 14, 2007
    #1
    1. Advertising

  2. Ale

    EJP Guest

    Ale wrote:
    > Hi all!
    >
    > How can I implement a non-blocking receive on a MulticastSocket object?
    >
    > if (in.ready())
    > {// Receive data}


    That's not a great technique on a Socket btw, but you can get the effect
    by setting a very short read timeout on a Socket, or a DatagramSocket or
    MulticastSocket which extends it.
     
    EJP, Jan 14, 2007
    #2
    1. Advertising

  3. EJP wrote:
    > Ale wrote:
    >> Hi all!
    >>
    >> How can I implement a non-blocking receive on a MulticastSocket object?
    >>
    >> if (in.ready())
    >> {// Receive data}

    >
    > That's not a great technique on a Socket btw, but you can get the effect
    > by setting a very short read timeout on a Socket, or a DatagramSocket or
    > MulticastSocket which extends it.


    The real question is why would you want to? There is nothing for the
    read thread to do until the data packet arrives.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Jan 14, 2007
    #3
  4. Ale

    EJP Guest

    Knute Johnson wrote:
    > The real question is why would you want to? There is nothing for the
    > read thread to do until the data packet arrives.


    Well we don't know what the OP is actually doing. He *might* be
    simulating Selector.select() on MulticastSockets, as for some bizarre
    reason Sun *still* haven't provided a selectable MulticastSocketChannel
    despite the effort required being pretty trivial.

    But I agree. I think I've seen exactly two valid uses for
    ready()/available()/FIONREAD in about twenty years, and I can't remember
    the other one.
     
    EJP, Jan 15, 2007
    #4
  5. Ale

    Ale Guest

    I need to implement a chat using multicast and without using RMI or
    other middlewares...

    So if the user wants to write (send) a message, the chat client must
    not block on receive... Am I in fault?

    I think i'll set a short timeout on socket....

    Thank you!

    Ale
     
    Ale, Jan 15, 2007
    #5
  6. Ale wrote:
    > I need to implement a chat using multicast and without using RMI or
    > other middlewares...
    >
    > So if the user wants to write (send) a message, the chat client must
    > not block on receive... Am I in fault?
    >
    > I think i'll set a short timeout on socket....
    >
    > Thank you!
    >
    > Ale
    >


    That is not the most efficient way to solve your problem. Java is very
    simple to multi-thread so what I would do is write a thread that is the
    receiver and then use your GUI to be the transmitter. That way you can
    receive data and send data at the same time.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Jan 15, 2007
    #6
  7. Knute Johnson wrote:
    > ...That way you can receive data and send data at the same time.


    This idea, formally known as "full duplex", isn't exactly revolutionary. ;)
     
    John Ersatznom, Jan 16, 2007
    #7
  8. John Ersatznom wrote:
    > Knute Johnson wrote:
    >> ...That way you can receive data and send data at the same time.

    >
    > This idea, formally known as "full duplex", isn't exactly revolutionary. ;)


    It is interesting though that the non-blocking I/O questions are usually
    caused by not understanding this simple concept.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Jan 16, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendra Gunawan
    Replies:
    1
    Views:
    12,954
    Allan Herriman
    Apr 8, 2004
  2. Andre Kelmanson

    blocking i/o vs. non blocking i/o (performance)

    Andre Kelmanson, Oct 10, 2003, in forum: C Programming
    Replies:
    3
    Views:
    984
    Valentin Tihomirov
    Oct 12, 2003
  3. nukleus
    Replies:
    14
    Views:
    887
    Chris Uppal
    Jan 22, 2007
  4. Christian
    Replies:
    5
    Views:
    753
    Esmond Pitt
    Dec 2, 2007
  5. Serge Savoie
    Replies:
    4
    Views:
    287
    Serge Savoie
    Oct 1, 2008
Loading...

Share This Page