enable the socket option IPV6_V6ONLY if IPV6_V6ONLY is available.
Listens for connections, using the specified int
as the backlog. A call to listen only applies if the socket
is of type SOCK_STREAM or SOCK_SEQPACKET.
backlog
- the maximum length of the queue for pending connections.
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 )
require 'socket' include Socket::Constants socket = Socket.new( AF_INET, SOCK_STREAM, 0 ) socket.listen( 1 )
On unix based systems the above will work because a new sockaddr
struct is created on the address ADDR_ANY, for an arbitrary port number as handed off by the kernel. It will not work on Windows, because Windows requires that the socket
is bound by calling bind before it can listen.
If the backlog amount exceeds the implementation-dependent maximum queue length, the implementation’s maximum queue length will be used.
On unix-based based systems the following system exceptions may be raised if the call to listen fails:
Errno::EBADF - the socket argument is not a valid file descriptor
Errno::EDESTADDRREQ - the socket is not bound to a local address, and the protocol does not support listening on an unbound socket
Errno::EINVAL - the socket is already connected
Errno::ENOTSOCK - the socket argument does not refer to a socket
Errno::EOPNOTSUPP - the socket protocol does not support listen
Errno::EACCES - the calling process does not have appropriate privileges
Errno::EINVAL - the socket has been shut down
Errno::ENOBUFS - insufficient resources are available in the system to complete the call
On Windows systems the following system exceptions may be raised if the call to listen fails:
Errno::ENETDOWN - the network is down
Errno::EADDRINUSE - the socket’s local address is already in use. This usually occurs during the execution of bind but could be delayed if the call to bind was to a partially wildcard address (involving ADDR_ANY) and if a specific address needs to be committed at the time of the call to listen
Errno::EINPROGRESS - a Windows Sockets 1.1 call is in progress or the service provider is still processing a callback function
Errno::EINVAL - the socket
has not been bound with a call to bind.
Errno::EISCONN - the socket
is already connected
Errno::EMFILE - no more socket descriptors are available
Errno::ENOBUFS - no buffer space is available
Errno::ENOTSOC - socket
is not a socket
Errno::EOPNOTSUPP - the referenced socket
is not a type that supports the listen method
listen manual pages on unix-based systems
listen function in Microsoft’s Winsock functions reference
Returns the hostname.
p Socket.gethostname #=> "hal"
Note that it is not guaranteed to be able to convert to IP address using gethostbyname, getaddrinfo, etc. If you need local IP address, use Socket.ip_address_list
.
Use Addrinfo.getaddrinfo
instead. This method is deprecated for the following reasons:
The 3rd element of the result is the address family of the first address. The address families of the rest of the addresses are not returned.
Uncommon address representation: 4/16-bytes binary string to represent IPv4/IPv6 address.
gethostbyname() may take a long time and it may block other threads. (GVL cannot be released since gethostbyname() is not thread safe.)
This method uses gethostbyname() function already removed from POSIX.
This method obtains the host information for hostname.
p Socket.gethostbyname("hal") #=> ["localhost", ["hal"], 2, "\x7F\x00\x00\x01"]
Use Addrinfo#getnameinfo
instead. This method is deprecated for the following reasons:
Uncommon address representation: 4/16-bytes binary string to represent IPv4/IPv6 address.
gethostbyaddr() may take a long time and it may block other threads. (GVL cannot be released since gethostbyname() is not thread safe.)
This method uses gethostbyname() function already removed from POSIX.
This method obtains the host information for address.
p Socket.gethostbyaddr([221,186,184,68].pack("CCCC")) #=> ["carbon.ruby-lang.org", [], 2, "\xDD\xBA\xB8D"] p Socket.gethostbyaddr([127,0,0,1].pack("CCCC")) ["localhost", [], 2, "\x7F\x00\x00\x01"] p Socket.gethostbyaddr(([0]*15+[1]).pack("C"*16)) #=> ["localhost", ["ip6-localhost", "ip6-loopback"], 10, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"]
Obtains address information for nodename:servname.
Note that Addrinfo.getaddrinfo
provides the same functionality in an object oriented style.
family should be an address family such as: :INET, :INET6, etc.
socktype should be a socket type such as: :STREAM, :DGRAM, :RAW, etc.
protocol should be a protocol defined in the family, and defaults to 0 for the family.
flags should be bitwise OR of Socket::AI_* constants.
Socket.getaddrinfo("www.ruby-lang.org", "http", nil, :STREAM) #=> [["AF_INET", 80, "carbon.ruby-lang.org", "221.186.184.68", 2, 1, 6]] # PF_INET/SOCK_STREAM/IPPROTO_TCP Socket.getaddrinfo("localhost", nil) #=> [["AF_INET", 0, "localhost", "127.0.0.1", 2, 1, 6], # PF_INET/SOCK_STREAM/IPPROTO_TCP # ["AF_INET", 0, "localhost", "127.0.0.1", 2, 2, 17], # PF_INET/SOCK_DGRAM/IPPROTO_UDP # ["AF_INET", 0, "localhost", "127.0.0.1", 2, 3, 0]] # PF_INET/SOCK_RAW/IPPROTO_IP
reverse_lookup directs the form of the third element, and has to be one of below. If reverse_lookup is omitted, the default value is nil
.
+true+, +:hostname+: hostname is obtained from numeric address using reverse lookup, which may take a time. +false+, +:numeric+: hostname is the same as numeric address. +nil+: obey to the current +do_not_reverse_lookup+ flag.
If Addrinfo
object is preferred, use Addrinfo.getaddrinfo
.
creates a listening socket bound to self.
returns a list of addrinfo objects as an array.
This method converts nodename (hostname) and service (port) to addrinfo. Since the conversion is not unique, the result is a list of addrinfo objects.
nodename or service can be nil if no conversion intended.
family, socktype and protocol are hint for preferred protocol. If the result will be used for a socket with SOCK_STREAM, SOCK_STREAM should be specified as socktype. If so, Addrinfo.getaddrinfo
returns addrinfo list appropriate for SOCK_STREAM. If they are omitted or nil is given, the result is not restricted.
Similarly, PF_INET6 as family restricts for IPv6.
flags should be bitwise OR of Socket::AI_??? constants such as follows. Note that the exact list of the constants depends on OS.
AI_PASSIVE Get address to use with bind() AI_CANONNAME Fill in the canonical name AI_NUMERICHOST Prevent host name resolution AI_NUMERICSERV Prevent service name resolution AI_V4MAPPED Accept IPv4-mapped IPv6 addresses AI_ALL Allow all addresses AI_ADDRCONFIG Accept only if any address is assigned
Note that socktype should be specified whenever application knows the usage of the address. Some platform causes an error when socktype is omitted and servname is specified as an integer because some port numbers, 512 for example, are ambiguous without socktype.
Addrinfo.getaddrinfo("www.kame.net", 80, nil, :STREAM) #=> [#<Addrinfo: 203.178.141.194:80 TCP (www.kame.net)>, # #<Addrinfo: [2001:200:dff:fff1:216:3eff:feb1:44d7]:80 TCP (www.kame.net)>]
returns an addrinfo object for IP address.
The port, socktype, protocol of the result is filled by zero. So, it is not appropriate to create a socket.
Addrinfo.ip("localhost") #=> #<Addrinfo: 127.0.0.1 (localhost)>
returns true if addrinfo is IPv4 address. returns false otherwise.
Addrinfo.tcp("127.0.0.1", 80).ipv4? #=> true Addrinfo.tcp("::1", 80).ipv4? #=> false Addrinfo.unix("/tmp/sock").ipv4? #=> false
returns true if addrinfo is IPv6 address. returns false otherwise.
Addrinfo.tcp("127.0.0.1", 80).ipv6? #=> false Addrinfo.tcp("::1", 80).ipv6? #=> true Addrinfo.unix("/tmp/sock").ipv6? #=> false
returns true if addrinfo is internet (IPv4/IPv6) address. returns false otherwise.
Addrinfo.tcp("127.0.0.1", 80).ip? #=> true Addrinfo.tcp("::1", 80).ip? #=> true Addrinfo.unix("/tmp/sock").ip? #=> false
Listens for connections, using the specified int
as the backlog. A call to listen only applies if the socket
is of type SOCK_STREAM or SOCK_SEQPACKET.
backlog
- the maximum length of the queue for pending connections.
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 )
require 'socket' include Socket::Constants socket = Socket.new( AF_INET, SOCK_STREAM, 0 ) socket.listen( 1 )
On unix based systems the above will work because a new sockaddr
struct is created on the address ADDR_ANY, for an arbitrary port number as handed off by the kernel. It will not work on Windows, because Windows requires that the socket
is bound by calling bind before it can listen.
If the backlog amount exceeds the implementation-dependent maximum queue length, the implementation’s maximum queue length will be used.
On unix-based based systems the following system exceptions may be raised if the call to listen fails:
Errno::EBADF - the socket argument is not a valid file descriptor
Errno::EDESTADDRREQ - the socket is not bound to a local address, and the protocol does not support listening on an unbound socket
Errno::EINVAL - the socket is already connected
Errno::ENOTSOCK - the socket argument does not refer to a socket
Errno::EOPNOTSUPP - the socket protocol does not support listen
Errno::EACCES - the calling process does not have appropriate privileges
Errno::EINVAL - the socket has been shut down
Errno::ENOBUFS - insufficient resources are available in the system to complete the call
On Windows systems the following system exceptions may be raised if the call to listen fails:
Errno::ENETDOWN - the network is down
Errno::EADDRINUSE - the socket’s local address is already in use. This usually occurs during the execution of bind but could be delayed if the call to bind was to a partially wildcard address (involving ADDR_ANY) and if a specific address needs to be committed at the time of the call to listen
Errno::EINPROGRESS - a Windows Sockets 1.1 call is in progress or the service provider is still processing a callback function
Errno::EINVAL - the socket
has not been bound with a call to bind.
Errno::EISCONN - the socket
is already connected
Errno::EMFILE - no more socket descriptors are available
Errno::ENOBUFS - no buffer space is available
Errno::ENOTSOC - socket
is not a socket
Errno::EOPNOTSUPP - the referenced socket
is not a type that supports the listen method
listen manual pages on unix-based systems
listen function in Microsoft’s Winsock functions reference
Listens for connections, using the specified int
as the backlog. A call to listen only applies if the socket
is of type SOCK_STREAM or SOCK_SEQPACKET.
backlog
- the maximum length of the queue for pending connections.
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 )
require 'socket' include Socket::Constants socket = Socket.new( AF_INET, SOCK_STREAM, 0 ) socket.listen( 1 )
On unix based systems the above will work because a new sockaddr
struct is created on the address ADDR_ANY, for an arbitrary port number as handed off by the kernel. It will not work on Windows, because Windows requires that the socket
is bound by calling bind before it can listen.
If the backlog amount exceeds the implementation-dependent maximum queue length, the implementation’s maximum queue length will be used.
On unix-based based systems the following system exceptions may be raised if the call to listen fails:
Errno::EBADF - the socket argument is not a valid file descriptor
Errno::EDESTADDRREQ - the socket is not bound to a local address, and the protocol does not support listening on an unbound socket
Errno::EINVAL - the socket is already connected
Errno::ENOTSOCK - the socket argument does not refer to a socket
Errno::EOPNOTSUPP - the socket protocol does not support listen
Errno::EACCES - the calling process does not have appropriate privileges
Errno::EINVAL - the socket has been shut down
Errno::ENOBUFS - insufficient resources are available in the system to complete the call
On Windows systems the following system exceptions may be raised if the call to listen fails:
Errno::ENETDOWN - the network is down
Errno::EADDRINUSE - the socket’s local address is already in use. This usually occurs during the execution of bind but could be delayed if the call to bind was to a partially wildcard address (involving ADDR_ANY) and if a specific address needs to be committed at the time of the call to listen
Errno::EINPROGRESS - a Windows Sockets 1.1 call is in progress or the service provider is still processing a callback function
Errno::EINVAL - the socket
has not been bound with a call to bind.
Errno::EISCONN - the socket
is already connected
Errno::EMFILE - no more socket descriptors are available
Errno::ENOBUFS - no buffer space is available
Errno::ENOTSOC - socket
is not a socket
Errno::EOPNOTSUPP - the referenced socket
is not a type that supports the listen method
listen manual pages on unix-based systems
listen function in Microsoft’s Winsock functions reference
Use Addrinfo.getaddrinfo
instead. This method is deprecated for the following reasons:
The 3rd element of the result is the address family of the first address. The address families of the rest of the addresses are not returned.
gethostbyname() may take a long time and it may block other threads. (GVL cannot be released since gethostbyname() is not thread safe.)
This method uses gethostbyname() function already removed from POSIX.
This method lookups host information by hostname.
TCPSocket.gethostbyname("localhost") #=> ["localhost", ["hal"], 2, "127.0.0.1"]
Appends the given string to the underlying buffer string. The stream must be opened for writing. If the argument is not a string, it will be converted to a string using to_s
. Returns the number of bytes written. See IO#write
.
Truncates the buffer string to at most integer bytes. The stream must be opened for writing.
Attempts to [match] the given pattern
anywhere (at any [position]) n the [target substring]; does not modify the [positions].
If the match succeeds:
Returns a byte offset: the distance in bytes between the current [position] and the end of the matched substring.
Sets all [match values].
scanner = StringScanner.new('foobarbazbatbam') scanner.pos = 6 scanner.exist?(/bat/) # => 6 put_match_values(scanner) # Basic match values: # matched?: true # matched_size: 3 # pre_match: "foobarbaz" # matched : "bat" # post_match: "bam" # Captured match values: # size: 1 # captures: [] # named_captures: {} # values_at: ["bat", nil] # []: # [0]: "bat" # [1]: nil put_situation(scanner) # Situation: # pos: 6 # charpos: 6 # rest: "bazbatbam" # rest_size: 9
If the match fails:
Returns nil
.
Clears all [match values].
scanner.exist?(/nope/) # => nil match_values_cleared?(scanner) # => true
Returns the ‘rest’ of the [stored string] (all after the current [position]), which is the [target substring]:
scanner = StringScanner.new('foobarbaz') scanner.rest # => "foobarbaz" scanner.pos = 3 scanner.rest # => "barbaz" scanner.terminate scanner.rest # => ""
Associates the given value
with the given key
; returns value
.
If the given key
exists, replaces its value with the given value
; the ordering is not affected (see Entry Order):
h = {foo: 0, bar: 1} h[:foo] = 2 # => 2 h.store(:bar, 3) # => 3 h # => {:foo=>2, :bar=>3}
If key
does not exist, adds the key
and value
; the new entry is last in the order (see Entry Order):
h = {foo: 0, bar: 1} h[:baz] = 2 # => 2 h.store(:bat, 3) # => 3 h # => {:foo=>0, :bar=>1, :baz=>2, :bat=>3}