End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
BasicSocket#local_address
bsock.local_address => addrinfo
ext/socket/basicsocket.c
View on GitHub ↗
static VALUE
bsock_local_address(VALUE sock)
{
union_sockaddr buf;
socklen_t len = (socklen_t)sizeof buf;
socklen_t len0 = len;
rb_io_t *fptr;
GetOpenFile(sock, fptr);
if (getsockname(fptr->fd, &buf.addr, &len) < 0)
rb_sys_fail("getsockname(2)");
if (len0 < len) len = len0;
return rsock_fd_socket_addrinfo(fptr->fd, &buf.addr, len);
}
Returns an Addrinfo object for local address obtained by getsockname.
Note that addrinfo.protocol is filled by 0.
Example
TCPSocket.open("www.ruby-lang.org", 80) {|s| p s.local_address #=> #<Addrinfo: 192.168.0.129:36873 TCP> } TCPServer.open("127.0.0.1", 1512) {|serv| p serv.local_address #=> #<Addrinfo: 127.0.0.1:1512 TCP> }