class

TCPSocket

Inherits IPSocket / Socket / Crystal::System::Socket / IO::Buffered / IO / Reference / Object

A Transmission Control Protocol (TCP/IP) socket.

NOTE: To use TCPSocket, you must explicitly import it with require "socket"

Usage example:

require "socket"

client = TCPSocket.new("localhost", 1234)
client << "message\n"
response = client.gets
client.close

Constructors

new(family : Family = Family::INET, blocking = nil)

Creates a new TCPSocket, waiting to be connected.

Source
new(host : String, port, dns_timeout = nil, connect_timeout = nil, blocking = nil)

Creates a new TCP connection to a remote TCP server.

You may limit the DNS resolution time with dns_timeout and limit the connection time to the remote server with connect_timeout. Both values must be in seconds (integers or floats).

Source
new(*, fd : Handle, family : Family = Family::INET, blocking = nil)

Creates an UNIXSocket from an existing system file descriptor or socket handle.

This adopts fd into the IO system that will reconfigure it as per the event loop runtime requirements.

NOTE: On Windows, the handle must have been created with WSA_FLAG_OVERLAPPED.

Source

Class methods

open(host : String, port, &)

Opens a TCP socket to a remote TCP server, yields it to the block, then eventually closes the socket when the block returns.

Returns the value of the block.

Source

Instance methods

tcp_keepalive_count

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

Source
tcp_keepalive_count=(val : Int)
Source
tcp_keepalive_idle

The amount of time in seconds the connection must be idle before sending keepalive probes.

Source
tcp_keepalive_idle=(val : Int)
Source
tcp_keepalive_interval

The amount of time in seconds between keepalive probes.

Source
tcp_keepalive_interval=(val : Int)
Source
tcp_nodelay=(val : Bool)

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

Source
tcp_nodelay?

Returns true if the Nagle algorithm is disabled.

Source