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.
maxlen
- the maximum number of bytes to receive from the socket
flags
- zero or more of the MSG_
options
# 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
On unix-based based systems the following system exceptions may be raised if the call to recvfrom fails:
Errno::EAGAIN - the socket
file descriptor is marked as O_NONBLOCK and no data is waiting to be received; or MSG_OOB
is set and no out-of-band data is available and either the socket
file descriptor is marked as O_NONBLOCK or the socket
does not support blocking to wait for out-of-band-data
Errno::EWOULDBLOCK - see Errno::EAGAIN
Errno::EBADF - the socket
is 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 - the MSG_OOB
flag 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 - the socket
does 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
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 on socket
are 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 - socket
has not been bound with a call to bind, or an unknown flag was specified, or MSG_OOB
was specified for a socket with SO_OOBINLINE
enabled, or (for byte stream-style sockets only) the internal len parameter on socket
was zero or negative
Errno::EISCONN - socket
is 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_OOB
was specified, but socket
is not stream-style such as type SOCK_STREAM
. OOB data is not supported in the communication domain associated with socket
, or socket
is unidirectional and supports only send operations
Errno::ESHUTDOWN - socket
has been shutdown. It is not possible to call recvfrom on a socket after shutdown has been invoked.
Errno::EWOULDBLOCK - socket
is 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.
Creates a pair of sockets connected each other.
domain should be a communications domain such as: :INET, :INET6, :UNIX, etc.
socktype should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
protocol should be a protocol defined in the domain, defaults to 0 for the domain.
s1, s2 = Socket.pair(:UNIX, :STREAM, 0) s1.send "a", 0 s1.send "b", 0 s1.close p s2.recv(10) #=> "ab" p s2.recv(10) #=> "" p s2.recv(10) #=> "" s1, s2 = Socket.pair(:UNIX, :DGRAM, 0) s1.send "a", 0 s1.send "b", 0 p s2.recv(10) #=> "a" p s2.recv(10) #=> "b"
Sets a socket option. These are protocol and system specific, see your local system documentation for details.
level
is an integer, usually one of the SOL_ constants such as Socket::SOL_SOCKET, or a protocol level. A string or symbol of the name, possibly without prefix, is also accepted.
optname
is an integer, usually one of the SO_ constants, such as Socket::SO_REUSEADDR. A string or symbol of the name, possibly without prefix, is also accepted.
optval
is the value of the option, it is passed to the underlying setsockopt() as a pointer to a certain number of bytes. How this is done depends on the type:
Integer: value is assigned to an int, and a pointer to the int is passed, with length of sizeof(int).
true or false: 1 or 0 (respectively) is assigned to an int, and the int is passed as for an Integer
. Note that false
must be passed, not nil
.
String: the string’s data and length is passed to the socket.
socketoption
is an instance of Socket::Option
Some socket options are integers with boolean values, in this case setsockopt
could be called like this:
sock.setsockopt(:SOCKET, :REUSEADDR, true) sock.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, true) sock.setsockopt(Socket::Option.bool(:INET, :SOCKET, :REUSEADDR, true))
Some socket options are integers with numeric values, in this case setsockopt
could be called like this:
sock.setsockopt(:IP, :TTL, 255) sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_TTL, 255) sock.setsockopt(Socket::Option.int(:INET, :IP, :TTL, 255))
Option values may be structs. Passing them can be complex as it involves examining your system headers to determine the correct definition. An example is an ip_mreq
, which may be defined in your system headers as:
struct ip_mreq { struct in_addr imr_multiaddr; struct in_addr imr_interface; };
In this case setsockopt
could be called like this:
optval = IPAddr.new("224.0.0.251").hton + IPAddr.new(Socket::INADDR_ANY, Socket::AF_INET).hton sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_ADD_MEMBERSHIP, optval)
Gets a socket option. These are protocol and system specific, see your local system documentation for details. The option is returned as a Socket::Option
object.
level
is an integer, usually one of the SOL_ constants such as Socket::SOL_SOCKET, or a protocol level. A string or symbol of the name, possibly without prefix, is also accepted.
optname
is an integer, usually one of the SO_ constants, such as Socket::SO_REUSEADDR. A string or symbol of the name, possibly without prefix, is also accepted.
Some socket options are integers with boolean values, in this case getsockopt
could be called like this:
reuseaddr = sock.getsockopt(:SOCKET, :REUSEADDR).bool optval = sock.getsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR) optval = optval.unpack "i" reuseaddr = optval[0] == 0 ? false : true
Some socket options are integers with numeric values, in this case getsockopt
could be called like this:
ipttl = sock.getsockopt(:IP, :TTL).int optval = sock.getsockopt(Socket::IPPROTO_IP, Socket::IP_TTL) ipttl = optval.unpack("i")[0]
Option values may be structs. Decoding them can be complex as it involves examining your system headers to determine the correct definition. An example is a +struct linger+, which may be defined in your system headers as:
struct linger { int l_onoff; int l_linger; };
In this case getsockopt
could be called like this:
# Socket::Option knows linger structure. onoff, linger = sock.getsockopt(:SOCKET, :LINGER).linger optval = sock.getsockopt(Socket::SOL_SOCKET, Socket::SO_LINGER) onoff, linger = optval.unpack "ii" onoff = onoff == 0 ? false : true
Returns the local address of the socket as a sockaddr string.
TCPServer.open("127.0.0.1", 15120) {|serv| p serv.getsockname #=> "\x02\x00;\x10\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00" }
If Addrinfo
object is preferred over the binary string, use BasicSocket#local_address
.
Receives a message and return the message as a string and an address which the message come from.
maxlen is the maximum number of bytes to receive.
flags should be a bitwise OR of Socket::MSG_* constants.
ipaddr is same as IPSocket#{peeraddr,addr}.
u1 = UDPSocket.new u1.bind("127.0.0.1", 4913) u2 = UDPSocket.new u2.send "uuuu", 0, "127.0.0.1", 4913 p u1.recvfrom(10) #=> ["uuuu", ["AF_INET", 33230, "localhost", "127.0.0.1"]]
returns the socket type as an integer.
Addrinfo.tcp("localhost", 80).socktype == Socket::SOCK_STREAM #=> true
Receives a message via unixsocket.
maxlen is the maximum number of bytes to receive.
flags should be a bitwise OR of Socket::MSG_* constants.
outbuf will contain only the received data after the method call even if it is not empty at the beginning.
s1 = Socket.new(:UNIX, :DGRAM, 0) s1_ai = Addrinfo.unix("/tmp/sock1") s1.bind(s1_ai) s2 = Socket.new(:UNIX, :DGRAM, 0) s2_ai = Addrinfo.unix("/tmp/sock2") s2.bind(s2_ai) s3 = UNIXSocket.for_fd(s2.fileno) s1.send "a", 0, s2_ai p s3.recvfrom(10) #=> ["a", ["AF_UNIX", "/tmp/sock1"]]
Creates a pair of sockets connected to each other.
socktype should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
protocol should be a protocol defined in the domain. 0 is default protocol for the domain.
s1, s2 = UNIXSocket.pair s1.send "a", 0 s1.send "b", 0 p s2.recv(10) #=> "ab"
Returns current locale id (lcid). The default locale is WIN32OLE::LOCALE_SYSTEM_DEFAULT
.
lcid = WIN32OLE.locale
Sets current locale id (lcid).
WIN32OLE.locale = 1033 # set locale English(U.S) obj = WIN32OLE_VARIANT.new("$100,000", WIN32OLE::VARIANT::VT_CY)
Searches through the hash comparing obj with the key using ==
. Returns the key-value pair (two elements array) or nil
if no match is found. See Array#assoc
.
h = {"colors" => ["red", "blue", "green"], "letters" => ["a", "b", "c" ]} h.assoc("letters") #=> ["letters", ["a", "b", "c"]] h.assoc("foo") #=> nil
Searches through the hash comparing obj with the value using ==
. Returns the first key-value pair (two-element array) that matches. See also Array#rassoc
.
a = {1=> "one", 2 => "two", 3 => "three", "ii" => "two"} a.rassoc("two") #=> [2, "two"] a.rassoc("four") #=> nil
Returns an Array of the name and value of the environment variable with name
or nil
if the name cannot be found.
Returns an Array of the name and value of the environment variable with value
or nil
if the value cannot be found.
Writes the given object(s) to ios. Returns nil
.
The stream must be opened for writing. Each given object that isn’t a string will be converted by calling its to_s
method. When called without arguments, prints the contents of $_
.
If the output field separator ($,
) is not nil
, it is inserted between objects. If the output record separator ($\
) is not nil
, it is appended to the output.
$stdout.print("This is ", 100, " percent.\n")
produces:
This is 100 percent.
Formats and writes to ios, converting parameters under control of the format string. See Kernel#sprintf
for details.
Print an argument or list of arguments to the default output stream
cgi = CGI.new cgi.print # default: cgi.print == $DEFAULT_OUTPUT.print
Returns true if the ipaddr is a private address. IPv4 addresses in 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 as defined in RFC 1918 and IPv6 Unique Local Addresses in fc00::/7 as defined in RFC 4193 are considered private.
Returns the prefix length in bits for the ipaddr.
Sets the prefix length in bits
Returns true
iff the current severity level allows for the printing of ERROR
messages.
Creates a matrix where rows
is an array of arrays, each of which is a row of the matrix. If the optional argument copy
is false, use the given arrays as the internal structure of the matrix without copying.
Matrix.rows([[25, 93], [-1, 66]]) => 25 93 -1 66