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 before it listens or connects.

Create one with TCPSocket.v4() or TCPSocket.v6(), then set its options with set_option(), such as set_option[NoDelay](True). listen() turns it into a TCPListener after bind() gives it a local address, and connect() turns it into a TCPStream.

Examples:

from std.net import SocketAddrV4
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)
socket.bind(.v4(SocketAddrV4(.localhost, 0)))
var listener = socket^.listen()

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.

bind​

def bind(self, addr: SocketAddr)

Binds the socket to a local address.

Unlike TCPListener(bind=), it leaves ReuseAddress as it is, so set that option first to bind a port that connections from an earlier socket still hold.

Args:

  • ​addr (SocketAddr): The local address, of the socket's IP version. Port 0 picks a free port, which local_addr() on the listener or stream returns.

Raises:

If the bind fails, such as when another socket holds the address or addr isn't of the socket's IP version.

listen​

def listen(deinit self, *, backlog: Int = Int(128)) -> TCPListener

Listens for connections, consuming the socket.

Args:

  • ​backlog (Int): How many connections can wait to be accepted.

Returns:

TCPListener: The listener.

Raises:

If backlog doesn't fit in a C int, or the listen fails.

connect​

def connect(deinit self, addr: SocketAddr) -> TCPStream

Connects the socket to a remote address, consuming the socket.

Args:

  • ​addr (SocketAddr): The remote address, of the socket's IP version.

Returns:

TCPStream: The connection.

Raises:

If the connection fails, such as when nothing listens at addr or addr isn't of the socket's IP version.