IP services
Serial Communication Service
XBee Wi-Fi RF Module User Guide
68
TCP
When
is configured for Transmission Control Protocol (TCP), only one connection is
allowed at a time. If a transmission is attempted while a TCP connection exists, the data is sent on
that connection, ignoring the
and
parameters. This
connection can be initiated by a local host or by a network client.
A local host initiates a connection by sending data to the serial port. A connection is created based on
the
DL (IP address)
and
DE
commands. However, if
DL
is a broadcast address, then UDP is used,
ignoring the TCP configuration.
A network client establishing a TCP connection to the XBee Wi-Fi RF Module uses the port defined by
. When established, any data sent by the local host does not create a new connection
based on
DL
and
DE
, but rather uses the existing connection.
API mode
API mode allows you to specify the protocol (UDP or TCP) destination address, destination port, and
source port for transmission.
UDP mode
If you specify UDP mode in the
Transmit (TX) Request: IPv4 - 0x20
, no connection is made to the
destination address and port. Instead, the data is packetized and sent directly, providing the source
port matches the local port specified by
C0 (Serial Communication Service Port)
.
TCP mode
In API mode, multiple TCP connections are allowed simultaneously. A TCP connection is fully defined
by these four entities:
n
Local IP address
n
Local port number
n
Remote IP address
n
Remote port number
When an
Transmit (TX) Request: IPv4 - 0x20
is sent to the device, it specifies a destination address and
port. To send data on an existing TCP connection, the destination address and port given in the API
frame must match the remote address and port of an existing TCP connection. The search for a
matching connection ignores the source port number given in the API frame. This means that only one
TCP connection is allowed per remote port.
The source port matters in the event that a matching TCP connection is not found. If it is 0, then an
attempt is made to create a new connection prior to sending the data. If not, the data is dropped with
an error.
For purposes of the following discussion, a client requests a TCP connection of a server and a server
accepts a TCP connection request from a client.
As a client, the best strategy is to specify a source port of 0 and a destination port to match the
listening socket [
C0 (Serial Communication Service Port)
] of the receiving device or the server port for
any other network device. This way, if a connection is not found, a new one is created.
As a server, the best strategy is to swap the source and destination ports found in the IPv4 receive
packet and place them in the response, which is an IPv4 transmit packet. This allows the response to
be sent back on the same socket as the received data. If the data is sent to the listening socket of the
other device rather than to the source socket given in the IPv4 receive packet, then an extra socket is
created. While this still works, it unnecessarily creates an extra socket connection.