mtcp_accept − accept a connection on an mTCP socket


#include <mtcp_api.h>

int mtcp_accept(mctx_t mctx, int sockid, struct sockaddr *addr, socklen_t *addrlen);


mtcp_accept() function is used to accept an incoming connection request from a TCP client. During a successful mtcp_accept() call, the mTCP stack extracts the first connection request from the listening queue of pending connections associated with the passive socket ID sockid. It then creates a new socket and returns its corresponding socket ID to the user. The newly created socket (that is returned) can be used to communicate with the TCP client while the original socket sockid is unaffected by this call. The passive socket represented by the sockid socket descriptor should be bound to a local address using mtcp_bind() function and should be listening for connections by calling mtcp_listen() function before it can be used for accepting new connection requests.

The addr argument is a pointer to sockaddr structure that gets filled up with the address of the peer node while the addrlen field contains the size of the peer address. If the addr argument is NULL, nothing is filled in and as a result, addrlen is not used and should also be NULL.

If no pending connections are present on the queue, mtcp_accept() fails with error EAGAIN errno.

In order to be notified of incoming connections on a socket, the user can use mtcp_epoll_wait() function call. A readable event is delivered when a new connection arrives on the listening socket.

An mtcp_accept() call takes an additional argument named mctx that represents the per-core mTCP context in an application (see mtcp_create_context() for details).


Returns an mTCP descriptor ID of the newly created active mTCP socket; -1 on failure. In case of failure, errno is set appropriately.



No connections requests are available at the moment.


sockid is not a valid socket descriptor for accepting connections.


The application thread is scheduled to shut down.


The socket referred to by the sockid argument is not a listening (passive) socket.


New active socket could not be created as the mTCP core is out of socket descriptors at the moment.


For performance reasons, it is recommended that the same thread (represented by mctx argument) that spawns the active socket via mtcp_accept() should handle operations (mtcp_read(), mtcp_write(), and mtcp_getsockopt()) on that socket for the entire lifetime of the connection (till mtcp_close()).


mTCP development team <>


mtcp_bind(), mtcp_listen(), mtcp_socket(), 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