This method is called when the parser found syntax error.
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.
yield socket and client address for each a connection accepted via given sockets.
The arguments are a list of sockets. The individual argument should be a socket or an array of sockets.
This method yields the block sequentially. It means that the next connection is not accepted until the block returns. So concurrent mechanism, thread for example, should be used to service multiple clients at a time.
Requests a connection to be made on the given remote_sockaddr
after O_NONBLOCK is set for the underlying file descriptor. Returns 0 if successful, otherwise an exception is raised.
# +remote_sockaddr+ - the +struct+ sockaddr contained in a string or Addrinfo object
# Pull down Google's web page require 'socket' include Socket::Constants socket = Socket.new(AF_INET, SOCK_STREAM, 0) sockaddr = Socket.sockaddr_in(80, 'www.google.com') begin # emulate blocking connect socket.connect_nonblock(sockaddr) rescue IO::WaitWritable IO.select(nil, [socket]) # wait 3-way handshake completion begin socket.connect_nonblock(sockaddr) # check connection failure rescue Errno::EISCONN end end socket.write("GET / HTTP/1.0\r\n\r\n") results = socket.read
Refer to Socket#connect
for the exceptions that may be thrown if the call to connect_nonblock fails.
Socket#connect_nonblock
may raise any error corresponding to connect(2) failure, including Errno::EINPROGRESS.
If the exception is Errno::EINPROGRESS, it is extended by IO::WaitWritable
. So IO::WaitWritable
can be used to rescue the exceptions for retrying connect_nonblock.
By specifying ‘exception: false`, the options hash allows you to indicate that connect_nonblock
should not raise an IO::WaitWritable
exception, but return the symbol :wait_writable instead.
# Socket#connect
Disallows further read using shutdown system call.
s1, s2 = UNIXSocket.pair s1.close_read s2.puts #=> Broken pipe (Errno::EPIPE)
Disallows further write using shutdown system call.
UNIXSocket.pair {|s1, s2| s1.print "ping" s1.close_write p s2.read #=> "ping" s2.print "pong" s2.close p s1.read #=> "pong" }
Returns an Addrinfo
object for local address obtained by getsockname.
Note that addrinfo.protocol is filled by 0.
TCPSocket.open("www.ruby-lang.org", 80) {|s| p s.local_address #=> #<Addrinfo: 192.168.0.129:36873 TCP> } TCPServer.open("127.0.0.1", 1512) {|serv| p serv.local_address #=> #<Addrinfo: 127.0.0.1:1512 TCP> }
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.
creates a new Socket
connected to the address of local_addrinfo
.
If local_addrinfo is nil, the address of the socket is not bound.
The timeout specify the seconds for timeout. Errno::ETIMEDOUT is raised when timeout occur.
If a block is given the created socket is yielded for each address.
Returns true for IPv4 loopback address (127.0.0.0/8). It returns false otherwise.
Returns true for IPv6 loopback address (::1). It returns false otherwise.
Returns true for IPv6 link local address (ff80::/10). It returns false otherwise.
Returns true for IPv6 site local address (ffc0::/10). It returns false otherwise.
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.
Closes the read end of a StringIO
. Will raise an IOError
if the strio is not readable.
Closes the write end of a StringIO
. Will raise an IOError
if the strio is not writeable.
Returns true
if strio is not readable, false
otherwise.
Returns true
if strio is not writable, false
otherwise.
Returns the Encoding
object that represents the encoding of the file. If strio is write mode and no encoding is specified, returns nil
.