Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Addrinfo#connect_internal
connect_internal(local_addrinfo, timeout=nil) { |socket| ... }
ext/socket/lib/socket.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.1.0-preview1/ext/socket/lib/socket.rb, line 59
def connect_internal(local_addrinfo, timeout=nil) # :yields: socket
sock = Socket.new(self.pfamily, self.socktype, self.protocol)
begin
sock.ipv6only! if self.ipv6?
sock.bind local_addrinfo if local_addrinfo
if timeout
case sock.connect_nonblock(self, exception: false)
when 0 # success or EISCONN, other errors raise
break
when :wait_writable
sock.wait(CONNECT_EVENTS, timeout) or
raise Errno::ETIMEDOUT, "user specified timeout for #{self.ip_address}:#{self.ip_port}"
# Check SO_ERROR instead of relying on the connect_nonblock retry;
# some kernels (e.g. Darwin 27) answer the retry connect(2) with
# EISCONN even when the connection has failed. [Bug #22223]
err = sock.getsockopt(Socket::SOL_SOCKET, Socket::SO_ERROR).int
unless err.zero?
raise SystemCallError.new("connect(2) for #{self.ip_address}:#{self.ip_port}", err)
end
end while true
else
sock.connect(self)
end
rescue Exception
sock.close
raise
end
if block_given?
begin
yield sock
ensure
sock.close
end
else
sock
end
end
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.
See also
#connect_from
#connect_to
#connect
#family_addrinfo
also in
4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0