Returns a hash, that will be turned into a JSON
object and represent this object.
Stores class name (Range
) with JSON
array of arguments a
which include first
(integer), last
(integer), and exclude_end?
(boolean) as JSON
string.
Deserializes JSON
string by constructing new Regexp
object with source s
(Regexp
or String) and options o
serialized by to_json
Returns a hash, that will be turned into a JSON
object and represent this object.
Try to convert obj into a Regexp
, using to_regexp method. Returns converted regexp or nil if obj cannot be converted for any reason.
Regexp.try_convert(/re/) #=> /re/ Regexp.try_convert("re") #=> nil o = Object.new Regexp.try_convert(o) #=> nil def o.to_regexp() /foo/ end Regexp.try_convert(o) #=> /foo/
Returns a hash, that will be turned into a JSON
object and represent this object.
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_addrinfo, contains protocol-specific address information of 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.
maxlen
- the maximum number of bytes to receive from the socket
flags
- zero or more of the MSG_
options
outbuf
- destination String buffer
opts
- keyword hash, supporting ‘exception: false`
# 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_addrinfo = socket.accept begin # emulate blocking recvfrom pair = client.recvfrom_nonblock(20) rescue IO::WaitReadable 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::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 ‘exception: false`, the options hash allows you to indicate that accept_nonblock
should not raise an IO::WaitReadable
exception, but return the symbol :wait_readable instead.
Accepts an incoming connection using accept(2) after O_NONBLOCK is set for the underlying file descriptor. It returns an array containing the accepted socket for the incoming connection, client_socket, and an Addrinfo
, client_addrinfo.
# In one script, 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) begin # emulate blocking accept client_socket, client_addrinfo = socket.accept_nonblock rescue IO::WaitReadable, Errno::EINTR IO.select([socket]) retry end puts "The client said, '#{client_socket.readline.chomp}'" client_socket.puts "Hello from script one!" socket.close # In another script, 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 "Hello from script 2." puts "The server said, '#{socket.readline.chomp}'" socket.close
Refer to Socket#accept
for the exceptions that may be thrown if the call to accept_nonblock fails.
Socket#accept_nonblock
may raise any error corresponding to accept(2) failure, including Errno::EWOULDBLOCK.
If the exception is Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::ECONNABORTED
or Errno::EPROTO
, it is extended by IO::WaitReadable
. So IO::WaitReadable
can be used to rescue the exceptions for retrying accept_nonblock.
By specifying ‘exception: false`, the options hash allows you to indicate that accept_nonblock
should not raise an IO::WaitReadable
exception, but return the symbol :wait_readable instead.
Packs path as an AF_UNIX sockaddr string.
Socket.sockaddr_un("/tmp/sock") #=> "\x01\x00/tmp/sock\x00\x00..."
sendmsg_nonblock
sends a message using sendmsg(2) system call in non-blocking manner.
It is similar to BasicSocket#sendmsg
but the non-blocking flag is set before the system call and it doesn’t retry the system call.
By specifying ‘exception: false`, the opts hash allows you to indicate that sendmsg_nonblock
should not raise an IO::WaitWritable
exception, but return the symbol :wait_writable instead.
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 result, mesg, is the data received.
When recvfrom(2) returns 0, Socket#recv_nonblock
returns an empty string as data. The meaning depends on the socket: EOF on TCP, empty packet on UDP, etc.
maxlen
- the number of bytes to receive from the socket
flags
- zero or more of the MSG_
options
options
- keyword hash, supporting ‘exception: false`
serv = TCPServer.new("127.0.0.1", 0) af, port, host, addr = serv.addr c = TCPSocket.new(addr, port) s = serv.accept c.send "aaa", 0 begin # emulate blocking recv. p s.recv_nonblock(10) #=> "aaa" rescue IO::WaitReadable IO.select([s]) retry end
Refer to Socket#recvfrom
for the exceptions that may be thrown if the call to recv_nonblock fails.
BasicSocket#recv_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 recv_nonblock.
By specifying ‘exception: false`, the options hash allows you to indicate that recv_nonblock
should not raise an IO::WaitWritable
exception, but return the symbol :wait_writable instead.
recvmsg receives a message using recvmsg(2) system call in non-blocking manner.
It is similar to BasicSocket#recvmsg
but non-blocking flag is set before the system call and it doesn’t retry the system call.
By specifying ‘exception: false`, the opts hash allows you to indicate that recvmsg_nonblock
should not raise an IO::WaitWritable
exception, but return the symbol :wait_writable instead.
returns a string which shows the sockaddr in addrinfo with human-readable form.
Addrinfo.tcp("localhost", 80).inspect_sockaddr #=> "127.0.0.1:80" Addrinfo.tcp("ip6-localhost", 80).inspect_sockaddr #=> "[::1]:80" Addrinfo.unix("/tmp/sock").inspect_sockaddr #=> "/tmp/sock"
Returns the IP address and port number as 2-element array.
Addrinfo.tcp("127.0.0.1", 80).ip_unpack #=> ["127.0.0.1", 80] Addrinfo.tcp("::1", 80).ip_unpack #=> ["::1", 80]
Returns true for IPv6 unspecified address (::). It returns false otherwise.
Returns the socket path as a string.
Addrinfo.unix("/tmp/sock").unix_path #=> "/tmp/sock"
Receives up to maxlen bytes from udpsocket
using recvfrom(2) after O_NONBLOCK is set for the underlying file descriptor. If maxlen is omitted, its default value is 65536. 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("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([s2]) 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 ‘exception: false`, the options hash allows you to indicate that recvmsg_nonblock should not raise an IO::WaitWritable
exception, but return the symbol :wait_writable instead.
Accepts an incoming connection using accept(2) after O_NONBLOCK is set for the underlying file descriptor. It returns an accepted TCPSocket
for the incoming connection.
require 'socket' serv = TCPServer.new(2202) begin # emulate blocking accept sock = serv.accept_nonblock rescue IO::WaitReadable, Errno::EINTR IO.select([serv]) retry end # sock is an accepted socket.
Refer to Socket#accept
for the exceptions that may be thrown if the call to TCPServer#accept_nonblock
fails.
TCPServer#accept_nonblock
may raise any error corresponding to accept(2) failure, including Errno::EWOULDBLOCK.
If the exception is Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::ECONNABORTED
, Errno::EPROTO
, it is extended by IO::WaitReadable
. So IO::WaitReadable
can be used to rescue the exceptions for retrying accept_nonblock.
By specifying ‘exception: false`, the options hash allows you to indicate that accept_nonblock
should not raise an IO::WaitReadable
exception, but return the symbol :wait_readable instead.
Accepts an incoming connection using accept(2) after O_NONBLOCK is set for the underlying file descriptor. It returns an accepted UNIXSocket
for the incoming connection.
require 'socket' serv = UNIXServer.new("/tmp/sock") begin # emulate blocking accept sock = serv.accept_nonblock rescue IO::WaitReadable, Errno::EINTR IO.select([serv]) retry end # sock is an accepted socket.
Refer to Socket#accept
for the exceptions that may be thrown if the call to UNIXServer#accept_nonblock
fails.
UNIXServer#accept_nonblock
may raise any error corresponding to accept(2) failure, including Errno::EWOULDBLOCK.
If the exception is Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::ECONNABORTED
or Errno::EPROTO
, it is extended by IO::WaitReadable
. So IO::WaitReadable
can be used to rescue the exceptions for retrying accept_nonblock.
By specifying ‘exception: false`, the options hash allows you to indicate that accept_nonblock
should not raise an IO::WaitReadable
exception, but return the symbol :wait_readable instead.
Sends io as file descriptor passing.
s1, s2 = UNIXSocket.pair s1.send_io STDOUT stdout = s2.recv_io p STDOUT.fileno #=> 1 p stdout.fileno #=> 6 stdout.puts "hello" # outputs "hello\n" to standard output.
io may be any kind of IO
object or integer file descriptor.
Example
UNIXServer.open("/tmp/sock") {|serv| UNIXSocket.open("/tmp/sock") {|c| s = serv.accept c.send_io STDOUT stdout = s.recv_io p STDOUT.fileno #=> 1 p stdout.fileno #=> 7 stdout.puts "hello" # outputs "hello\n" to standard output. } }
klass will determine the class of io returned (using the IO.for_fd
singleton method or similar). If klass is nil
, an integer file descriptor is returned.
mode is the same as the argument passed to IO.for_fd