Rules / Types, classes & roles / domain-specific

IO::Socket::INET Reference

TCP Socket

What it is #

Routines #

Signatures are reproduced from the documentation, including their declared return types. A routine listed here is part of the type's published interface; whether Raku++ implements it is a separate question, answered by the examples below.

method new #

multi method new(
multi method new(

Creates a new socket. If :$listen is True, creates a new socket that listen on :$localhost (which can be an IP address or a domain name) on port :$localport; in other words the :$listen flag determines the server mode of the socket. Otherwise (i.e., :$listen is False), the new socket opens immediately a connection to :$host on port :$port. :$family defaults to PF_INET constant for IPv4, and can be set

method get #

method get()

Reads a line from the socket and returns it as of type Str. Return Nil on end-of-file (EOF).

method lines #

method lines()

Returns a lazy list of lines read from the socket.

method accept #

method accept()

In listen/server mode, waits for a new incoming connection. Once a new connection is established, an IO::Socket::INET instance (or a subclass instance) for consuming the connection is returned.

Examples, run three ways #

Every example below comes from the official documentation, together with the output that documentation asserts. Each was then executed by Rakudo and by Raku++ when this page was built. Where the three agree, one result is shown; where they do not, all three are — because which of them is wrong is exactly the information worth having.

4 no-output

class IO::Socket::INET does IO::Socket {}

Not executed: the documentation states no expected output for this example.

my $listen = IO::Socket::INET.new( :listen,
                               :localhost<localhost>,
                               :localport(3333) );
loop {
my $conn = $listen.accept;
try {
    while my $buf = $conn.recv(:bin) {
        $conn.write: $buf;
    }
}
$conn.close;

CATCH {
      default { .payload.say;      }
}

}

Not executed: the documentation states no expected output for this example.

my $conn = IO::Socket::INET.new( :host<localhost>,
                             :port(3333) );
$conn.print: 'Hello, Raku';
say $conn.recv;
$conn.close;

Not executed: the documentation states no expected output for this example.

    :$host,
    :$port,
    :$family = PF_INET,
    :$encoding = 'utf-8',
    :$nl-in = "\r\n",
--> IO::Socket::INET:D)
    :$localhost,
    :$localport,
    :$family = PF_INET,
    :$listen,
    :$encoding = 'utf-8',
    :$nl-in = "\r\n",
--> IO::Socket::INET:D)

Not executed: the documentation states no expected output for this example.