Skip to content

class TCPSocket
inherits IPSocket

A Transmission Control Protocol (TCP/IP) socket.

Usage example:

require "socket"

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

Direct known subclasses

TCPServer

Class methods

.open

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.

View source

.new(host, port, dns_timeout = nil, connect_timeout = 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).

Note that dns_timeout is currently ignored.

View source

.new(family : Family = Family::INET)

Creates a new TCPSocket, waiting to be connected.

View source

.new(*, fd : Int32, family : Family = Family::INET)

Creates a TCPSocket from an already configured raw file descriptor

View source

Methods

#tcp_keepalive_count

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

View source

#tcp_keepalive_count=(val : Int)

View source

#tcp_keepalive_idle

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

View source

#tcp_keepalive_idle=(val : Int)

View source

#tcp_keepalive_interval

The amount of time in seconds between keepalive probes.

View source

#tcp_keepalive_interval=(val : Int)

View source

#tcp_nodelay=(val : Bool)

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

View source

#tcp_nodelay?

Returns true if the Nagle algorithm is disabled.

View source