Method: IPSocket#addr
- Defined in:
- ipsocket.c
#addr([reverse_lookup]) ⇒ Array
Returns the local address as an array which contains address_family, port, hostname and numeric_address.
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.addr #=> ["AF_INET", 49429, "hal", "192.168.0.128"]
p sock.addr(true) #=> ["AF_INET", 49429, "hal", "192.168.0.128"]
p sock.addr(false) #=> ["AF_INET", 49429, "192.168.0.128", "192.168.0.128"]
p sock.addr(:hostname) #=> ["AF_INET", 49429, "hal", "192.168.0.128"]
p sock.addr(:numeric) #=> ["AF_INET", 49429, "192.168.0.128", "192.168.0.128"]
}
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'ipsocket.c', line 274 static VALUE ip_addr(int argc, VALUE *argv, VALUE sock) { rb_io_t *fptr; union_sockaddr addr; socklen_t len = (socklen_t)sizeof addr; int norevlookup; GetOpenFile(sock, fptr); if (argc < 1 || !rsock_revlookup_flag(argv[0], &norevlookup)) norevlookup = fptr->mode & FMODE_NOREVLOOKUP; if (getsockname(fptr->fd, &addr.addr, &len) < 0) rb_sys_fail("getsockname(2)"); return rsock_ipaddr(&addr.addr, len, norevlookup); } |