Creates a Socket from an existing socket file descriptor.
Returns true
if the string represents a valid IPv4 or IPv6 address.
Creates a TCP socket.
Creates an UDP socket.
Creates an UNIX socket.
Accepts an incoming connection.
Accepts an incoming connection.
Binds the socket to a local address.
Binds the socket on port to all local interfaces.
Binds the socket to a local address.
Calls shutdown(2)
with SHUT_RD
Calls shutdown(2)
with SHUT_WR
Returns true
if this IO
is closed.
Tries to connect to a remote address.
Connects the socket to a remote address.
Connects the socket to a remote host:port.
Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.
WARNING: The behavior of SO_LINGER
is platform specific.
Tells the previously bound socket to listen for incoming connections.
Tries to listen for connections on the previously bound socket.
Receives a text message from the previously bound address.
Receives a binary message from the previously bound address.
Sends a message to the specified remote address.
Sends a message to a previously connected remote address.
NOTE optval is restricted to Int32
until sizeof works on variables.
Returns true
if this IO
is associated with a terminal device (tty), false
otherwise.
IO::Evented
IO::Buffered
IO
IO
Reference
Reference
Object
Object
Creates a Socket from an existing socket file descriptor.
Returns true
if the string represents a valid IPv4 or IPv6 address.
Creates an UDP socket. Consider using UDPSocket
unless you need full control over the socket.
Creates an UNIX socket. Consider using UNIXSocket
or UNIXServer
unless you need full control over the 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
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
Binds the socket to a local address.
require "socket" sock = Socket.tcp(Socket::Family::INET) sock.bind "localhost", 1234
Binds the socket on port to all local interfaces.
require "socket" sock = Socket.tcp(Socket::Family::INET6) sock.bind 1234
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)
Calls shutdown(2)
with SHUT_RD
Calls shutdown(2)
with SHUT_WR
Tries to connect to a remote address. Yields an IO::TimeoutError
or an Socket::ConnectError
error if the connection failed.
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")
Connects the socket to a remote host:port.
require "socket" sock = Socket.tcp(Socket::Family::INET) sock.connect "crystal-lang.org", 80
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>
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.
Tells the previously bound socket to listen for incoming connections.
Tries to listen for connections on the previously bound socket. Yields an Socket::Error
on failure.
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
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)
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)
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])
NOTE optval is restricted to Int32
until sizeof works on variables.
© 2012–2020 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.35.1/Socket.html