Method: UDPSocket#recvfrom_nonblock
- Defined in:
- lib/socket.rb
#recvfrom_nonblock(len, flag = 0, outbuf = nil, exception: true) ⇒ Object
call-seq:
udpsocket.recvfrom_nonblock(maxlen [, flags[, outbuf [, ]]]) => [mesg, sender_inet_addr]
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#recv_nonblock returns nil. In most cases it means the connection was closed, but it may also mean an empty packet was received, as the underlying API makes it impossible to distinguish these two cases.
Parameters
-
maxlen- the number of bytes to receive from the socket -
flags- zero or more of theMSG_options -
outbuf- destination String buffer -
options- keyword hash, supporting ‘exception: false`
Example
require ‘socket’ s1 = UDPSocket.new s1.bind(“127.0.0.1”, 0) s2 = UDPSocket.new s2.bind(“127.0.0.1”, 0) s2.connect(*s1.addr.values_at(3,1)) s1.connect(*s2.addr.values_at(3,1)) s1.send “aaa”, 0 begin # emulate blocking recvfrom p s2.recvfrom_nonblock(10) #=> [“aaa”, [“AF_INET”, 33302, “localhost.localdomain”, “127.0.0.1”]] rescue IO::WaitReadable IO.select() retry end
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.
See
-
Socket#recvfrom
1697 1698 1699 |
# File 'lib/socket.rb', line 1697 def recvfrom_nonblock(len, flag = 0, outbuf = nil, exception: true) __recvfrom_nonblock(len, flag, outbuf, exception) end |