UNIXSocket
Inherits Socket / Crystal::System::Socket / IO::Buffered / IO / Reference / Object
A local interprocess communication clientsocket.
Available on UNIX-like operating systems, and Windows 10 Build 17063 or above. Not all features are supported on Windows.
NOTE: To use UNIXSocket, you must explicitly import it with require "socket"
Example usage:
require "socket"
sock = UNIXSocket.new("/tmp/myapp.sock")
sock.puts "message"
response = sock.gets
sock.close
Constructors
Connects a named UNIX socket, bound to a filesystem pathname.
Creates an UNIXSocket from an existing system file descriptor or socket handle.
This adopts the system socket 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.
Class methods
Opens an UNIX socket to a filesystem pathname, yields it to the block, then eventually closes the socket when the block returns.
Returns the value of the block.
Returns a pair of unnamed UNIX sockets.
require "socket"
left, right = UNIXSocket.pair
spawn do
# echo server
message = right.gets
right.puts message
end
left.puts "message"
left.gets # => "message"
Creates a pair of unnamed UNIX sockets (see pair) and yields them to the
block. Eventually closes both sockets when the block returns.
Returns the value of the block.
Instance methods
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