struct TCPServer
Overview
A Transmission Control Protocol (TCP/IP) server.
Usage example:
require "socket/tcp_server"
def handle_client(client)
message = client.gets
client.puts message
end
TCPServer.open("localhost", 1234) do |server|
while client = server.accept?
spawn handle_client(client)
end
end
Options:
- backlog to specify how many pending connections are allowed.
- reuse_port to enable multiple processes to bind to the same port (
SO_REUSEPORT). - reuse_address to enable multiple processes to bind to the same address (
SO_REUSEADDR). - dns_timeout to specify the timeout for DNS lookups when binding to a hostname.
Included Modules
Defined in:
socket/tcp_server.crConstructors
-
.new(host : String, port : Int, *, backlog : Int32 = Socket::SOMAXCONN, dns_timeout : Time::Span? = nil, reuse_port : Bool = false, reuse_address : Bool = true) : TCPServer
Creates a
TCPServerlistening on port on all interfaces specified by host. -
.new(raw : Socket::Raw)
Creates a
TCPServerfrom a raw socket. -
.new(address : Socket::IPAddress, *, backlog : Int32 = Socket::SOMAXCONN, reuse_port : Bool = false, reuse_address : Bool = true) : TCPServer
Creates a new
TCPServerlistening on address. -
.new(port : Int, *, backlog : Int32 = Socket::SOMAXCONN, reuse_port : Bool = false, reuse_address : Bool = true) : TCPServer
Creates a new
TCPServer, listening on port on all local interfaces (::).
Class Method Summary
-
.open(host : String, port : Int, *, backlog : Int32 = Socket::SOMAXCONN, dns_timeout : Time::Span? = nil, reuse_port : Bool = false, reuse_address : Bool = true, &block)
Creates a new
TCPServerlistenting on host and port, and yields it to the block. -
.open(address : Socket::IPAddress, *, backlog : Int32 = Socket::SOMAXCONN, reuse_port : Bool = false, reuse_address : Bool = true, &block)
Creates a new
TCPServerlistening on address, and yields it to the block. -
.open(port : Int, *, backlog : Int32 = Socket::SOMAXCONN, reuse_port : Bool = false, reuse_address : Bool = true, &block)
Creates a new
TCPServer, listening on all interfaces on port, and yields it to the block.
Instance Method Summary
-
#accept : TCPSocket
Accepts an incoming connection and returns the client
TCPSocket. -
#accept? : TCPSocket?
Accepts an incoming connection.
-
#close : Nil
Closes this socket.
-
#closed? : Bool
Returns
trueif this socket is closed. - #keepalive=(value : Bool) : Bool
- #keepalive? : Bool
-
#local_address : Socket::IPAddress
Returns the
Socket::IPAddressthis server listens on. -
#local_address? : Socket::IPAddress?
Returns the
Socket::IPAddressthis server listens on, ornilif the socket is closed. -
#raw : Socket::Raw
Returns the raw socket wrapped by this TCP server.
-
#recv_buffer_size : Int32
Returns the receive buffer size for this socket.
-
#recv_buffer_size=(value : Int32) : Int32
Sets the receive buffer size for this socket.
-
#reuse_address? : Bool
Returns
trueif this socket has been configured to reuse the address (seeSO_REUSEADDR). -
#reuse_port? : Bool
Returns
trueif this socket has been configured to reuse the port (seeSO_REUSEPORT). -
#send_buffer_size : Int32
Returns the send buffer size for this socket.
-
#send_buffer_size=(value : Int32) : Int32
Sets the send buffer size for this socket.
-
#sync=(value : Bool) : Bool
Sets the sync flag on this socket.
-
#sync? : Bool
Returns the sync flag on this socket.
-
#tcp_keepalive_count : Int32
Returns the number of probes sent, without response before dropping the connection.
-
#tcp_keepalive_count=(value : Int32) : Int32
Sets the number of probes sent, without response before dropping the connection.
-
#tcp_keepalive_idle : Int32
Returns the amount of time (in seconds) the connection must be idle before sending keepalive probes.
-
#tcp_keepalive_idle=(value : Int32) : Int32
Sets the amount of time (in seconds) the connection must be idle before sending keepalive probes.
-
#tcp_keepalive_interval : Int32
Returns the amount of time (in seconds) between keepalive probes.
-
#tcp_keepalive_interval=(value : Int32) : Int32
Sets the amount of time (in seconds) between keepalive probes.
-
#tcp_nodelay=(value : Bool) : Bool
Disable the Nagle algorithm when set to
true, otherwise enables it. -
#tcp_nodelay? : Bool
Returns
trueif the Nable algorithm is disabled.
Instance methods inherited from module Socket::Server
accept : IOaccept(&block) accept, accept? : IO?
accept?(&block) accept?
Instance methods inherited from struct Struct
==(other) : Bool
==,
hash(hasher)
hash,
inspect(io : IO) : Nil
inspect,
pretty_print(pp) : Nil
pretty_print,
to_s(io)
to_s
Instance methods inherited from struct Value
==(other : JSON::Any)==(other : YAML::Any)
==(other) ==, dup dup
Instance methods inherited from class Object
!=(other)
!=,
!~(other)
!~,
==(other)
==,
===(other : JSON::Any)===(other : YAML::Any)
===(other) ===, =~(other) =~, class class, dup dup, hash(hasher)
hash hash, inspect(io : IO)
inspect inspect, itself itself, not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, tap(&block) tap, to_json(io : IO)
to_json to_json, to_pretty_json(indent : String = " ")
to_pretty_json(io : IO, indent : String = " ") to_pretty_json, to_s
to_s(io : IO) to_s, to_yaml(io : IO)
to_yaml to_yaml, try(&block) try, unsafe_as(type : T.class) forall T unsafe_as
Constructor methods inherited from class Object
from_json(string_or_io, root : String) : selffrom_json(string_or_io) : self from_json, from_yaml(string_or_io : String | IO) : self from_yaml
Constructor Detail
Creates a TCPServer listening on port on all interfaces specified by host.
host can either be an IP address or a hostname.
Creates a new TCPServer listening on address.
Creates a new TCPServer, listening on port on all local interfaces (::).
Class Method Detail
Creates a new TCPServer listenting on host and port, and yields it to the block.
Eventually closes the server socket when the block returns.
Returns the value of the block.
Creates a new TCPServer listening on address, and yields it to the block.
Eventually closes the server socket when the block returns.
Returns the value of the block.
Creates a new TCPServer, listening on all interfaces on port, and yields it to the
block.
Eventually closes the server socket when the block returns.
Returns the value of the block.
Instance Method Detail
Accepts an incoming connection and returns the client TCPSocket.
require "socket/tcp_server"
TCPServer.open(2022) do |server|
loop do
socket = server.accept
# handle the client in a fiber
spawn handle_connection(socket)
end
end
Raises if the server is closed after invoking this method.
Accepts an incoming connection.
Returns the client TCPSocket or nil if the server is closed after invoking
this method.
require "socket/tcp_server"
TCPServer.open(2022) do |server|
loop do
if socket = server.accept?
# handle the client in a fiber
spawn handle_connection(socket)
else
# another fiber closed the server
break
end
end
endReturns the Socket::IPAddress this server listens on.
Raises Socket::Error if the socket is closed.
Returns the Socket::IPAddress this server listens on, or nil if
the socket is closed.
Sets the receive buffer size for this socket.
Returns true if this socket has been configured to reuse the address (see SO_REUSEADDR).
Returns true if this socket has been configured to reuse the port (see SO_REUSEPORT).
Sets the send buffer size for this socket.
Sets the sync flag on this socket.
All TCPSockets accepted by this server will have the same sync flag.
Returns the sync flag on this socket.
All TCPSockets accepted by this server will have the same sync flag.
Returns the number of probes sent, without response before dropping the connection.
Sets the number of probes sent, without response before dropping the connection.
Returns the amount of time (in seconds) the connection must be idle before sending keepalive probes.
Sets the amount of time (in seconds) the connection must be idle before sending keepalive probes.
Returns the amount of time (in seconds) between keepalive probes.
Sets the amount of time (in seconds) between keepalive probes.
Disable the Nagle algorithm when set to true, otherwise enables it.