class IO::Socket::INET

TCP Socket

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

IO::Socket::INET provides TCP sockets, both the server and the client side.

For UDP support, please see IO::Socket::Async.

Here is an example of a very simplistic "echo" server that listens on localhost, port 3333:

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;      }
    }
 
}

And a client that connects to it, and prints out what the server answers:

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

Please bear in mind that this is a synchronous connection; an attempt by any of the nodes to write without the other reading will produce an Could not receive data from socket: Connection reset by peer error.

Methods

method new

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

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 to PF_INET6 constant for IPv6.

For text operations (such as method lines and method get), :$encoding specifies the encoding, and :$nl-in determines the character(s) that separate lines.

Methods

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.

Type Graph

Type relations for IO::Socket::INET
perl6-type-graph IO::Socket::INET IO::Socket::INET Any Any IO::Socket::INET->Any IO::Socket IO::Socket IO::Socket::INET->IO::Socket Mu Mu Any->Mu

Expand above chart

Routines supplied by role IO::Socket

IO::Socket::INET does role IO::Socket, which provides the following routines:

(IO::Socket) method recv

method recv(IO::Socket:D: Cool $elems = Inf:$bin)

Receive a packet and return it, either as a Blob if :bin was passed, or a Str if not. Receives up to $elems or 65535 (whichever is smaller) bytes or characters.

Fails if the socket is not connected.

(IO::Socket) method read

method read(IO::Socket:D: Int(Cool$bytes)

Reads $bytes bytes from the socket and returns them in a Blob.

Fails if the socket is not connected.

(IO::Socket) routine get

Defined as:

method get(IO::Socket:D: --> Str:D)

Reads a single line of input from the socket, removing the trailing newline characters (as set by .nl-in). Returns Nil, if no more input is available.

Fails if the socket is not connected.

(IO::Socket) method print

method print(IO::Socket:D: Str(Cool$string)

Writes the supplied string to the socket, thus sending it to other end of the connection. The binary version is method write.

Fails if the socket is not connected.

(IO::Socket) method write

method write(IO::Socket:D: Blob:D $buf)

Writes the supplied buffer to the socket, thus sending it to other end of the connection. The string version is method print.

Fails if the socket is not connected.

(IO::Socket) method put

method put(IO::Socket:D: Str(Cool$string)

Writes the supplied string, with a \n appended to it, to the socket, thus sending it to other end of the connection.

Fails if the socket is not connected.

(IO::Socket) method close

method close(IO::Socket:D)

Closes the socket.

Fails if the socket is not connected.

(IO::Socket) method native-descriptor

method native-descriptor()

This returns a value that the operating system would understand as a "socket descriptor" and is suitable for passing to a native function that requires a socket descriptor as an argument such as setsockopt.