UDP socket receive question

Discussion in 'Ruby' started by Martin Pirker, Oct 23, 2004.

  1. Hello...

    Copying the minimalistic UDP receive example from the online Pickaxe I

    require 'socket'
    port = 12000
    server = UDPSocket.open
    server.bind(nil, port)
    loop do
    text,sender = server.recvfrom(1024)
    puts text
    end


    localhost --> localhost UDP connections work fine


    so I tried connecting 2 different boxes
    check network connection by netcat:

    sender: echo "whatever" |netcat -u 192.168.x.y 12000
    receiver: netcat -u -l -p 12000
    connection is ok

    receiver netcat and sender Ruby script work ok, too


    but, with receiver Ruby (like above) neither sender netcat nor Ruby
    script are able to make a transfer -> Ruby receiver drops pakets?!?

    lsof shows a "UDP localhost:12000" binding, so everything should be ok
    on the receiving side


    Question:
    I simply want a Ruby script collecting all UDP pakets arriving at a
    specific port, above script only works for me localhost-->localhost
    why?


    ruby 1.8.2 (2004-07-16) [i686-linux] (aka pre1)


    thanks for any hint, I'm out of ideas
    Martin
     
    Martin Pirker, Oct 23, 2004
    #1
    1. Advertising

  2. Martin Pirker wrote:
    > Hello...
    >
    > Copying the minimalistic UDP receive example from the online Pickaxe I
    >
    > require 'socket'
    > port = 12000
    > server = UDPSocket.open
    > server.bind(nil, port)

    ^^^
    Try a hostname here? You can use the string '<any>'. I guess that means
    any interface.

    > loop do
    > text,sender = server.recvfrom(1024)
    > puts text
    > end
    >
    >
    > localhost --> localhost UDP connections work fine
    >
    >
    > so I tried connecting 2 different boxes
    > check network connection by netcat:
    >
    > sender: echo "whatever" |netcat -u 192.168.x.y 12000
    > receiver: netcat -u -l -p 12000
    > connection is ok
    >
    > receiver netcat and sender Ruby script work ok, too
    >
    >
    > but, with receiver Ruby (like above) neither sender netcat nor Ruby
    > script are able to make a transfer -> Ruby receiver drops pakets?!?
    >
    > lsof shows a "UDP localhost:12000" binding, so everything should be ok
    > on the receiving side
    >
    >
    > Question:
    > I simply want a Ruby script collecting all UDP pakets arriving at a
    > specific port, above script only works for me localhost-->localhost
    > why?
    >
    >
    > ruby 1.8.2 (2004-07-16) [i686-linux] (aka pre1)
    >
    >
    > thanks for any hint, I'm out of ideas
    > Martin
     
    Joel VanderWerf, Oct 23, 2004
    #2
    1. Advertising

  3. Martin Pirker

    Eric Hodel Guest

    --Apple-Mail-1--15181070
    Content-Transfer-Encoding: 7bit
    Content-Type: text/plain; charset=US-ASCII; format=flowed

    On Oct 22, 2004, at 9:34 PM, Joel VanderWerf wrote:

    > Martin Pirker wrote:
    >> Hello...
    >> Copying the minimalistic UDP receive example from the online Pickaxe I
    >> require 'socket'
    >> port = 12000
    >> server = UDPSocket.open
    >> server.bind(nil, port)

    > ^^^
    > Try a hostname here? You can use the string '<any>'. I guess that
    > means any interface.


    No. nil means bind to all interfaces. See getaddrinfo(3).

    --
    Eric Hodel - - http://segment7.net
    All messages signed with fingerprint:
    FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04


    --Apple-Mail-1--15181070
    content-type: application/pgp-signature; x-mac-type=70674453;
    name=PGP.sig
    content-description: This is a digitally signed message part
    content-disposition: inline; filename=PGP.sig
    content-transfer-encoding: 7bit

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (Darwin)

    iD8DBQFBeo9EMypVHHlsnwQRAkbWAJ0W3LHh3WFkk1VDZkTbFwK7z6JNbwCfa4kV
    Ns7YxZ7LVleh77FJ2UU01ms=
    =hlFS
    -----END PGP SIGNATURE-----

    --Apple-Mail-1--15181070--
     
    Eric Hodel, Oct 23, 2004
    #3
  4. Eric Hodel wrote:
    > On Oct 22, 2004, at 9:34 PM, Joel VanderWerf wrote:
    >
    >> Martin Pirker wrote:
    >>
    >>> Hello...
    >>> Copying the minimalistic UDP receive example from the online Pickaxe I
    >>> require 'socket'
    >>> port = 12000
    >>> server = UDPSocket.open
    >>> server.bind(nil, port)

    >>
    >> ^^^
    >> Try a hostname here? You can use the string '<any>'. I guess that
    >> means any interface.

    >
    >
    > No. nil means bind to all interfaces. See getaddrinfo(3).


    Ok, but UDPSocket#bind accepts '<any>' as well. It appears (from brief
    groping in etx/socket/socket.c) that nil and '<any>' are synonyms. So
    substituting one for the other will not make a difference in the problem
    the OP was trying to fix.
     
    Joel VanderWerf, Oct 23, 2004
    #4
  5. Joel VanderWerf <> wrote:
    > Eric Hodel wrote:
    >> On Oct 22, 2004, at 9:34 PM, Joel VanderWerf wrote:
    >>> Martin Pirker wrote:
    >>>> Copying the minimalistic UDP receive example from the online Pickaxe I
    >>>> require 'socket'
    >>>> port = 12000
    >>>> server = UDPSocket.open
    >>>> server.bind(nil, port)
    >>> ^^^
    >>> Try a hostname here? You can use the string '<any>'. I guess that
    >>> means any interface.

    >>
    >> No. nil means bind to all interfaces. See getaddrinfo(3).

    >
    > Ok, but UDPSocket#bind accepts '<any>' as well. It appears (from brief
    > groping in etx/socket/socket.c) that nil and '<any>' are synonyms. So
    > substituting one for the other will not make a difference in the problem
    > the OP was trying to fix.



    but it does fix it

    nil gives a "UDP localhost:12000" binding according to lsof

    "<any>" and "" gives a "UDP *:12000" binding and appears to work as
    expected => accepts any packet arriving from any sender at this port


    question is, why I didn't find this myself?

    I assumed nothing set aka nil means "don't bind to anything, accept
    everything", so I just copied from the example
    reading the "socket-level access" paragraph at the beginning points
    one to "" as INADDR_ANY, but nowhere a word of "<any>" or a definiton
    of nil ?

    thanks! always nice&helpful ppl here,
    Martin
     
    Martin Pirker, Oct 23, 2004
    #5
    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. Andrew Chin

    receive udp packets on windows xp

    Andrew Chin, Dec 8, 2005, in forum: Java
    Replies:
    3
    Views:
    952
    Roedy Green
    Dec 8, 2005
  2. Tom St Denis

    Re: How to format/send/receive UDP packet in C?

    Tom St Denis, Jun 24, 2003, in forum: C Programming
    Replies:
    0
    Views:
    3,073
    Tom St Denis
    Jun 24, 2003
  3. Replies:
    0
    Views:
    1,880
  4. Rahul Gandhi

    Re: How to format/send/receive UDP packet in C?

    Rahul Gandhi, Jun 24, 2003, in forum: C Programming
    Replies:
    0
    Views:
    8,778
    Rahul Gandhi
    Jun 24, 2003
  5. Kishore_R

    Socket() failed for UDP socket

    Kishore_R, Oct 11, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    230
    Anno Siegel
    Oct 11, 2005
Loading...

Share This Page