The OpenThread TCP APIs may be invoked via the OpenThread CLI.
Form a network with at least two devices.
On node 1, initialize the TCP CLI module and listen for incoming connections using the example TCP listener.
> tcp init > tcp listen :: 30000
The ::
specifies the IPv6 Unspecified Address.
On node 2, initialize the TCP CLI module, connect to node 1, and send a simple message.
> tcp init > tcp connect fe80:0:0:0:a8df:580a:860:ffa4 30000 > tcp send hello
After running the tcp connect
command on node 2, you should see a printout on node 2 similar to below:
TCP: Connection established
In addition, you should also see a printout on node 1 similar to below:
Accepted connection from [fe80:0:0:0:8f3:f602:bf9b:52f2]:49152 TCP: Connection established
After running the tcp send
command on node 2, you should see a printout on node 1 similar to below:
TCP: Received 5 bytes: hello
For a more in-depth example, see this video.
Unceremoniously ends the TCP connection, if one exists, associated with the example TCP endpoint, transitioning the TCP endpoint to the closed state.
> tcp abort TCP: Connection reset Done
Transfers the specified number of bytes using the TCP connection currently associated with the example TCP endpoint (this TCP connection must be established before using this command).
> tcp benchmark run Done TCP Benchmark Complete: Transferred 73728 bytes in 7233 milliseconds TCP Goodput: 81.546 kb/s
Get the last result of TCP benchmark. If the benchmark is ongoing, it will show that benchmark is ongoing. This command is used for test scripts which automate the tcp benchmark test.
> tcp benchmark result TCP Benchmark Status: Ongoing Done > tcp benchmark result TCP Benchmark Status: Completed TCP Benchmark Complete: Transferred 73728 bytes in 7056 milliseconds TCP Goodput: 83.592 kb/s
Associates a name (i.e. IPv6 address and port) to the example TCP endpoint.
::
).> tcp bind :: 30000 Done
Establishes a connection with the specified peer.
If the connection establishment is successful, the resulting TCP connection is associated with the example TCP endpoint.
> tcp connect fe80:0:0:0:a8df:580a:860:ffa4 30000 Done TCP: Connection established
The address can be an IPv4 address, which will be synthesized to an IPv6 address using the preferred NAT64 prefix from the network data.
Note: The command will return
InvalidState
when the preferred NAT64 prefix is unavailable.
> tcp connect 172.17.0.1 1234 Connecting to synthesized IPv6 address: fdde:ad00:beef:2:0:0:ac11:1 Done
Deinitializes the example TCP listener and the example TCP endpoint.
> tcp deinit Done
List the TCP CLI commands.
> tcp help abort benchmark bind connect deinit help init listen send-message sendend stoplistening Done
Initializes the example TCP listener and the example TCP endpoint.
If “tls” is used, then the TLS protocol will be used for the connection (on top of TCP). When communicating over TCP between two nodes, either both should use TLS or neither should (a non-TLS endpoint cannot communicate with a TLS endpoint). The first two options, “linked” and “circular”, specify that TLS should not be used and specify a buffering strategy to use with TCP; two endpoints of a TCP connection may use different buffering strategies.
The behaviors of “linked” and “circular” buffering are identical, but the option is provided so that users of TCP can inspect the code to see an example of using the two buffering strategies.
> tcp init tls Done
Uses the example TCP listener to listen for incoming connections on the specified name (i.e. IPv6 address and port).
If no TCP connection is associated with the example TCP endpoint, then any incoming connections matching the specified name are accepted and associated with the example TCP endpoint.
::
).> tcp listen :: 30000 Done
Send data over the TCP connection associated with the example TCP endpoint.
> tcp send hello Done
Sends the “end of stream” signal (i.e., FIN segment) over the TCP connection associated with the example TCP endpoint. This promises the peer that no more data will be sent to it over this TCP connection.
> tcp sendend Done
Stops listening for incoming TCP connections using the example TCP listener.
> tcp stoplistening Done