Name

SPVM::IO::Socket - Sockets

Description

IO::Socket in SPVM is an abstract class that represents a network socket. It is designed to work seamlessly with Go and Go::Context.

Usage

use IO::Socket::IP;
use Go;
use Go::Time;

# Create a simple TCP client
my $socket = IO::Socket::IP->new({
  Context => Go->bg,
  PeerAddr => "example.com",
  PeerPort => 80,
});

# Send a request
my $payload = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n";
$socket->syswrite($payload);

Details

IO::Socket is an abstract class. For concrete implementations, see "Well Known Child Classes".

Socket Constant Values

See Sys::Socket::Constant for constant values such as AF_INET, SOCK_STREAM, etc.

Goroutine and Non-blocking I/O

All I/O operations in this class (connect, accept, read, write, etc.) are non-blocking and integrated with goroutines.

If an I/O operation cannot be completed immediately, the current goroutine yields control to the Go scheduler, allowing other goroutines to run. When the socket becomes ready, the goroutine is automatically resumed.

Example:

# Running a concurrent TCP server
Go->go(method : void () {
  my $listen = IO::Socket::IP->new({
    Context => Go->bg,
    LocalAddr => "0.0.0.0",
    LocalPort => 8080,
    Listen    => 10,
  });
  
  while (my $client = $listen->accept) {
    # Handle each connection in a new goroutine
    Go->go([$client : IO::Socket] method : void () {
      my $buf = (mutable string)new_string_len 1024;
      $client->sysread(Go->bg, $buf);
      $client->syswrite(Go->bg, "Hello from Goroutine!");
      $client->close;
    });
  }
});

Go->gosched;

Well Known Child Classes

Super Class

IO::Handle

Fields

Domain

has Domain : protected int;

A protocol family, such as AF_INET, AF_INET6, AF_UNIX.

Type

has Type : protected int;

A socket type, such as SOCK_STREAM, SOCK_DGRAM, SOCK_RAW.

Proto

has Proto : protected int;

A socket protocol, such as IPPROTO_TCP, IPPROTO_UDP.

Listen

has Listen : protected int;

The number of listen backlog.

Sockaddr

has Sockaddr : protected Sys::Socket::Sockaddr;

A Sys::Socket::Sockaddr object used by "connect" or "bind" method.

Instance Methods

init

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

Options:

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

[Name][Type][Default Value]

  • Domain : Int = 0

  • Type : Int = 0

  • Proto : Int = 0

  • Listen : Int = 0

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

Exceptions:

The Blocking option is not allowed in this class or its children.

sockdomain

method sockdomain : int ();

Returns the value of "Domain" field.

socktype

method socktype : int ();

Returns the value of "Type" field.

protocol

method protocol : int ();

Returns the value of "Proto" field.

set_blocking

method set_blocking : void ($blocking : int);

This method is the same as IO::Handle#set_blocking method, but if a true value is given to $blocking, an exception is thrown.

Exceptions:

Calling set_blocking method given a true value on an IO::Socket object is forbidden.

socket

protected method socket : void ();

Opens a socket using "Domain" field, "Type" field, and "Proto" field.

IO::Handle#FD field is set to the file descriptor of the socket.

This method calls Sys#socket method.

Exceptions:

Exceptions thrown by Sys#socket method could be thrown.

connect

protected method connect : void ($context : Go::Context);

Performs connect operation with a context $context.

Exceptions:

Exceptions thrown by Sys#connect method could be thrown.

Exceptions thrown by Go#gosched_io_write method could be thrown.

bind

protected method bind : void ();

Performs bind operation.

This method calls Sys#bind method given the value of IO::Handle#FD field and the value of "Sockaddr" field.

Exceptions:

Exceptions thrown by Sys#bind method could be thrown.

listen

protected method listen : void ();

Does the same thing that listen system call does given the file descriptor IO::Handle#FD field.

This method calls Sys#listen.

Exceptions:

"Listen" field must be greater than 0.

Exceptions thrown by Sys#listen method could be thrown.

accept

