role IO::Socket { ... }

IO::Socket contains read and write methods for sockets. It is usually used through IO::Socket::INET.

Methods§

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.

If the socket has previously been read from in non-:bin mode, it's not always safe to read from it in :bin mode again. Depending on the decoder it's possible for the :bin read to skip over bytes that the decoder has read ahead messing up the order of bytes and chars. This effect can occur with the UTF-8 decoder and if the previously decoded char could still receive combiners.

In Rakudo versions prior to 2024.07 mixing of binary and non-binary reads is unsupported.

Fails if the socket is not connected.

method read§

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

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

The same caveat of mixed binary and non-binary reads described in method recv applies.

Fails if the socket is not connected.

routine get§

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.

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.

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.

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.

method close§

method close(IO::Socket:D)

Closes the socket.

Fails if the socket is not connected.

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.