Name

SPVM::IO::Socket - Sockets

Description

SPVM::IO::Socket class has methods for sockets.

Usage

use IO::Socket;
use Sys::Socket::Constant as SOCKET;

# Create a new AF_INET socket
my $socket = IO::Socket->new({Domain => SOCKET->AF_INET});

# Create a new AF_INET6 socket
my $socket = IO::Socket->new({Domain => SOCKET->AF_INET6});

# Create a new AF_UNIX socket
my $socket = IO::Socket->new({Domain => SOCKET->AF_UNIX});

Details

Porting

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

Socket Constant Values

See Sys::Socket::Constant about constant values for sockets.

Goroutine

IO::Socket class works with Go class.

Sockets created with IO::Socket class are non-blocking sockets by default.

If a socket connect, accept, read, or write operation needs to wait for IO, the program passes control to a Goroutine.

The control will return when IO waiting is finished or a timeout occurs.

Super Class

IO::Handle

Fields

Domain

has Domain : protected int;

A protocol family, like AF_INET, AF_INET6, AF_UNIX.

Type

has Type : protected int;

A socket type, like SOCK_STREAM, SOCK_DGRAM.

Proto

has Proto : protected ro int;

A particular protocol, normally this is set to 0.

Timeout

has Timeout : protected double;

A timeout seconds for system calls that would set errno to EWOULDBLOCK, like read(), write(), connect(), accept().

Listen

has Listen : protected int;

Class Methods

new

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

The socket is set to non-blocking mode.

Options:

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

  • Domain : Int

  • Type : Int

  • Proto : Int

  • Timeout : Double

  • Listen : Int

See also SPVM::Sys::Socket::Constant.

Instance Methods

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.

timeout

method timeout : double ();

Returns the value of "Timeout" field.

set_timeout

method set_timeout : void ($timeout : double);

Sets "Timeout" field to $timeout.

DESTROY

method DESTROY : void ();

A destructor. This method closes the socket by calling "close" method if the socket is opened.

socket

method socket : void ();

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

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

This method calls Sys#socket method.

Exceptions:

Exceptions thrown by Sys#socket method could be thrown.

shutdown

method shutdown : void ($how : int);

Shuts down the socket assciated with the file descriptor IO::Handle#FD field given the way $how.

This method calls Sys#shutdown method.

See Sys::Socket::Constant about constant values given to $how.

  • SHUT_RD

  • SHUT_WR

  • SHUT_RDWR

Exceptions:

Exceptions thrown by Sys#shutdown method could be thrown.

close

method close : int ();

Closes the socket assciated with the file descriptor IO::Handle#FD field.

Exceptions:

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

bind

method bind : void ($sockaddr : Sys::Socket::Sockaddr);

Does the same thing that bind system call does given a socket address $sockaddr and the file descriptor stored in IO::Handle#FD field.

This method calls Sys#bind.

Exceptions:

Exceptions thrown by Sys#bind method could be thrown.

listen

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:

Exceptions thrown by Sys#listen method could be thrown.

accept

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

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

This method calls Sys#accept.

Exceptions:

Exceptions thrown by Sys#accept method could be thrown.

recvfrom

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

sendto

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

recv

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

Calls "recvfrom" method given the arguments to this method with $from_ref set to undef and returns its return value.

Exceptions:

Exceptions thrown by "recvfrom" method could be thrown.

send

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

Calls "sendto" method given the arguments to this method with $to set to undef and returns its return value.

Exceptions:

Exceptions thrown by "sendto" method could be thrown.

read

method read : int ($buffer : mutable string, $length : int = -1, $offset : int = 0);

Reads the length $length of data from the stream associated with the file descriptoer "FD" and store it to the offset $offset position of the string $string.

And returns the read length.

This method calls "recv" method given the arguments given to this method and returns its return values.

write

method write : int ($buffer : string, $length : int = -1, $offset : int = 0);

Writes the length $length from the offset $offset of the string $buffer to the stream associated with the file descriptoer "FD".

And returns the write length.

This method calls "send" method given the arguments given to this method and returns its return values.

Exceptions:

Exceptions thrown by "send" method could be thrown.

sockname

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

Returns the local socket address of the socket assciated with the file descriptor IO::Handle#FD field.

This method calls Sys#getsockname method.

Exceptions:

Exceptions thrown by Sys#getsockname method could be thrown.

peername

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

Returns the peer socket address of the socket assciated with the file descriptor IO::Handle#FD field.

This method calls Sys#getpeername method.

Exceptions:

Exceptions thrown by Sys#getpeername method could be thrown.

connected

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

If "peername" method does not throw an exception, returns the return value, otherwise returns undef.

atmark

method atmark : int ();

If the socket assciated with the file descriptor IO::Handle#FD field is currently positioned at the urgent data mark, returns 1, otherwise returns 0.

This method calls Sys::Socket#sockatmark method.

Exceptions:

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

sockopt

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

Gets a socket option of the file descriptor stored in IO::Handle#FD field given the socket level $level and the option name $option_name.

This method calls Sys#getsockopt method given the arguments given to this method and returns its return value.

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 of the file descriptor stored in IO::Handle#FD field given the socket level $level, the option name $option_name, and the option value $option_value.

This method calls Sys#setsockopt method given the arguments given to this method.

Exceptions:

Exceptions thrown by Sys#setsockopt method could be thrown.

Well Known Child Classes

See Also

Copyright & License

Copyright (c) 2023 Yuki Kimoto

MIT License