Skip to content

class Socket
inherits IO

Included modules

Crystal::System::Socket IO::Buffered

Direct known subclasses

IPSocket UNIXSocket

Class methods

.fcntl(fd, cmd, arg = 0)

View source

.ip?(string : String)

Returns true if the string represents a valid IPv4 or IPv6 address.

View source

.tcp(family : Family, blocking = false)

Creates a TCP socket. Consider using TCPSocket or TCPServer unless you need full control over the socket.

View source

.udp(family : Family, blocking = false)

Creates an UDP socket. Consider using UDPSocket unless you need full control over the socket.

View source

.unix(type : Type = Type::STREAM, blocking = false)

Creates an UNIX socket. Consider using UNIXSocket or UNIXServer unless you need full control over the socket.

View source

.new(family : Family, type : Type, protocol : Protocol = Protocol::IP, blocking = false)

View source

.new(fd, family : Family, type : Type, protocol : Protocol = Protocol::IP, blocking = false)

Creates a Socket from an existing socket file descriptor / handle.

View source

Methods

#accept : Socket

Accepts an incoming connection.

Returns the client socket. Raises an IO::Error (closed stream) exception if the server is closed after invoking this method.

require "socket"

server = TCPServer.new(2202)
socket = server.accept
socket.puts Time.utc
socket.close
View source

#accept?

Accepts an incoming connection.

Returns the client Socket or nil if the server is closed after invoking this method.

require "socket"

server = TCPServer.new(2202)
if socket = server.accept?
  socket.puts Time.utc
  socket.close
end
View source

#bind(host : String, port : Int)

Binds the socket to a local address.

require "socket"

sock = Socket.tcp(Socket::Family::INET)
sock.bind "localhost", 1234
View source

#bind(port : Int)

Binds the socket on port to all local interfaces.

require "socket"

sock = Socket.tcp(Socket::Family::INET6)
sock.bind 1234
View source

#bind(addr : Socket::Address)

Binds the socket to a local address.

require "socket"

sock = Socket.udp(Socket::Family::INET)
sock.bind Socket::IPAddress.new("192.168.1.25", 80)
View source

#blocking

View source

#blocking=(value)

View source

#broadcast=(val : Bool)

View source

#broadcast?

View source

#close_on_exec=(arg : Bool)

View source

#close_on_exec?

View source

#close_read

Calls shutdown(2) with SHUT_RD

View source

#close_write

Calls shutdown(2) with SHUT_WR

View source

#closed? : Bool

Returns true if this IO is closed.

IO defines returns false, but including types may override.

View source

#connect(addr, timeout = nil

Tries to connect to a remote address. Yields an IO::TimeoutError or an Socket::ConnectError error if the connection failed.

View source

#connect(host : String, port : Int, connect_timeout = nil)

Connects the socket to a remote host:port.

require "socket"

sock = Socket.tcp(Socket::Family::INET)
sock.connect "crystal-lang.org", 80
View source

#connect(addr, timeout = nil) : Nil

Connects the socket to a remote address. Raises if the connection failed.

require "socket"

sock = Socket.unix
sock.connect Socket::UNIXAddress.new("/tmp/service.sock")
View source

#family : Family

View source

#fcntl(cmd, arg = 0)

View source

#fd

Returns the handle associated with this socket from the operating system.

  • on POSIX platforms, this is a file descriptor (Int32)
  • on Windows, this is a SOCKET handle (LibC::SOCKET)
View source

#finalize

View source

#inspect(io : IO) : Nil

Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.

class Person
  def initialize(@name : String, @age : Int32)
  end
end

Person.new("John", 32).inspect # => #<Person:0x10fd31f20 @name="John", @age=32>
View source

#keepalive=(val : Bool)

View source

#keepalive?

View source

#linger

View source

#linger=(val : Int?)

Warning

The behavior of SO_LINGER is platform specific. Bad things may happen especially with nonblocking sockets. See Cross-Platform Testing of SO_LINGER by Nybek for more information.

  • nil: disable SO_LINGER
  • Int: enable SO_LINGER and set timeout to Int seconds
  • 0: abort on close (socket buffer is discarded and RST sent to peer). Depends on platform and whether shutdown() was called first.
  • >=1: abort after Int seconds on close. Linux and Cygwin may block on close.
View source

#listen(backlog : Int = SOMAXCONN

Tries to listen for connections on the previously bound socket. Yields an Socket::Error on failure.

View source

#listen(backlog : Int = SOMAXCONN)

Tells the previously bound socket to listen for incoming connections.

View source

#protocol : Protocol

View source

#receive(max_message_size = 512) : Tuple(String, Address)

Receives a text message from the previously bound address.

require "socket"

server = Socket.udp(Socket::Family::INET)
server.bind("localhost", 1234)

message, client_addr = server.receive
View source

#receive(message : Bytes) : Tuple(Int32, Address)

Receives a binary message from the previously bound address.

require "socket"

server = Socket.udp(Socket::Family::INET)
server.bind("localhost", 1234)

message = Bytes.new(32)
bytes_read, client_addr = server.receive(message)
View source

#recv_buffer_size

View source

#recv_buffer_size=(val : Int32)

View source

#reuse_address=(val : Bool)

View source

#reuse_address?

View source

#reuse_port=(val : Bool)

View source

#reuse_port?

View source

#send(message) : Int32

Sends a message to a previously connected remote address.

require "socket"

sock = Socket.udp(Socket::Family::INET)
sock.connect("example.com", 2000)
sock.send("text message")

sock = Socket.unix(Socket::Type::DGRAM)
sock.connect Socket::UNIXAddress.new("/tmp/service.sock")
sock.send(Bytes[0])
View source

#send(message, to addr : Address) : Int32

Sends a message to the specified remote address.

require "socket"

server = Socket::IPAddress.new("10.0.3.1", 2022)
sock = Socket.udp(Socket::Family::INET)
sock.connect("example.com", 2000)
sock.send("text query", to: server)
View source

#send_buffer_size

View source

#send_buffer_size=(val : Int32)

View source

#tty?

Returns true if this IO is associated with a terminal device (tty), false otherwise.

IO returns false, but including types may override.

STDIN.tty?          # => true
IO::Memory.new.tty? # => false
View source

#type : Type

View source