Socket#recvfrom
static VALUE
sock_recvfrom(int argc, VALUE *argv, VALUE sock)
{
return rsock_s_recvfrom(sock, argc, argv, RECV_SOCKET);
}
Receives up to maxlen bytes from socket. flags is zero or more of the MSG_ options. The first element of the results, mesg, is the data received. The second element, sender_addrinfo, contains protocol-specific address information of the sender.
Parameters
-
maxlen- the maximum number of bytes to receive from the socket -
flags- zero or more of theMSG_options
Example
# In one file, start this first require 'socket' include Socket::Constants socket = Socket.new( AF_INET, SOCK_STREAM, 0 ) sockaddr = Socket.pack_sockaddr_in( 2200, 'localhost' ) socket.bind( sockaddr ) socket.listen( 5 ) client, client_addrinfo = socket.accept data = client.recvfrom( 20 )[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.pack_sockaddr_in( 2200, 'localhost' ) socket.connect( sockaddr ) socket.puts "Watch this get cut short!" socket.close
Unix-based Exceptions
On unix-based based systems the following system exceptions may be raised if the call to recvfrom fails:
-
Errno::EAGAIN- thesocketfile descriptor is marked as O_NONBLOCK and no data is waiting to be received; orMSG_OOBis set and no out-of-band data is available and either thesocketfile descriptor is marked as O_NONBLOCK or thesocketdoes not support blocking to wait for out-of-band-data -
Errno::EBADF- thesocketis not a valid file descriptor -
Errno::ECONNRESET- a connection was forcibly closed by a peer -
Errno::EFAULT- the socket’s internal buffer, address or address length cannot be accessed or written -
Errno::EINTR- a signal interrupted recvfrom before any data was available -
Errno::EINVAL- theMSG_OOBflag is set and no out-of-band data is available -
Errno::EIO- an i/o error occurred while reading from or writing to the filesystem -
Errno::ENOBUFS- insufficient resources were available in the system to perform the operation -
Errno::ENOMEM- insufficient memory was available to fulfill the request -
Errno::ENOSR- there were insufficient STREAMS resources available to complete the operation -
Errno::ENOTCONN- a receive is attempted on a connection-mode socket that is not connected -
Errno::ENOTSOCK- thesocketdoes not refer to a socket -
Errno::EOPNOTSUPP- the specified flags are not supported for this socket type -
Errno::ETIMEDOUT- the connection timed out during connection establishment or due to a transmission timeout on an active connection
Windows Exceptions
On Windows systems the following system exceptions may be raised if the call to recvfrom fails:
-
Errno::ENETDOWN- the network is down -
Errno::EFAULT- the internal buffer and from parameters onsocketare not part of the user address space, or the internal fromlen parameter is too small to accommodate the peer address -
Errno::EINTR- the (blocking) call was cancelled by an internal call to the WinSock function WSACancelBlockingCall -
Errno::EINPROGRESS- a blocking Windows Sockets 1.1 call is in progress or the service provider is still processing a callback function -
Errno::EINVAL-sockethas not been bound with a call to bind, or an unknown flag was specified, orMSG_OOBwas specified for a socket withSO_OOBINLINEenabled, or (for byte stream-style sockets only) the internal len parameter onsocketwas zero or negative -
Errno::EISCONN-socketis already connected. The call to recvfrom is not permitted with a connected socket on a socket that is connection oriented or connectionless. -
Errno::ENETRESET- the connection has been broken due to the keep-alive activity detecting a failure while the operation was in progress. -
Errno::EOPNOTSUPP-MSG_OOBwas specified, butsocketis not stream-style such as typeSOCK_STREAM. OOB data is not supported in the communication domain associated withsocket, orsocketis unidirectional and supports only send operations -
Errno::ESHUTDOWN-sockethas been shutdown. It is not possible to call recvfrom on a socket after shutdown has been invoked. -
Errno::EWOULDBLOCK-socketis marked as nonblocking and a call to recvfrom would block. -
Errno::EMSGSIZE- the message was too large to fit into the specified buffer and was truncated. -
Errno::ETIMEDOUT- the connection has been dropped, because of a network failure or because the system on the other end went down without notice -
Errno::ECONNRESET- the virtual circuit was reset by the remote side executing a hard or abortive close. The application should close the socket; it is no longer usable. On a UDP-datagram socket this error indicates a previous send operation resulted in an ICMP Port Unreachable message.