Skip to content

class UNIXSocket
inherits Socket

A local interprocess communication clientsocket.

Only available on UNIX and UNIX-like operating systems.

Example usage:

require "socket"

sock = UNIXSocket.new("/tmp/myapp.sock")
sock.puts "message"
response = sock.gets
sock.close

Direct known subclasses

UNIXServer

Class methods

.open(path, type : Type = Type::STREAM

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.

View source

.pair(type : Type = Type::STREAM)

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"
View source

.pair(type : Type = Type::STREAM

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.

View source

.new(path : String, type : Type = Type::STREAM)

Connects a named UNIX socket, bound to a filesystem pathname.

View source

.new(*, fd : Int32, type : Type = Type::STREAM, path : String? = nil)

Creates a UNIXSocket from an already configured raw file descriptor

View source

Methods

#local_address

View source

#receive

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

#remote_address

View source