lwIP Wiki
Advertisement

netconn_bind[]

err_t netconn_bind ( struct netconn * aNetConn, ip_addr_t * aAddr, u16_t aPort);
  • in aNetConn : the netconn object to be bound to the given IP/port
  • in aAddr : the local IP address, i.e. the address of the device lwIP is running on. Use IP_ADDR_ANY (which translates to NULL) to bind to any IP.
  • in aPort : the port number the connection will use

Bind a netconn to a local IP address or to any IP address. Note that if you try to bind the same address and/or port you might get an error (ERR_USE, address in use), even if you delete the netconn. Only after some time (minutes) the resources are completely cleared in the underlying stack due to the need to follow the TCP specification and go through the TCP timewait state.

The following function is used to implement a server. netconn_bind can be used for a client too (see example in netconn_connect) but is often omitted if you don't care about the local address and port.

Example of a server (lwIP v1.4.0):

#define TNetConn struct netconn *
TNetConn xNetConn = netconn_new ( NETCONN_TCP );   

netconn_bind   ( xNetConn, IP_ADDR_ANY, cTcpPort );
netconn_listen ( xNetConn );

while ( true )
{
   TNetConn xNewConn;
   Int8     xErr = netconn_accept ( xNetConn, &xNewConn );

   if ( xErr != ERR_OK )
      continue;

   NetConnServeClient ( xNewConn );  // process client request
   netconn_delete     ( xNewConn );
}



(last changed: Sept. 26, 2011)

Advertisement