Extended Socket example: TCP Listener

The following example demonstrates setting up a TCP listener on the XBee Cellular and interacting with incoming connections. It will open up a listener socket on a given port and then receive data from a client.

Note The module must either have a public IP or a be on a private network in order to be accessible as a server (listener).

Send a Socket Create frame

Note The XBee Cellular does not support incoming TLS sockets.

Field Value
Frame type

0x40 (Socket Create)

Frame ID 0x01
Protocol 0x01 (TCP)

Socket Create frame data:

7E 00 03 40 01 01 BD

Receive a Socket Create response

The response contains the socket ID assigned. This example assumes zero.

Field Value
Frame type

0xC0 (Socket Create Response)

Frame ID 0x01
Socket ID 0x00
Status 0x00 (Success)

Socket Create Response received from XBee:

7E 00 04 C0 01 00 00 3E

Designate the socket as a listener

The Socket Bind/Listen Frame takes the socket ID from the socket create response and a source port that the socket will then listen on. In this example port 10001 is used.

Field Value
Frame type

0x46 (Socket Listen)

Frame ID 0x01
Socket ID 0x00
Source Port 0x2711 (10001)

Socket Bind/Listen frame data:

7E 00 05 46 01 00 27 11 80

Receive a Socket Bind/Listen Response

The Socket Bind/Listen Response contains a Status. A Status of zero is a success and any other value is an error.

Field Value
Frame type

0xC6 (Socket Listen)

Frame ID 0x01
Socket ID 0x00
Status 0x00 (Success)

Socket Bind/Listen frame received from XBee:

7E 00 04 C6 01 00 00 38

Making a connection to the listener socket

The IP of the XBee can be acquired through the MY AT command.

ATMY
172.20.1.235

Using an external tool like netcat, a connection can be made to the given address.

nc -p 10001 172.20.1.235 10001
Hello XBee!

After the connection has been made, the XBee outputs a Socket New IPv4 Client frame indicating the presence of a new client connection. It contains the listener's socket ID and the new Client Socket ID along with the connection's remote address information.

Field Value
Frame type

0xCC (Socket New IPv4 Client)

Socket ID 0x00
Client Socket ID 0x01
Remote Address 0x0A 0x0A 4A 9D
Remote Port 0x27 0x11

Socket New IPv4 Client frame:

7E 00 09 CC 00 01 0A 0A 4A 9D 27 11 FF

Note XBee Cellular Cat-1 variants require data to be sent before the connection is presented. Other variants present the connection as soon as it is made.

Receiving Data from the new socket

After the connection is established, data received from the new socket is contained in a Socket Receive frame just like any other TCP socket.

Field Value
Frame type

0xCD (Socket Status)

Frame ID 0x01
Socket ID 0x01
Status 0x00
Payload

Hello XBee!

Receive Data indicating data from remote TCP peer:

7E 00 10 CD 00 01 00 48 65 6C 6C 6F 20 58 42 65 65 21 0A 8E

Receive a Socket Status indicating closed connection

You may close the client socket remotely which elicits a Socket Status with a Status of 0x07.

Field Value
Frame type

0xCF (Socket Status)

Socket ID 0x01
Status 0x07 (Connection lost)

Socket Status received from XBee indicating connection lost:

7E 00 03 CF 01 07 28

When a Socket Status indicating a connection close is received, the socket ID will have been de-allocated by the XBee and no further operations are possible or necessary using that ID.