Accepts an incoming connection returning an array containing the (integer) file descriptor for the incoming connection, client_socket_fd, 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.pack_sockaddr_in( 2200, 'localhost' ) socket.bind( sockaddr ) socket.listen( 5 ) client_fd, client_addrinfo = socket.sysaccept client_socket = Socket.for_fd( client_fd ) 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.pack_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 sysaccept fails.
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"
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"
Returns the remote address of the socket as a sockaddr string.
TCPServer.open("127.0.0.1", 1440) {|serv| c = TCPSocket.new("127.0.0.1", 1440) s = serv.accept p s.getpeername #=> "\x02\x00\x82u\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00" }
If Addrinfo
object is preferred over the binary string, use BasicSocket#remote_address
.
Returns the user and group on the peer of the UNIX socket. The result is a two element array which contains the effective uid and the effective gid.
Socket.unix_server_loop("/tmp/sock") {|s| begin euid, egid = s.getpeereid # Check the connected client is myself or not. next if euid != Process.uid # do something about my resource. ensure s.close end }
send mesg via basicsocket.
mesg should be a string.
flags should be a bitwise OR of Socket::MSG_* constants.
dest_sockaddr should be a packed sockaddr string or an addrinfo.
TCPSocket.open("localhost", 80) {|s| s.send "GET / HTTP/1.0\r\n\r\n", 0 p s.read }
sendmsg sends a message using sendmsg(2) system call in blocking manner.
mesg is a string to send.
flags is bitwise OR of MSG_* constants such as Socket::MSG_OOB.
dest_sockaddr is a destination socket address for connection-less socket. It should be a sockaddr such as a result of Socket.sockaddr_in
. An Addrinfo
object can be used too.
controls is a list of ancillary data. The element of controls should be Socket::AncillaryData
or 3-elements array. The 3-element array should contains cmsg_level, cmsg_type and data.
The return value, numbytes_sent is an integer which is the number of bytes sent.
sendmsg can be used to implement send_io as follows:
# use Socket::AncillaryData. ancdata = Socket::AncillaryData.int(:UNIX, :SOCKET, :RIGHTS, io.fileno) sock.sendmsg("a", 0, nil, ancdata) # use 3-element array. ancdata = [:SOCKET, :RIGHTS, [io.fileno].pack("i!")] sock.sendmsg("\0", 0, nil, ancdata)
Returns the remote address as an array which contains address_family, port, hostname and numeric_address. It is defined for connection oriented socket such as TCPSocket
.
If reverse_lookup
is true
or :hostname
, hostname is obtained from numeric_address using reverse lookup. Or if it is false
, or :numeric
, hostname is the same as numeric_address. Or if it is nil
or omitted, obeys to ipsocket.do_not_reverse_lookup
. See Socket.getaddrinfo
also.
TCPSocket.open("www.ruby-lang.org", 80) {|sock| p sock.peeraddr #=> ["AF_INET", 80, "carbon.ruby-lang.org", "221.186.184.68"] p sock.peeraddr(true) #=> ["AF_INET", 80, "carbon.ruby-lang.org", "221.186.184.68"] p sock.peeraddr(false) #=> ["AF_INET", 80, "221.186.184.68", "221.186.184.68"] p sock.peeraddr(:hostname) #=> ["AF_INET", 80, "carbon.ruby-lang.org", "221.186.184.68"] p sock.peeraddr(:numeric) #=> ["AF_INET", 80, "221.186.184.68", "221.186.184.68"] }
creates a socket connected to the address of self.
The optional argument opts is options represented by a hash. opts may have following options:
specify the timeout in seconds.
If a block is given, it is called with the socket and the value of the block is returned. The socket is returned otherwise.
Addrinfo.tcp("www.ruby-lang.org", 80).connect {|s| s.print "GET / HTTP/1.0\r\nHost: www.ruby-lang.org\r\n\r\n" puts s.read }
returns the canonical name as a 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.
type 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.
type 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"
Note that mode
defaults to 'r'
if string
is frozen.
Creates a new StringIO instance formed from string
and mode
; see Access Modes.
With no block, returns the new instance:
strio = StringIO.open # => #<StringIO>
With a block, calls the block with the new instance and returns the block’s value; closes the instance on block exit.
StringIO.open {|strio| p strio } # => #<StringIO>
Related: StringIO.new
.
Reinitializes the stream with the given other
(string or StringIO
) and mode
; see IO.new
:
StringIO.open('foo') do |strio| p strio.string strio.reopen('bar') p strio.string other_strio = StringIO.new('baz') strio.reopen(other_strio) p strio.string other_strio.close end
Output:
"foo" "bar" "baz"
Closes self
for both reading and writing.
Raises IOError
if reading or writing is attempted.
Related: StringIO#close_read
, StringIO#close_write
.
Returns true
if self
is closed for both reading and writing, false
otherwise.