Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages

Asynchronous bind function associates the provided address and local port to the socket.

The function can be used with both TCP and UDP sockets. It is mandatory to call the bind function before starting any UDP or TCP server operation. Upon socket bind completion, the application will receive a SOCKET_MSG_BIND message in the socket callback.

Functions

NMI_API sint8 bind (SOCKET sock, struct sockaddr *pstrAddr, uint8 u8AddrLen)
 

NMI_API sint8 bind ( SOCKET  sock,
struct sockaddr pstrAddr,
uint8  u8AddrLen 
)
Asynchronous bind function associates the provided address and local port to the socket.
The function can be used with both TCP and UDP sockets. It is mandatory to call the @ref bind function before starting any UDP or TCP server operation.
Upon socket bind completion, the application will receive a @ref SOCKET_MSG_BIND message in the socket callback.
Parameters
[in]sockSocket ID, must hold a non negative value. A negative value will return a socket error SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in.
[in]pstrAddrPointer to socket address structure "sockaddr_in" sockaddr_in
[in]u8AddrLenSize of the given socket address structure in bytes.
Precondition
The socket function must be called to allocate a socket before passing the socket ID to the bind function.
See Also
socket
connect
listen
accept
recv
recvfrom
send
sendto
Returns
The function returns ZERO for successful operations and a negative value otherwise. The possible error values are:

Example

This example demonstrates the call of the bind socket operation after a successful socket operation.

SOCKET udpServerSocket =-1;
int ret = -1;
if(udpServerSocket == -1)
{
if(udpServerSocket >= 0)
{
addr.sin_port = _htons(UDP_SERVER_PORT);
ret = bind(udpServerSocket,(struct sockaddr*)&addr,sizeof(addr));
if(ret == 0)
printf("Bind success!\n");
else
{
printf("Bind Failed. Error code = %d\n",ret);
close(udpServerSocket);
}
else
{
printf("UDP Server Socket Creation Failed\n");
return;
}
}
Parameters
[in]sockSocket ID, must hold a non negative value. A negative value will return a socket error SOCK_ERR_INVALID_ARG. Indicating that an invalid argument is passed in.
[in]pstrAddrPointer to socket address structure sockaddr_in.
[in]u8AddrLenSize of the given socket address structure in bytes.
Precondition
The socket function must be called to allocate a socket before passing the socket ID to the bind function.
See Also
socket
connect
listen
accept
recv
recvfrom
send
sendto
Returns
The function returns ZERO for successful operations and a negative value otherwise. The possible error values are:

Example

This example demonstrates the call of the bind socket operation after a successful socket operation.

SOCKET udpServerSocket =-1;
int ret = -1;
if(udpServerSocket == -1)
{
udpServerSocket = socket(AF_INET, SOCK_DGRAM, 0);
if(udpServerSocket >= 0)
{
addr.sin_port = _htons(1234);
ret = bind(udpServerSocket,(struct sockaddr*)&addr,sizeof(addr));
if(ret != 0)
{
printf("Bind Failed. Error code = %d\n",ret);
close(udpServerSocket);
}
}
else
{
printf("UDP Server Socket Creation Failed\n");
return;
}
}

References m2m_memcpy(), MAX_SOCKET, NULL, tstrBindCmd::sock, SOCK_ERR_INVALID, SOCK_ERR_INVALID_ARG, SOCK_ERR_NO_ERROR, SOCKET_CMD_BIND, SOCKET_CMD_SSL_BIND, SOCKET_REQUEST, SSL_FLAGS_ACTIVE, tstrBindCmd::strAddr, tstrSockAddr::u16Family, tstrSockAddr::u16Port, tstrSocket::u16SessionID, tstrBindCmd::u16SessionID, and tstrSockAddr::u32IPAddr.

Referenced by demo_start(), IperfTCP_ServerStart(), IperfUDP_ServerStart(), main(), run_udp_server(), setup_android_sockets(), and wifi_ap_provision().