End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
BasicSocket#getpeername
basicsocket.getpeername => sockaddr
ext/socket/basicsocket.c
View on GitHub ↗
static VALUE
bsock_getpeername(VALUE sock)
{
union_sockaddr buf;
socklen_t len = (socklen_t)sizeof buf;
socklen_t len0 = len;
rb_io_t *fptr;
GetOpenFile(sock, fptr);
if (getpeername(fptr->fd, &buf.addr, &len) < 0)
rb_sys_fail("getpeername(2)");
if (len0 < len) len = len0;
return rb_str_new((char*)&buf, len);
}
Returns the remote address of the socket as a sockaddr string.
Example
TCPServer.open("127.0.0.1", 1440) {|serv| c = TCPSocket.new("127.0.0.1", 1440) s = serv.accept p s.getpeername #=> "\x02\x00\x82u\x7F\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00" }
If Addrinfo object is preferred over the binary string, use BasicSocket#remote_address.
See also
#getpeereid
#getsockname
#do_not_reverse_lookup=
#getsockopt
also in
4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1