IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: Nightly
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​

AnyType, Deinitable, Movable

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:

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:

Returns:

Option.OptionType: The option's value.

Raises:

If the option can't be read.