![](http://datasheet.mmic.net.cn/Microchip-Technology/MCW1001AT-I-SS_datasheet_98973/MCW1001AT-I-SS_24.png)
MCW1001A
DS70671A-page 24
Preliminary
2011 Microchip Technology Inc.
5.2
UDP
UDP is a standard transport layer protocol described in
Request for Comments (RFC) 768. It provides fast but
unreliable data-gram based transfers over networks,
and forms the foundation SNTP, SNMP, DNS, and
many other protocol standards. Connections over UDP
should be thought of as data-gram based transfers.
Each packet is a separate entity, the application should
expect some packets to arrive out-of-order or even fail
to reach the destination node. This is in contrast to
TCP, in which the connection is thought of as a stream
and network errors are automatically corrected. These
tradeoffs in reliability are made for an increase in
throughput. In general, UDP transfers operate two to
three times faster than those made over TCP. As UDP
is packet-oriented, each packet must be dealt with in its
entirety by your application before returning to the main
stack loop. When a packet is received, your application
will be called to handle it. This packet will no longer be
available the next time your application is called.
Therefor, you must either perform all necessary
processing or copy the data elsewhere before
returning. When transmitting a packet, your application
must build and transmit the complete packet in one
cycle. The following example illustrates how to use the
TCP packets.
EXAMPLE 5-2:
USING UDP PACKETS
The
MCW1001A
UDP
sockets
require
less
configuration than the TCP counterparts. Sending
packets using UDP sockets can be accomplished by
creating the socket and issuing a Sendto() command
specifying the destination address and port number.
Receiving packets on a UDP socket requires an extra
bind step to set the local port number.
5.2.1
MCW1001A UDP SERVER
EXAMPLE
:
1.
Create a UDP socket with a call to socket
specifying the UDP socket type. If successful,
an 8-bit socket handle will be returned.
2.
Bind the socket to a local port number with a call
to bind()
3.
Receive data using this socket with a call to
recvfrom()
4.
Optionally reply to the client with a call to
Sendto() specifying the returned address and
port number returned in the recvfrom() call.
5.2.2
MCW1001A UDP CLIENT
EXAMPLE
:
1.
Create the UDP socket with a call to socket
specifying the UDP socket type. If successful,
an 8 bit socket handle will be returned.
2.
Transmit data to a remote UDP server with a call
to Sendto() specifying the remote IP address
and port number.
3.
Optionally receive data from the server using
this socket with a call to recvfrom().