method accept : IO::Socket ($context : Go::Context, $peer_ref : Sys::Socket::Sockaddr[] = undef);

Performs accept operation and returns a client socket object.

Returns a new client socket instance.

Exceptions:

Exceptions thrown by Sys#accept method could be thrown.

Exceptions thrown by Go#gosched_io_read method could be thrown.

shutdown

method shutdown : void ($how : int);

Performs shutdown operation given the way $how.

This method calls Sys#shutdown method.

See Sys::Socket::Constant about constant values for $how: SHUT_RD, SHUT_WR, SHUT_RDWR.

Exceptions:

Exceptions thrown by Sys#shutdown method could be thrown.

close

method close : void ();

Closes the socket file descriptor.

Exceptions:

If this socket is not opened or already closed, an exception is thrown.

recvfrom

method recvfrom : int ($context : Go::Context, $buffer : mutable string, $length : int, $flags : int, $from_ref : Sys::Socket::Sockaddr[], $offset : int = 0)

Performs recvfrom operation and returns read length with a context $context.

If no data is available, it yields until the socket is ready.

Exceptions:

Exceptions thrown by Sys#recvfrom method could be thrown.

Exceptions thrown by Go#gosched_io_read method could be thrown.

sendto

method sendto : int ($context : Go::Context, $buffer : string, $flags : int, $to : Sys::Socket::Sockaddr, $length : int = -1, $offset : int = 0);

Performs sendto operation and returns write length with a context.

If the transmit buffer is full, it yields until space becomes available.

Exceptions:

Exceptions thrown by Sys#sendto method could be thrown.

Exceptions thrown by Go#gosched_io_write method could be thrown.

recv

method recv : int ($context : Go::Context, $buffer : mutable string, $length : int = -1, $flags : int = 0, $offset : int = 0);

Performs recv operation by calling "recvfrom" with $from_ref set to undef.

Exceptions:

Exceptions thrown by "recvfrom" method could be thrown.

send

method send : int ($context : Go::Context, $buffer : string, $flags : int = 0, $length : int = -1, $offset : int = 0);

Performs send operation by calling "sendto" with $to set to undef.

Exceptions:

Exceptions thrown by "sendto" method could be thrown.

sysread

method sysread : int ($context : Go::Context, $buffer : mutable string, $length : int = -1, $offset : int = 0);

Perform sysread operation by calling "recv" with $flags set to 0.

Exceptions:

Exceptions thrown by "recv" method could be thrown.

syswrite

method syswrite : int ($context : Go::Context, $buffer : string, $length : int = -1, $offset : int = 0);

Perform syswrite operation by calling "send" with $flags set to 0.

Exceptions:

Exceptions thrown by "send" method could be thrown.

sockname

method sockname : Sys::Socket::Sockaddr ();

Returns a Sys::Socket::Sockaddr for the local address.

Exceptions:

Exceptions thrown by Sys#getsockname method could be thrown.

peername

method peername : Sys::Socket::Sockaddr ();

Returns a Sys::Socket::Sockaddr for the remote address.

Exceptions:

Exceptions thrown by Sys#getpeername method could be thrown.

connected

method connected : Sys::Socket::Sockaddr ();

Checks if the socket is connected. Returns a Sys::Socket::Sockaddr if "peername" succeeds, otherwise returns undef.

atmark

method atmark : int ();

Calls Sys::Socket#sockatmark and returns the result.

Exceptions:

Exceptions thrown by Sys::Socket#sockatmark method could be thrown.

sockopt

method sockopt : int ($level : int, $option_name : int);

Gets a socket option by calling Sys#getsockopt.

Exceptions:

Exceptions thrown by Sys#getsockopt method could be thrown.

setsockopt

method setsockopt : void ($level : int, $option_name : int, $option_value : object of string|Int);

Sets a socket option by calling Sys#setsockopt.

Exceptions:

Exceptions thrown by Sys#setsockopt method could be thrown.

See Also

Porting

This class is a port of Perl's IO::Socket to SPVM.

Copyright & License

Copyright (c) 2026 Yuki Kimoto

MIT License