IO::Socket Reference
Network socket
What it is #
Position in the hierarchy #
| Inherits from | — |
| Does | — |
| Consumed by | IO::Socket::INET |
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 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
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)
Returns 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.
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.
1 no-output
role IO::Socket { ... }Not executed: the documentation states no expected output for this example.