![Texas Instruments CC3220 Скачать руководство пользователя страница 84](http://html.mh-extra.com/html/texas-instruments/cc3220/cc3220_programmers-manual_1094609084.webp)
Socket Working Flow
84
SWRU455A – February 2017 – Revised March 2017
Copyright © 2017, Texas Instruments Incorporated
Socket
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
}
6.5.1.2
Server Side
1. Open the TCP socket. Use family type: SL_AF_INET for IPv4, and SL_AF_INET6 for IPv6. The socket
is the public socket of the server.
2. Bind the public port of the server. The host application must set a specific port for the server to allow
clients to connect.
3. Listen. This stage marks the socket as a server socket. Here an additional socket is allocated for this
specific server socket to reserve a socket for the next client connection (from this point the server
socket is ready to accept new connections even if the host still did not call to sl_Accept).
4. Accept a client connection. This step extracts a connection request from the queue of pending
connections on the server socket, and creates a new connected socket for data exchange between the
server and the client side. The original public socket is not affected by this call, and an additional
accept could be called on the public socket to accept additional clients. Each newly created client
decreases the number of available sockets in the system by one. IPv6 server sockets bound to any
interface, can accept IPv6 and IPv4 clients. When accepting IPv4 clients, the returned client IP
address is IPv4 mapped to IPv6 format (for example, :00:ffff:ipv4).
5. Send and receive the data. Use the client socket descriptor to send and receive data. This step is done
in the same way as in a client socket.
6. Close the data socket. To close a connection with a specific client, the close operation should be called