SWRU455M February 2017 – October 2020 CC3120 , CC3120MOD , CC3130 , CC3135 , CC3135MOD , CC3220MOD , CC3220MODA , CC3220R , CC3220S , CC3220SF , CC3230S , CC3230SF , CC3235MODAS , CC3235MODASF , CC3235MODS , CC3235MODSF , CC3235S , CC3235SF
UDP is a connectionless transport protocol. It does not require establishing a connection with a peer socket, and each packet is individually managed. However, the SimpleLink device lets the host application use UDP either as a connectionless or a connection-oriented protocol. In the connection-oriented mode, received packets with a different source than the connect source are dropped. In UDP there are no client and server sides. Both sides can initiate data exchange or wait for reception of data. In most applications one side waits for data reception and one side initiates the data exchange. The side that waits for data reception is considered as a server and the side that initiates the data exchange is considered as a client.
Figure 7-2 shows these two methods:
Example:
_i16 Sd;
_i16 Status;
SlSockAddrIn_t Addr;
_i8 SendBuf[] = "Hello World !!!";
_i8 RecvBuf[1460];
Sd = sl_Socket(SL_AF_INET, SL_SOCK_DGRAM, 0);
if( 0 > Sd )
{
// error
}
Addr.sin_family = SL_AF_INET;
Addr.sin_port = sl_Htons(5001);
Addr.sin_addr.s_addr = SL_INADDR_ANY;
Status = sl_Bind(Sd, ( SlSockAddr_t *)&Addr, sizeof(SlSockAddrIn_t));
if( Status )
{
// error
}
Addr.sin_family = SL_AF_INET;
Addr.sin_port = sl_Htons(5001);
Addr.sin_addr.s_addr = sl_Htonl(SL_IPV4_VAL(192,168,1,31));
Status = sl_SendTo(Sd, SendBuf, strlen(SendBuf), 0, (SlSockAddr_t*)&Addr,sizeof(SlSockAddr_t));
if( strlen(SendBuf) != Status )
{
// error
}
AddrSize = sizeof(SlSockAddrIn_t);
Status = sl_RecvFrom(Sd, RecvBuf, 1460, 0, ( SlSockAddr_t *)&Addr, &AddrSize);
if( 0 > Status )
{
// error
}
Status = sl_Close(Sd);
if( Status )
{
// error
}