For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).
TCPSocket
struct TCPSocket
A TCP socket to configure.
Create one with TCPSocket.v4() or TCPSocket.v6(), then set its options
with set_option(), such as set_option[NoDelay](True).
Examples:
from std.net.tcp import TCPSocket
from std.net.tcp.option import ReusePort
def main() raises:
var socket = TCPSocket.v4()
socket.set_option[ReusePort](True)
Implemented traits
Methods
__deinit__
def __deinit__(deinit self)
Closes the socket, ignoring any error.
v4
static def v4() -> Self
Opens an IPv4 TCP socket.
Returns:
Self: The socket.
Raises:
If the socket can't be opened.
v6
static def v6() -> Self
Opens an IPv6 TCP socket.
Returns:
Self: The socket.
Raises:
If the socket can't be opened.
close
def close(deinit self)
Closes the socket, consuming it.
Raises:
If the close fails.
set_option
def set_option[Option: SettableSocketOption](self, var option: Option.OptionType)
Sets a socket option.
Parameters:
- Option (
SettableSocketOption): The option to set, such asNoDelay.
Args:
- option (
Option.OptionType): The option's new value.
Raises:
If the option can't be set.
get_option
def get_option[Option: GettableSocketOption](self) -> Option.OptionType
Returns a socket option's value.
Parameters:
- Option (
GettableSocketOption): The option to read, such asNoDelay.
Returns:
Option.OptionType: The option's value.
Raises:
If the option can't be read.