returns the canonical name as an string.
nil is returned if no canonical name.
The canonical name is set by Addrinfo.getaddrinfo
when AI_CANONNAME is specified.
list = Addrinfo.getaddrinfo("www.ruby-lang.org", 80, :INET, :STREAM, nil, Socket::AI_CANONNAME) p list[0] #=> #<Addrinfo: 221.186.184.68:80 TCP carbon.ruby-lang.org (www.ruby-lang.org)> p list[0].canonname #=> "carbon.ruby-lang.org"
Connects udpsocket to host:port.
This makes possible to send without destination address.
u1 = UDPSocket.new u1.bind("127.0.0.1", 4913) u2 = UDPSocket.new u2.connect("127.0.0.1", 4913) u2.send "uuuu", 0 p u1.recvfrom(10) #=> ["uuuu", ["AF_INET", 33230, "localhost", "127.0.0.1"]]
Sends mesg via udpsocket.
flags should be a bitwise OR of Socket::MSG_* constants.
u1 = UDPSocket.new u1.bind("127.0.0.1", 4913) u2 = UDPSocket.new u2.send "hi", 0, "127.0.0.1", 4913 mesg, addr = u1.recvfrom(10) u1.send mesg, 0, addr[3], addr[1] p u2.recv(100) #=> "hi"
Accepts an incoming connection. It returns a new TCPSocket
object.
TCPServer.open("127.0.0.1", 14641) {|serv| s = serv.accept s.puts Time.now s.close }
Returns a file descriptor of a accepted connection.
TCPServer.open("127.0.0.1", 28561) {|serv| fd = serv.sysaccept s = IO.for_fd(fd) s.puts Time.now s.close }
Accepts an incoming connection. It returns a new UNIXSocket
object.
UNIXServer.open("/tmp/sock") {|serv| UNIXSocket.open("/tmp/sock") {|c| s = serv.accept s.puts "hi" s.close p c.read #=> "hi\n" } }
Accepts a new connection. It returns the new file descriptor which is an integer.
UNIXServer.open("/tmp/sock") {|serv| UNIXSocket.open("/tmp/sock") {|c| fd = serv.sysaccept s = IO.new(fd) s.puts "hi" s.close p c.read #=> "hi\n" } }
Closes the SOCKS connection.
Returns the path of the local address of unixsocket.
s = UNIXServer.new("/tmp/sock") p s.path #=> "/tmp/sock"
Returns the remote address as an array which contains address_family and unix_path.
Example
serv = UNIXServer.new("/tmp/sock") c = UNIXSocket.new("/tmp/sock") p c.peeraddr #=> ["AF_UNIX", "/tmp/sock"]
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"
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"
Equivalent to StringIO.new
except that when it is called with a block, it yields with the new instance and closes it, and returns the result which returned from the block.
Reinitializes the stream with the given other_StrIO or string and mode (see StringIO#new).
Closes a StringIO
. The stream is unavailable for any further data operations; an IOError
is raised if such an attempt is made.
Returns true
if the stream is completely closed, false
otherwise.
Seeks to a given offset amount in the stream according to the value of whence (see IO#seek
).
Reset the scan pointer (index 0) and clear matching data.
Sets the scan pointer to the end of the string and clear matching data.
Appends str
to the string being scanned. This method does not affect scan pointer.
s = StringScanner.new("Fri Dec 12 1975 14:39") s.scan(/Fri /) s << " +1000 GMT" s.string # -> "Fri Dec 12 1975 14:39 +1000 GMT" s.scan(/Dec/) # -> "Dec"
Returns the character position of the scan pointer. In the ‘reset’ position, this value is zero. In the ‘terminated’ position (i.e. the string is exhausted), this value is the size of the string.
In short, it’s a 0-based index into the string.
s = StringScanner.new("abcädeföghi") s.charpos # -> 0 s.scan_until(/ä/) # -> "abcä" s.pos # -> 5 s.charpos # -> 4
Returns the byte position of the scan pointer. In the ‘reset’ position, this value is zero. In the ‘terminated’ position (i.e. the string is exhausted), this value is the bytesize of the string.
In short, it’s a 0-based index into bytes of the string.
s = StringScanner.new('test string') s.pos # -> 0 s.scan_until /str/ # -> "test str" s.pos # -> 8 s.terminate # -> #<StringScanner fin> s.pos # -> 11
Sets the byte position of the scan pointer.
s = StringScanner.new('test string') s.pos = 7 # -> 7 s.rest # -> "ring"