SOCKET clientSocketHdl;
uint8 rxBuffer[256], acSendBuffer[256];
/* Socket event handler */
void udpClientSocketEventHandler(SOCKET sock, uint8 u8Msg, void * pvMsg)
{
if((u8Msg == SOCKET_MSG_RECV) || (u8Msg == SOCKET_MSG_RECVFROM))
{
tstrSocketRecvMsg *pstrRecvMsg = (tstrSocketRecvMsg*)pvMsg;
if((pstrRecvMsg->pu8Buffer != NULL) && (pstrRecvMsg->s16BufferSize > 0))
{
uint16 len;
// Format a message in the acSendBuffer and put its length in len
sendto(clientSocketHdl, acSendBuffer, len, 0,
(struct sockaddr*)&strAddr, sizeof(struct sockaddr_in));
recvfrom(clientSocketHdl, rxBuffer, sizeof(rxBuffer), 0);
// Close the socket after finished
close(clientSocketHdl);
}
}
}
/* This function needs to be called from main function. For the callbacks to be invoked correctly, the API m2m_wifi_handle_events must be called continuously from main.*/
void udpClientStart(char *pcServerIP)
{
struct sockaddr_in strAddr;
// Initialize the socket layer.
socketInit();
// Register socket application callbacks.
registerSocketCallback(udpClientSocketEventHandler, NULL);
clientSocketHdl = socket(AF_INET,SOCK_DGRAM,u8Flags);
if(clientSocketHdl >= 0)
{
uint16 len;
strAddr.sin_family = AF_INET;
strAddr.sin_port = _htons(1234);
strAddr.sin_addr.s_addr = nmi_inet_addr(pcServerIP);
// Format some message in the acSendBuffer and put its length in len
sendto(clientSocketHdl, acSendBuffer, len, 0, (struct sockaddr*)&strAddr,
sizeof(struct sockaddr_in));
recvfrom(clientSocketHdl, rxBuffer, sizeof(rxBuffer), 0);
}
}