WINC1500 IoT Software APIs: recvfrom

WINC1500 IoT Software API

WINC1500 IoT Software APIs  19.5.2
WINC Software API Reference Manual

Functions

NMI_API sint16 recvfrom (SOCKET sock, void *pvRecvBuf, uint16 u16BufLen, uint32 u32Timeoutmsec)
 

Detailed Description

Receives data from a UDP Socket.

The asynchronous recvfrom function is used to retrieve data from a UDP socket. The socket must already be bound to a local port before a call to the recvfrom function is made (i.e message SOCKET_MSG_BIND is received with successful status in the socket callback).

Upon calling the recvfrom function with a successful return code, the application is expected to receive a notification in the socket callback whenever a message is received through the SOCKET_MSG_RECVFROM event.

Receiving the SOCKET_MSG_RECVFROM message in the callback with zero, indicates that the socket is closed. Whereby a negative buffer length indicates one of the socket error codes such as socket timeout error :

The recvfrom callback can also be used to show the IP address of the remote host that sent the frame by using the "strRemoteAddr" element in the tstrSocketRecvMsg structure. (refer to the code example)

Function Documentation

◆ recvfrom()

NMI_API sint16 recvfrom ( SOCKET  sock,
void *  pvRecvBuf,
uint16  u16BufLen,
uint32  u32TimeoutSeconds 
)
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]pvRecvBufPointer to a buffer that will hold the received data. The buffer shall be used in the recv callback to deliver the received data to the caller. The buffer must be resident in memory (heap or global buffer).
[in]u16BufLenThe buffer size in bytes.
[in]u32TimeoutSecondsTimeout for the recv function in milli-seconds. If the value is set to ZERO, the timeout will be set to infinite (the recv function waits forever).
Precondition
  • The socket function must be called to allocate a UDP socket before passing the socket ID to the recvfrom function.
  • The socket corresponding to the socket ID must be successfully bound to a local port through the call to a bind function.
See also
socket bind close
Returns
The function returns ZERO for successful operations and a negative value otherwise. The possible error values are:

Example

The example demonstrates a code snippet for the calling of the recvfrom function in the socket callback upon notification of a successful bind event, and the parsing of the received data when the SOCKET_MSG_RECVFROM event is received.

switch(u8Msg)
{
{
tstrSocketBindMsg *pstrBind = (tstrSocketBindMsg*)pvMsg;
if(pstrBind != NULL)
{
if(pstrBind->status == 0)
{
recvfrom(sock, gau8SocketTestBuffer, TEST_BUFFER_SIZE, 0);
}
else
{
M2M_ERR("bind\n");
}
}
}
break;
{
if(pstrRx->s16BufferSize > 0)
{
//get the remote host address and port number
uint16 u16port = pstrRx->strRemoteAddr.sin_port;
uint32 strRemoteHostAddr = pstrRx->strRemoteAddr.sin_addr.s_addr;
printf("Recieved frame with size = %d.\tHost address=%x, Port number = %d\n\n",pstrRx->s16BufferSize,strRemoteHostAddr, u16port);
ret = recvfrom(sock,gau8SocketTestBuffer,sizeof(gau8SocketTestBuffer),TEST_RECV_TIMEOUT);
}
else
{
printf("Socet recv Error: %d\n",pstrRx->s16BufferSize);
ret = close(sock);
}
}
break;
default:
break;
}
}
Generated on Thu Jan 26 2017 22:15:21 for WINC1500 IoT Software APIs by   doxygen 1.8.13