Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
UDPSocket#bind
(::String host, ::Integer port) -> void
udpsocket.bind(host, port) #=> 0
ext/socket/udpsocket.c
View on GitHub ↗
static VALUE
udp_bind(VALUE self, VALUE host, VALUE port)
{
struct udp_arg arg = {.io = self};
arg.res = rsock_addrinfo(host, port, rsock_fd_family(rb_io_descriptor(self)), SOCK_DGRAM, 0);
VALUE result = rb_ensure(udp_bind_internal, (VALUE)&arg, rsock_freeaddrinfo, (VALUE)arg.res);
if (!result) {
rsock_sys_fail_host_port("bind(2)", host, port);
}
return INT2FIX(0);
}
Binds udpsocket to host:port.
Example
u1 = UDPSocket.new u1.bind("127.0.0.1", 4913) u1.send "message-to-self", 0, "127.0.0.1", 4913 p u1.recvfrom(10) #=> ["message-to", ["AF_INET", 4913, "localhost", "127.0.0.1"]]