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

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:

  • 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.

recv

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

Calls "recvfrom" method with $from set to undef and returns its return value.

Exceptions:

Exceptions thrown by "recvfrom" method could be thrown.

recvfrom

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

send

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

Calls "sendto" method with $to set to undef and returns its return value.

Exceptions:

Exceptions thrown by "sendto" method could be thrown.

sendto

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

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.

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 remote 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.

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.

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.

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.

peerport

method peerport : int ();

This method is implemented in a child class.

Exceptions:

Not implemented.

peerhost

method peerhost : string ();

This method is implemented in a child class.

Exceptions:

Not implemented.

write

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

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

This method calls "send" method with $buffer set to $string, $flags set to 0, $length set to $length, and $buf_offset set to $offset.

And returns its return value.

Exceptions:

Exceptions thrown by "send" method could be thrown.

read

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

sockopt

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

setsockopt

method setsockopt : void ($level : int, $optname : int, $optval : object of string|Int)

See Also

Sys::Socket

Sys::Socket

Sys::Socket::Constant

Sys::Socket::Constant

IO::Socket::INET

IO::Socket::INET

Perl's IO::Socket

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

Copyright & License

Copyright (c) 2023 Yuki Kimoto

MIT License

1 POD Error

The following errors were encountered while parsing the POD:

Around line 327:

Unterminated C<...> sequence