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.crConstructors
-
.new(raw : Socket::Raw, address : Socket::UNIXAddress)
Creates a
UNIXServerfrom a raw socket. -
.new(path : String, *, mode : File::Permissions? = nil, backlog : Int32 = 128) : UNIXServer
Creates a named UNIX socket listening on a filesystem pathname.
-
.new(address : Socket::UNIXAddress, *, mode : File::Permissions? = nil, backlog = 128) : UNIXServer
Creates a named UNIX socket listening on address.
Class Method Summary
-
.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.
Instance Method Summary
-
#accept : UNIXSocket
Accepts an incoming connection and returns the client
UNIXSocket. -
#accept? : UNIXSocket?
Accepts an incoming connection.
-
#close
Closes the socket, then deletes the filesystem pathname if it exists.
-
#closed? : Bool
Returns
trueif this socket is closed. -
#local_address : Socket::UNIXAddress
Returns the
Socket::UNIXAddressthis server listens on. -
#local_address? : Socket::UNIXAddress?
Returns the
Socket::UNIXAddressthis server listens on, ornilif the socket is closed. -
#raw : Socket::Raw
Returns the raw socket wrapped by this UNIX server.
-
#sync=(value : Bool) : Bool
Sets the sync flag on this socket.
-
#sync? : Bool
Returns the sync flag on this socket.
Instance methods inherited from module Socket::Server
accept : IOaccept(&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) : selffrom_json(string_or_io) : self from_json, from_yaml(string_or_io : String | IO) : self from_yaml
Constructor Detail
Creates a UNIXServer from a raw socket.
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")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"))Class Method Detail
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.
Instance Method Detail
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.
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
endReturns the Socket::UNIXAddress this server listens on.
Raises Socket::Error if the socket is closed.
Returns the Socket::UNIXAddress this server listens on, or nil if the socket is closed.
Sets the sync flag on this socket.
All UNIXSockets accepted by this server will have the same sync flag.
Returns the sync flag on this socket.
All UNIXSockets accepted by this server will have the same sync flag.