Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method

TCPServer.new

(?::String host, ::Integer port) -> ::TCPServer
TCPServer.new([hostname,] port) => tcpserver

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.

See also #accept #accept_nonblock also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0