- Open the TCP socket. Use family type: SL_AF_INET for IPv4, and SL_AF_INET6 for IPv6.
- Bind the source port. This step is optional for the client socket. If the sl_Bind API is not called, the SimpleLink device internally binds a random source port. Binding the port is performed in the same way a server socket binds a port (see the following example).
- Initiate a connection to the server. The TCP IPv6 client can also connect to the IPv4 server. In this case, when the IPv6 socket is connecting to the IPv4 server, the IPv4 destination address is mapped to IPv6 format (for example, ::00:ffff:ipv4).
- Send and receive the data.
- Close the socket. By default the sl_Close API returns immediately and the close process is done internally. There are two ways to confirm that all the data was transmitted and the socket closed gracefully:
- By default: the sl_Close API returns immediately, while the close process is done internally. The socket is closed only after all queued packets successfully transmit. If the device failed to transmit all queued packets, the host application is notified through an asynchronous error event (SL_SOCKET_TX_FAILED_EVENT).
- The common option in BSD: use the SO_LINGER option. When a socket is set as linger, the sl_Close API does not return until all queued packets successfully transmit, or earlier if the linger configured time-out expires with an appropriate error indication.
Example:
_i16 Status;
_i16 Sd;
SlSockAddrIn_t Addr;
_i8 SendBuf[] = "Hello World !!!";
_i8 RecvBuf[1460];
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));
Sd = sl_Socket(SL_AF_INET, SL_SOCK_STREAM, 0);
if( 0 > Sd )
{
// error
}
Status = sl_Connect(Sd, ( SlSockAddr_t *)&Addr, sizeof(SlSockAddrIn_t));
if( Status )
{
// error
}
Status = sl_Send(Sd, SendBuf, strlen(SendBuf), 0 );
if( strlen(SendBuf) != Status )
{
// error
}
Status = sl_Recv(Sd, RecvBuf, 1460, 0);
if( 0 > Status )
{
// error
}
Status = sl_Close(Sd);
if( Status )
{
// error
}