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:

Included Modules

Defined in:

socket/tcp_server.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from module Socket::Server

accept : IO
accept(&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) : self
from_json(string_or_io) : self
from_json
, from_yaml(string_or_io : String | IO) : self from_yaml

Constructor Detail

def self.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 TCPServer listening on port on all interfaces specified by host.

host can either be an IP address or a hostname.


[View source]
def self.new(raw : Socket::Raw) #

Creates a TCPServer from a raw socket.


[View source]
def self.new(address : Socket::IPAddress, *, backlog : Int32 = Socket::SOMAXCONN, reuse_port : Bool = false, reuse_address : Bool = true) : TCPServer #

Creates a new TCPServer listening on address.


[View source]
def self.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 (::).


[View source]

Class Method Detail

def self.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 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.


[View source]
def self.open(address : Socket::IPAddress, *, backlog : Int32 = Socket::SOMAXCONN, reuse_port : Bool = false, reuse_address : Bool = true, &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.


[View source]
def self.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. Eventually closes the server socket when the block returns.

Returns the value of the block.


[View source]

Instance Method Detail

def accept : TCPSocket #

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.


[View source]
def accept? : TCPSocket? #

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
end

[View source]
def close : Nil #

Closes this socket.


[View source]
def closed? : Bool #

Returns true if this socket is closed.


[View source]
def keepalive=(value : Bool) : Bool #

def keepalive? : Bool #

def local_address : Socket::IPAddress #

Returns the Socket::IPAddress this server listens on.

Raises Socket::Error if the socket is closed.


[View source]
def local_address? : Socket::IPAddress? #

Returns the Socket::IPAddress this server listens on, or nil if the socket is closed.


[View source]
def raw : Socket::Raw #

Returns the raw socket wrapped by this TCP server.


[View source]
def recv_buffer_size : Int32 #

Returns the receive buffer size for this socket.


[View source]
def recv_buffer_size=(value : Int32) : Int32 #

Sets the receive buffer size for this socket.


[View source]
def reuse_address? : Bool #

Returns true if this socket has been configured to reuse the address (see SO_REUSEADDR).


[View source]
def reuse_port? : Bool #

Returns true if this socket has been configured to reuse the port (see SO_REUSEPORT).


[View source]
def send_buffer_size : Int32 #

Returns the send buffer size for this socket.


[View source]
def send_buffer_size=(value : Int32) : Int32 #

Sets the send buffer size for this socket.


[View source]
def sync=(value : Bool) : Bool #

Sets the sync flag on this socket.

All TCPSockets accepted by this server will have the same sync flag.


[View source]
def sync? : Bool #

Returns the sync flag on this socket.

All TCPSockets accepted by this server will have the same sync flag.


[View source]
def tcp_keepalive_count : Int32 #

Returns the number of probes sent, without response before dropping the connection.


[View source]
def tcp_keepalive_count=(value : Int32) : Int32 #

Sets the number of probes sent, without response before dropping the connection.


[View source]
def tcp_keepalive_idle : Int32 #

Returns the amount of time (in seconds) the connection must be idle before sending keepalive probes.


[View source]
def tcp_keepalive_idle=(value : Int32) : Int32 #

Sets the amount of time (in seconds) the connection must be idle before sending keepalive probes.


[View source]
def tcp_keepalive_interval : Int32 #

Returns the amount of time (in seconds) between keepalive probes.


[View source]
def tcp_keepalive_interval=(value : Int32) : Int32 #

Sets the amount of time (in seconds) between keepalive probes.


[View source]
def tcp_nodelay=(value : Bool) : Bool #

Disable the Nagle algorithm when set to true, otherwise enables it.


[View source]
def tcp_nodelay? : Bool #

Returns true if the Nable algorithm is disabled.


[View source]