![](http://datasheet.mmic.net.cn/Microchip-Technology/MCW1001AT-I-SS_datasheet_98973/MCW1001AT-I-SS_23.png)
2011 Microchip Technology Inc.
Preliminary
DS70671A-page 23
MCW1001A
5.0
TRANSMITTING AND
RECEIVING PACKETS
5.1
TCP
TCP is a standard transport layer protocol described in
Request for Comments (RFC) 793. It provides reliable
stream-based connections over unreliable networks,
and forms the foundation for HTTP, SMTP, and many
other protocol standards. Connections made over TCP
guarantee data transfer at the expense of throughput.
Connections are made through a three-way handshake
process, ensuring a one-to-one connection. Remote
nodes advertise how much data they are ready to
receive,
and
all
data
transmitted
must
be
acknowledged. If a remote node fails to acknowledge
the receipt of data, it is automatically retransmitted.
This ensures that network errors such as lost,
corrupted, or out-of-order packets are automatically
corrected. To accomplish this, TCP must operate in a
buffer. Once the transmit buffer is full, no more data can
be sent until the remote node has acknowledged
receipt. Likewise, the remote node cannot transmit
more data until the local device has acknowledged
receipt and that space is available in the buffer. The
following example illustrates how to use TCP packets:
EXAMPLE 5-1:
USING TCP PACKETS
The MCW1001A TCP sockets can be configured as
servers or clients. The procedure for transferring TCP
packet data in each configuration differs slightly.
5.1.1
MCW1001A TCP SERVER
EXAMPLE
:
1.
Create a TCP socket with call to socket(). This
will return an 8-bit socket handle to be used
when later referencing this socket.
2.
Bind the socket to the listen port with call to
bind(). The bind API specifies the 16-bit local
port number that the client will attempt a
connection on.
3.
Prepare the socket to listen for a connection with
call to listen(). The listen process opens the
specified number of listening sockets that
become the children sockets of the bound
parent. They will all listen to the port specified in
the bind process, but they may accept
connections from different clients (different IP
addresses, different remote port numbers). If the
number of sockets specified is not available, the
listen process will return the number of sockets
it was not able to allocate.
4.
Accept incoming connection through a call to
accept(). Accept is non-blocking for the
MCW1001A and accepting a connection from a
client must be done through polling the socket
with the Accept API.
5.
Communicate with the remote host using send()
and recv().
6.
Close the socket with call to close().
5.1.2
MCW1001A TCP CLIENT EXAMPLE
:
1.
Create a TCP socket with call to socket().
2.
Optionally apply a local port number with a call
to bind(). Otherwise, a pseudo-random local port
number will be assigned.
3.
Connect to the server with call to connect(),
passing sockaddr, IP address.
4.
Communicate with the remote host using send()
and recv().
5.
Close the socket with call to close().