Skip to content

class UNIXServer
inherits UNIXSocket

A local interprocess communication server socket.

Only available on UNIX and UNIX-like operating systems.

Example usage:

require "socket"

def handle_client(client)
  message = client.gets
  client.puts message
end

server = UNIXServer.new("/tmp/myapp.sock")
while client = server.accept?
  spawn handle_client(client)
end

Included modules

Socket::Server

Class methods

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

Creates a new UNIX server and yields it to the block. Eventually closes the server socket when the block returns.

Returns the value of the block.

View source

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

Creates a named UNIX socket, listening on a filesystem pathname.

Always deletes any existing filesystem pathname first, in order to cleanup any leftover socket file.

The server is of stream type by default, but this can be changed for another type. For example datagram messages:

UNIXServer.new("/tmp/dgram.sock", Socket::Type::DGRAM)

View source

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

Creates a UNIXServer from an already configured raw file descriptor

View source

Methods

#accept? : UNIXSocket?

Accepts an incoming connection.

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

View source

#close(delete = true)

Closes the socket, then deletes the filesystem pathname if it exists.

View source