End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
TCPServer.new
TCPServer.new([hostname,] port) => tcpserver
ext/socket/tcpserver.c
View on GitHub ↗
static VALUE
tcp_svr_init(int argc, VALUE *argv, VALUE sock)
{
VALUE hostname, port;
rb_scan_args(argc, argv, "011", &hostname, &port);
return rsock_init_inetsock(sock, hostname, port, Qnil, Qnil, INET_SERVER, Qnil, Qnil);
}
Creates a new server socket bound to port.
If hostname is given, the socket is bound to it.
Example
serv = TCPServer.new("127.0.0.1", 28561) s = serv.accept s.puts Time.now s.close
Internally, TCPServer.new calls getaddrinfo() function to obtain addresses. If getaddrinfo() returns multiple addresses, TCPServer.new tries to create a server socket for each address and returns first one that is successful.