SWRU368C May 2018 – January 2021 CC3100 , CC3100MOD , CC3200 , CC3200MOD
The first step is to create a socket. The returned socket handler is the most important element of the application. Networking does not work without the returned socket handler.
int SockID;
SockID = sl_Socket(SL_AF_INET, SL_SOCK_STREAM, 0);
SL_AF_INET indicates using IPv4 and SL_SOCK_STREAM indicates using TCP. Definitions for both values are in the socket.h header file. The example sets 0 in the third parameter to select a default protocol from the selected domain and type. More details can be found in the online documentation. Definitions of some structures and constants are located in the socket.h header file inside the SDK.
As a TCP client, the application executes sl_Connect() to connect to a server. The server implementation can be found below.
/* IP addressed of server side socket. Should be in long format,
* E.g: 0xc0a8010a == 192.168.1.10 */
#define IP_ADDR 0xc0a80168
int Status;
int Port = 5001;
SlSockAddrIn_t Addr;
Addr.sin_family = SL_AF_INET;
Addr.sin_port = sl_Htons((UINT16)Port);
Addr.sin_addr.s_addr = sl_Htonl((UINT32)IP_ADDR);
Status = sl_Connect(SockID, ( SlSockAddr_t *) &Addr, sizeof(SlSockAddrIn_t));
Addr specifies destination address and relevant information. Because struct type SlSockAddr is generic, use SlSockAddrIn_t to fill the details and cast it into SlSockAddr. Upon successful connection, the SockID socket handler is ready to perform data exchange.
The sl_Send() and sl_Recv() functions can be used for data exchange. Define the buffer size.
#define BUF_SIZE 1400
char SendBuf[BUF_SIZE];
/* Write data to your buffer*/
<write buffer action>
Status = sl_Send(SockID, SendBuf, BUF_SIZE, 0 );
char RecvBuf[BUF_SIZE];
Status = sl_Recv(SockID, RecvBuf, BUF_SIZE, 0);
Upon completion, close the socket with sl_Close() to allow the remaining applications to reuse the resource.
sl_Close(SockID);