SWRU368C May 2018 – January 2021 CC3100 , CC3100MOD , CC3200 , CC3200MOD
The server side of the socket is identical to the client side.
SockID = sl_Socket(SL_AF_INET,SL_SOCK_DGRAM, 0);
Similar to TCP, bind the socket to the local address. No listening is required as UDP is connectionless.
#define PORT_NUM 5001
SlSockAddrIn_t LocalAddr;
AddrSize = sizeof(SlSockAddrIn_t);
TestBufLen = BUF_SIZE;
LocalAddr.sin_family = SL_AF_INET;
LocalAddr.sin_port = sl_Htons((UINT16) PORT_NUM);
LocalAddr.sin_addr.s_addr = 0;
Status = sl_Bind(SockID, (SlSockAddr_t *) &LocalAddr, AddrSize);
The socket now tries to receive information on the socket. If the user did not specify the socket option as nonblocking, this command is blocked until an amount of BUF_SIZE of data is received. The fifth parameter specifies the source address sending the data.
#define BUF_SIZE 1400
SlSockAddrIn_t Addr;
char RecvBuf[BUF_SIZE];
Status = sl_RecvFrom(SockID, RecvBuf, BUF_SIZE, 0, (SlSockAddr_t *) &Addr, (SlSocklen_t*) &AddrSize );
Close the socket once communication is finished.
sl_Close(SockID);