struct UNIXServer

Overview

A local interprocess communication (UNIX socket) server socket.

Only available on UNIX and UNIX-like operating systems.

Usage example:

require "socket/unix_server"

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

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

Included Modules

Defined in:

socket/unix_server.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from module Socket::Server

accept : IO
accept(&block)
accept
, accept? : IO?
accept?(&block)
accept?

Instance methods inherited from struct Struct

==(other) : Bool ==, hash(hasher) hash, inspect(io : IO) : Nil inspect, pretty_print(pp) : Nil pretty_print, to_s(io) to_s

Instance methods inherited from struct Value

==(other : JSON::Any)
==(other : YAML::Any)
==(other)
==
, dup dup

Instance methods inherited from class Object

!=(other) !=, !~(other) !~, ==(other) ==, ===(other : JSON::Any)
===(other : YAML::Any)
===(other)
===
, =~(other) =~, class class, dup dup, hash(hasher)
hash
hash
, inspect(io : IO)
inspect
inspect
, itself itself, not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, tap(&block) tap, to_json(io : IO)
to_json
to_json
, to_pretty_json(indent : String = " ")
to_pretty_json(io : IO, indent : String = " ")
to_pretty_json
, to_s
to_s(io : IO)
to_s
, to_yaml(io : IO)
to_yaml
to_yaml
, try(&block) try, unsafe_as(type : T.class) forall T unsafe_as

Constructor methods inherited from class Object

from_json(string_or_io, root : String) : self
from_json(string_or_io) : self
from_json
, from_yaml(string_or_io : String | IO) : self from_yaml

Constructor Detail

def self.new(raw : Socket::Raw, address : Socket::UNIXAddress) #

Creates a UNIXServer from a raw socket.


[View source]
def self.new(path : String, *, mode : File::Permissions? = nil, backlog : Int32 = 128) : UNIXServer #

Creates a named UNIX socket listening on a filesystem pathname.

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

UNIXServer.new("/tmp/dgram.sock")

[View source]
def self.new(address : Socket::UNIXAddress, *, mode : File::Permissions? = nil, backlog = 128) : UNIXServer #

Creates a named UNIX socket listening on address.

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

UNIXServer.new(Socket::UNIXAddress.new("/tmp/dgram.sock"))

[View source]

Class Method Detail

def self.open(address : String | Socket::UNIXAddress, *, mode : File::Permissions? = nil, backlog = 128, &block) #

Creates a named UNIX socket listening on path and yields it to the block. Eventually closes the server socket when the block returns.

Returns the value of the block.


[View source]

Instance Method Detail

def accept : UNIXSocket #

Accepts an incoming connection and returns the client UNIXSocket.

require "socket/unix_server"

UNIXServer.open("path/to_my_socket") do |server|
  loop do
    socket = server.accept
    # handle the client in a fiber
    spawn handle_connection(socket)
  end
end

Raises if the server is closed after invoking this method.


[View source]
def accept? : UNIXSocket? #

Accepts an incoming connection.

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

require "socket/unix_server"

UNIXServer.open("path/to_my_socket") do |server|
  loop do
    if socket = server.accept?
      # handle the client in a fiber
      spawn handle_connection(socket)
    else
      # another fiber closed the server
      break
    end
  end
end

[View source]
def close #

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


[View source]
def closed? : Bool #

Returns true if this socket is closed.


[View source]
def local_address : Socket::UNIXAddress #

Returns the Socket::UNIXAddress this server listens on.

Raises Socket::Error if the socket is closed.


[View source]
def local_address? : Socket::UNIXAddress? #

Returns the Socket::UNIXAddress this server listens on, or nil if the socket is closed.


[View source]
def raw : Socket::Raw #

Returns the raw socket wrapped by this UNIX server.


[View source]
def sync=(value : Bool) : Bool #

Sets the sync flag on this socket.

All UNIXSockets accepted by this server will have the same sync flag.


[View source]
def sync? : Bool #

Returns the sync flag on this socket.

All UNIXSockets accepted by this server will have the same sync flag.


[View source]