A Transmission Control Protocol (TCP/IP) server.
Usage example:
require "socket"
def handle_client(client)
message = client.gets
client.puts message
end
server = TCPServer.new("localhost", 1234)
while client = server.accept?
spawn handle_client(client)
end Options:
SO_REUSEPORT).Binds a socket to the host and port combination.
Creates a new TCPServer, waiting to be bound.
Creates a new TCP server, listening on all local interfaces (::).
Creates a TCPServer from an already configured raw file descriptor
Creates a new TCP server, listening on all interfaces, and yields it to the block.
Creates a new TCP server and yields it to the block.
Accepts an incoming connection.
Socket::Server
TCPSocket
TCPSocket
TCPSocket
IPSocket
Socket
Socket
Socket
IO::Evented
IO::Buffered
IO
IO
Reference
Reference
Object
Object
Binds a socket to the host and port combination.
Creates a new TCP server, listening on all local interfaces (::).
Creates a TCPServer from an already configured raw file descriptor
Creates a new TCP server, listening on all interfaces, and yields it to the block. Eventually closes the server socket when the block returns.
Returns the value of the block.
Creates a new TCP server and yields it to the block. Eventually closes the server socket when the block returns.
Returns the value of the block.
Accepts an incoming connection.
Returns the client TCPSocket or nil if the server is closed after invoking this method.
require "socket"
server = TCPServer.new(2022)
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
© 2012–2020 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.35.1/TCPServer.html