instance method
Addrinfo#bind
() -> ::Socket
() { (::Socket) -> void } -> void
bind() { |sock| ... }
ext/socket/lib/socket.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/ext/socket/lib/socket.rb, line 178
def bind
sock = Socket.new(self.pfamily, self.socktype, self.protocol)
begin
sock.ipv6only! if self.ipv6?
sock.setsockopt(:SOCKET, :REUSEADDR, 1)
sock.bind(self)
rescue Exception
sock.close
raise
end
if block_given?
begin
yield sock
ensure
sock.close
end
else
sock
end
end
creates a socket bound to self.
If a block is given, it is called with the socket and the value of the block is returned. The socket is returned otherwise.
Example
Addrinfo.udp("0.0.0.0", 9981).bind {|s| s.local_address.connect {|s| s.send "hello", 0 } p s.recv(10) #=> "hello" }