Method: TCPSocket.gethostbyname

Defined in:
tcpsocket.c

.gethostbyname(hostname) ⇒ Array

Use Addrinfo.getaddrinfo instead. This method is deprecated for the following reasons:

  • The 3rd element of the result is the address family of the first address. The address families of the rest of the addresses are not returned.

  • gethostbyname() may take a long time and it may block other threads. (GVL cannot be released since gethostbyname() is not thread safe.)

  • This method uses gethostbyname() function already removed from POSIX.

This method lookups host information by hostname.

TCPSocket.gethostbyname("localhost")
#=> ["localhost", ["hal"], 2, "127.0.0.1"]

Returns:

  • (Array)


107
108
109
110
111
112
113
114
# File 'tcpsocket.c', line 107

static VALUE
tcp_s_gethostbyname(VALUE obj, VALUE host)
{
    rb_warn("TCPSocket.gethostbyname is deprecated; use Addrinfo.getaddrinfo instead.");
    struct rb_addrinfo *res =
        rsock_addrinfo(host, Qnil, AF_UNSPEC, SOCK_STREAM, AI_CANONNAME);
    return rsock_make_hostent(host, res, tcp_sockaddr);
}