Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

IO::Socket

Native Rust implementation built into the interpreter. Runtime: PP. See original documentation for the full Perl reference.

Provides object-oriented socket operations compatible with Perl’s IO::Socket. This replaces the XS-based IO::Socket that ships with perl5.

IO::Socket inherits from IO::Handle.

Most methods are currently stubs that return plausible defaults, sufficient for modules that probe socket capabilities without performing real I/O.

Synopsis

use IO::Socket::INET;
# TCP client
my $sock = IO::Socket::INET->new(
    PeerAddr => 'localhost',
    PeerPort => 8080,
    Proto    => 'tcp',
) or die "Cannot connect: $!";
print $sock "GET / HTTP/1.0\r\n\r\n";
# TCP server
my $server = IO::Socket::INET->new(
    LocalPort => 8080,
    Listen    => 5,
    Proto     => 'tcp',
    ReuseAddr => 1,
) or die "Cannot listen: $!";

Functions

accept

Accepts an incoming connection. Stub: returns undef.

my $client = $sock->accept();

atmark

Checks if the socket is at the out-of-band mark. Stub: returns 0.

if ($sock->atmark) { ... }

bind

Binds the socket to an address. Stub: returns 1 (success).

$sock->bind($packed_addr);

connect

Connects to a remote address. Stub: returns 0 (failure).

$sock->connect($packed_addr);

connected

Returns the peer address if connected, undef otherwise. Stub: returns undef.

if ($sock->connected) { ... }

getsockopt

Get or set socket options. sockopt/getsockopt return undef, setsockopt returns 1.

$sock->setsockopt(SOL_SOCKET, SO_REUSEADDR, 1);
my $val = $sock->getsockopt(SOL_SOCKET, SO_REUSEADDR);

listen

Starts listening for connections with the given backlog. Stub: returns 1.

$sock->listen(5);

new

Constructor. Creates a new IO::Socket blessed hashref, storing any key/value constructor arguments on the hash. In perl5 this delegates to subclasses (IO::Socket::INET, IO::Socket::UNIX).

use IO::Socket;
my $sock = IO::Socket->new(Domain => AF_INET, Type => SOCK_STREAM);

peername

Returns the packed sockaddr of the remote end. Stub: returns undef.

my $addr = $sock->peername();

protocol

Returns the protocol number. Stub: returns 0.

my $proto = $sock->protocol();

recv

Receives data from the socket. Stub: returns undef.

$sock->recv($buf, 1024, $flags);

send

Sends data on the socket. Stub: returns the length of the data argument.

$sock->send($data, $flags);

setsockopt

Get or set socket options. sockopt/getsockopt return undef, setsockopt returns 1.

$sock->setsockopt(SOL_SOCKET, SO_REUSEADDR, 1);
my $val = $sock->getsockopt(SOL_SOCKET, SO_REUSEADDR);

shutdown

Shuts down part or all of the socket connection. Stub: returns 1.

$sock->shutdown(2);

sockdomain

Returns the socket domain. Stub: returns AF_INET (2).

my $domain = $sock->sockdomain();

socket

Creates the underlying socket. Stub: returns 1.

$sock->socket(AF_INET, SOCK_STREAM, 0);

socketpair

Creates a pair of connected sockets. Stub: returns 1.

$sock->socketpair(AF_UNIX, SOCK_STREAM, 0);

sockname

Returns the packed sockaddr of the local end. Stub: returns undef.

my $addr = $sock->sockname();

sockopt

Get or set socket options. sockopt/getsockopt return undef, setsockopt returns 1.

$sock->setsockopt(SOL_SOCKET, SO_REUSEADDR, 1);
my $val = $sock->getsockopt(SOL_SOCKET, SO_REUSEADDR);

socktype

Returns the socket type. Stub: returns SOCK_STREAM (1).

my $type = $sock->socktype();

timeout

Gets or sets the timeout value. When setting, returns the new value. When getting, returns undef.

$sock->timeout(30);
my $t = $sock->timeout();