mtcp_socket − instantiate an mTCP endpoint for communication


#include <mtcp_api.h>

int mtcp_socket(mctx_t mctx, int domain, int type, int protocol);


mtcp_socket() can be used to create a communication endpoint for mTCP-based network connection. In many ways, it behaves similar to a BSD socket. An mtcp_socket() call takes an additional argument named mctx that represents the per-core mTCP context in an application (see mtcp_create_context() for details).

The domain argument is used to specify a communication domain; in mTCP, the only available protocol family for the domain is AF_INET which represents IPv4 Internet protocol.

The socket type denotes the communication semantics and the only available type in mTCP at the moment is SOCK_STREAM which represents TCP’s reliable, sequenced, full-duplex communication between two endpoints.

The protocol argument represents the specific protocol ID to be used with the socket. mTCP assumes this argument to be 0 (since it only supports single-protocol sockets).

Sockets of type SOCK_STREAM are full-duplex bytestreams. A stream socket must be in ESTABLISHED TCP state before data can be sent or received on it. A connection to another peer endpoint can be created with an mtcp_connect() call. Once connected, data may be exchanged using mtcp_read() and mtcp_write() calls. When a session needs to be terminated, mtcp_close() can be called. Socket operations can be manipulated using socket level options via mtcp_setsockopt() and mtcp_getsockopt() function calls. mTCP sockets run in non-blocking mode by default.


Returns an mTCP descriptor ID on success; -1 on failure. In case of failure, errno is set appropriately. Please note that each mTCP context (referred to by mctx ) creates its own dedicated pool of sockets.



domain is not supported.


The socket type is invalid.


No socket descriptor is available in the pool at the moment.


mTCP development team <>


mtcp_bind(), mtcp_listen(), mtcp_accept(), mtcp_connect(), mtcp_read(), mtcp_write()


This page is part of mTCP release 3.0 docs section. A description of the project, and information about reporting bugs, can be found at