netconn_accept()

Name

netconn_accept() -- Wait for incoming connections

Synopsis

struct netconn *netconn_accept(struct netconn *conn);

Description

This function blocks the process until a connection request from a remote host arrives on the TCP connection conn. The connection must be in the LISTEN state so netconn_listen() must be called prior to netconn_accept(). When a connection is established with the remote host, a new connection structure is returned.

Example

Example 4-1. This example shows how to open a TCP server on port 2000

Note: This is only an example for illustrative purposes, and a complete version should perform comprehensive error checking.

int
main()
{
    struct netconn *conn, *newconn;

    /* create a connection structure */
    conn = netconn_new(NETCONN_TCP);

    /* bind the connection to port 2000 on any local IP address */
    netconn_bind(conn, NULL, 2000);

    /* tell the connection to listen for incoming connection requests */
    netconn_listen(conn);

    /* block until we get an incoming connection */
    newconn = netconn_accept(conn);

    /* do something with the connection */
    process_connection(newconn);

    /* deallocate both connections */
    netconn_delete(newconn);
    netconn_delete(conn);
}