recvfrom_nonblock(...)
public
Receives up to maxlen bytes from socket 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_sockaddr, contains
protocol-specific information on the sender.
When recvfrom(2) returns 0, Socket#recvfrom_nonblock returns
an empty string as data. The meaning depends on the socket: EOF on TCP,
empty packet on UDP, etc.
Parameters
Example
require 'socket'
include Socket::Constants
socket = Socket.new(AF_INET, SOCK_STREAM, 0)
sockaddr = Socket.sockaddr_in(2200, 'localhost')
socket.bind(sockaddr)
socket.listen(5)
client, client_sockaddr = socket.accept
begin
pair = client.recvfrom_nonblock(20)
rescue Errno::EAGAIN, Errno::EWOULDBLOCK
IO.select([client])
retry
end
data = pair[0].chomp
puts "I only received 20 bytes '#{data}'"
sleep 1
socket.close
require 'socket'
include Socket::Constants
socket = Socket.new(AF_INET, SOCK_STREAM, 0)
sockaddr = Socket.sockaddr_in(2200, 'localhost')
socket.connect(sockaddr)
socket.puts "Watch this get cut short!"
socket.close
Refer to Socket#recvfrom for the
exceptions that may be thrown if the call to recvfrom_nonblock fails.
Socket#recvfrom_nonblock may
raise any error corresponding to recvfrom(2) failure, including
Errno::EAGAIN.
See
Show source
/*
* call-seq:
* socket.recvfrom_nonblock(maxlen) => [mesg, sender_sockaddr]
* socket.recvfrom_nonblock(maxlen, flags) => [mesg, sender_sockaddr]
*
* Receives up to _maxlen_ bytes from +socket+ 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_sockaddr_, contains protocol-specific information
* on the sender.
*
* When recvfrom(2) returns 0, Socket#recvfrom_nonblock returns
* an empty string as data.
* The meaning depends on the socket: EOF on TCP, empty packet on UDP, etc.
*
* === Parameters
* * +maxlen+ - the number of bytes to receive from the socket
* * +flags+ - zero or more of the +MSG_+ options
*
* === Example
* # In one file, start this first
* require 'socket'
* include Socket::Constants
* socket = Socket.new(AF_INET, SOCK_STREAM, 0)
* sockaddr = Socket.sockaddr_in(2200, 'localhost')
* socket.bind(sockaddr)
* socket.listen(5)
* client, client_sockaddr = socket.accept
* begin
* pair = client.recvfrom_nonblock(20)
* rescue Errno::EAGAIN, Errno::EWOULDBLOCK
* IO.select([client])
* retry
* end
* data = pair[0].chomp
* puts "I only received 20 bytes '#{data}'"
* sleep 1
* socket.close
*
* # In another file, start this second
* require 'socket'
* include Socket::Constants
* socket = Socket.new(AF_INET, SOCK_STREAM, 0)
* sockaddr = Socket.sockaddr_in(2200, 'localhost')
* socket.connect(sockaddr)
* socket.puts "Watch this get cut short!"
* socket.close
*
* Refer to Socket#recvfrom for the exceptions that may be thrown if the call
* to _recvfrom_nonblock_ fails.
*
* Socket#recvfrom_nonblock may raise any error corresponding to recvfrom(2) failure,
* including Errno::EAGAIN.
*
* === See
* * Socket#recvfrom
*/
static VALUE
sock_recvfrom_nonblock(int argc, VALUE *argv, VALUE sock)
{
return s_recvfrom_nonblock(sock, argc, argv, RECV_SOCKET);
}