Name

SPVM::IO::Socket::UNIX - UNIX Domain Socket

Description

IO::Socket::UNIX class in SPVM represents an UNIX domain socket.

Usage

use IO::Socket::UNIX;

# Client
my $sock_path = "/tmp/test.sock";
my $socket = IO::Socket::UNIX->new({
  Peer => $sock_path,
});

# Server
my $sock_path = "/tmp/test.sock";
my $socket = IO::Socket::UNIX->new({
  Local => $sock_path,
  Listen    => 5,
});

Super Class

IO::Socket

Fields

Local

has Local : string;

A local path.

Peer

has Peer : string;

A peer path.

Class Methods

new

static method new : IO::Socket::UNIX ($options : object[] = undef);

Creates a new IO::Socket::UNIX object given the options $options, and returns it.

This object represents a UNIX domain socket.

If "Peer" field is specified, this object becomes a client socket. It calls connect method.

If "Listen" field is a positive value, this object becomes a server socket. It calls bind method and listen method.

See "init" method about the options $options.

The blocking mode of the socket is set to non-blocking mode.

Instance Methods

init

protected method init : void ($options : object[] = undef);

Initializes fields of this instance given the options $options.

Options:

The following options are available adding the options for IO::Socket#init method are available.

[Name][Type][Default Value]

  • Peer : string = undef

    "Peer" field is set to this value.

  • Local : string = undef

    "Local" field is set to this value.

Domain field is always set to AF_UNIX.

Type field is set to SOCK_STREAM if Type option is not specified.

Proto field is always set to 0.

hostpath

method hostpath : string ();

Returns the pathname to the fifo at the local end.

peerpath

method peerpath : string ();

Returns the pathanme to the fifo at the peer end.

accept

method accept : IO::Socket::UNIX ($peer_ref : Sys::Socket::Sockaddr[] = undef);

This method is the same as accept method, but its return type is different.

FAQ

How to create a temporary file for a UNIX domain socket?

Use File::Temp class.

my $tmp_dir = File::Temp->newdir;

my $tmp_dir_name = $tmp_dir->dirname;

my $tmp_file = "$tmp_dir_name/test.sock";

Does a Unix domain socket work on Windows?

Yes, if Windows is recent.

See Also

Copyright & License

Copyright (c) 2023 Yuki Kimoto

MIT License