recvfrom_nonblock(len, flag = 0, outbuf = nil, exception: true) public

Receives up to maxlen bytes from udpsocket using recvfrom(2) after O_NONBLOCK is set for the underlying file descriptor. flags is zero or more of the MSG_ options. The first element of the results, mesg, is the data received. The second element, sender_inet_addr, is an array to represent the sender address.

When recvfrom(2) returns 0, Socket#recvfrom_nonblock returns an empty string as data. It means an empty packet.


  • maxlen - the number of bytes to receive from the socket

  • flags - zero or more of the MSG_ options

  • outbuf - destination String buffer

  • options - keyword hash, supporting `exception: false`


require 'socket'
s1 = UDPSocket.new
s1.bind("", 0)
s2 = UDPSocket.new
s2.bind("", 0)
s1.send "aaa", 0
begin # emulate blocking recvfrom
  p s2.recvfrom_nonblock(10)  #=> ["aaa", ["AF_INET", 33302, "localhost.localdomain", ""]]
rescue IO::WaitReadable

Refer to Socket#recvfrom for the exceptions that may be thrown if the call to recvfrom_nonblock fails.

UDPSocket#recvfrom_nonblock may raise any error corresponding to recvfrom(2) failure, including Errno::EWOULDBLOCK.

If the exception is Errno::EWOULDBLOCK or Errno::EAGAIN, it is extended by IO::WaitReadable. So IO::WaitReadable can be used to rescue the exceptions for retrying recvfrom_nonblock.

By specifying a keyword argument exception to false, you can indicate that recvfrom_nonblock should not raise an IO::WaitReadable exception, but return the symbol :wait_readable instead.


Show source
Register or log in to add new notes.
October 18, 2009
1 thank

Receiving data over UDP

It’s perfectly normal to receive ‘X’ strings with Ruby’s UDP sockets before the actual content.

Consider the following example:

require 'socket'

PORT = 5500

socket = UDPSocket.new
socket.bind('', PORT)

for i in 1..10
  p socket.recvfrom_nonblock(4096)

Now, sending data with netcat:

echo "Hello APIdock" | nc -vv -u 5500

The application would output:

["X", ["AF_INET", 61755, "localhost", ""]]
["X", ["AF_INET", 61755, "localhost", ""]]
["X", ["AF_INET", 61755, "localhost", ""]]
["X", ["AF_INET", 61755, "localhost", ""]]
["Hello APIdock\n", ["AF_INET", 61755, "localhost", ""]]