class Socket
inherits IO
¶
Included modules
Crystal::System::Socket
IO::Buffered
Direct known subclasses
IPSocket
UNIXSocket
Class methods¶
.ip?(string : String)
¶
(string : String)
Returns true
if the string represents a valid IPv4 or IPv6 address.
.tcp(family : Family, blocking = false)
¶
(family : Family, blocking = false)
Creates a TCP socket. Consider using TCPSocket
or TCPServer
unless you
need full control over the socket.
.udp(family : Family, blocking = false)
¶
(family : Family, blocking = false)
Creates an UDP socket. Consider using UDPSocket
unless you need full
control over the socket.
.unix(type : Type = Type::STREAM, blocking = false)
¶
(type : Type = Type::STREAM, blocking = false)
Creates an UNIX socket. Consider using UNIXSocket
or UNIXServer
unless
you need full control over the socket.
.new(family : Family, type : Type, protocol : Protocol = Protocol::IP, blocking = false)
¶
(family : Family, type : Type, protocol : Protocol = Protocol::IP, blocking = false)
.new(fd, family : Family, type : Type, protocol : Protocol = Protocol::IP, blocking = false)
¶
(fd, family : Family, type : Type, protocol : Protocol = Protocol::IP, blocking = false)
Creates a Socket from an existing socket file descriptor / handle.
Methods¶
#accept : Socket
¶
: 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
#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
#bind(host : String, port : Int)
¶
(host : String, port : Int)
Binds the socket to a local address.
require "socket"
sock = Socket.tcp(Socket::Family::INET)
sock.bind "localhost", 1234
#bind(port : Int)
¶
(port : Int)
Binds the socket on port to all local interfaces.
require "socket"
sock = Socket.tcp(Socket::Family::INET6)
sock.bind 1234
#bind(addr : Socket::Address)
¶
(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)
#closed? : Bool
¶
View source
: Bool
#connect(addr, timeout = nil
¶
(addr, timeout = nil
Tries to connect to a remote address. Yields an IO::TimeoutError
or an
Socket::ConnectError
error if the connection failed.
#connect(host : String, port : Int, connect_timeout = nil)
¶
(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
#connect(addr, timeout = nil) : Nil
¶
(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")
#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
)
#inspect(io : IO) : Nil
¶
(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>
#linger=(val : Int?)
¶
(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.
#listen(backlog : Int = SOMAXCONN
¶
(backlog : Int = SOMAXCONN
Tries to listen for connections on the previously bound socket.
Yields an Socket::Error
on failure.
#listen(backlog : Int = SOMAXCONN)
¶
(backlog : Int = SOMAXCONN)
Tells the previously bound socket to listen for incoming connections.
#receive(max_message_size = 512) : Tuple(String, Address)
¶
(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
#receive(message : Bytes) : Tuple(Int32, Address)
¶
(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)
#send(message) : Int32
¶
(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])
#send(message, to addr : Address) : Int32
¶
(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)
#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