nRF5 SDK v14.2.0
TCP Client
This information applies to the following SoftDevice: S132

This example demonstrates how the BSD Socket Interface can be used to send data to a remote TCP port. Remote port number 9000 is assumed in this example. Request and response formats used for this example are described in sections TCP Request Format and TCP Response Format, respectively.

The client will attempt to create a socket and connect to the remote server until successful. Once connected, it will send and receive sequence numbers from 0 to 100, using the same format as the Client example.

Once a packet is sent, blocking I/O is used to receive a packet, waiting until successfully received. The TCP server could be a PC application communicating to the TCP client on the kit, or another development kit running the Server example.

Since this example can run on a PC, it is not using any LEDs or buttons on the nRF5 DK.

Running the example from PC

The example can also be compiled on a Linux host to demonstrate the portability of the BSD Socket Interface. Simply run

make -f Makefile.unix

to compile it.

Common Modules Dependency and Usage

This section summarizes the usage of nRF5 resources and common modules in the examples apart from the IoT 6LoWPAN and lwIP stack library.

Module Inclusion/Usage Description
Timer 1 Timer for lwIP.
Button No No buttons are used.
LEDs No No LEDs are used.
Adv Data Encoder Yes The device name used is 'TCP_Socket_Client', IPSP Service UUID is included in the UUID list.
Scheduler Yes Scheduler is used for processing stack events.
Note
The lwIP library used for this example is under BSD-style license; this is different from the Nordic SDK license. The license text can be found at <InstallFolder>external/lwip/license.txt

Setup

You can find the source code and the project file of the example in the following folder: <InstallFolder>\examples\iot\socket\tcp\client

Testing

See Connecting devices to the router for a list of relevant Linux commands.

  1. Compile and program the application.
  2. Run a TCP Server to listen on TCP port number 9000.
  3. Prepare the Linux router device by initializing the 6LoWPAN module.
  4. Discover the advertising device by using the hcitool lescan command.
  5. Connect to the discovered device from the Linux console by using the Bluetooth 6LoWPAN connect command.
  6. Assign a static address to the interface, the same one as used in the example, by using the Assign a static address command.
  7. Run the Wireshark or hcidump program and observe the btX interface.
  8. On succesful TCP connection, observe data is sent from the kit to the server in the format specified in TCP Request Format.
  9. If the TCP server responds to the client with data format specified TCP Response Format, observe more data is sent and the sequence number is incremented.

Python TCP Server Example

The following is a python server example that listens on port 9000 and sends back responses for requests received on the port. Request and response structure are according to the format specified in sections TCP Request Format and TCP Response Format respectively.

import socket
import struct
SERVER_ADDR = ''
SERVER_PORT = 9000
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
server_addr = (SERVER_ADDR, SERVER_PORT, 0, 0)
sock.bind(server_addr)
sock.listen(1)
try:
client, addr = sock.accept()
print 'Connected'
while 1:
recv_data = client.recv(128)
recv_len = len(recv_data)
if recv_len >= 8:
rx_sequence_number = 0
rx_sequence_number = struct.unpack("!I", recv_data[:4])[0]
data = struct.pack("!i", (rx_sequence_number))
data += 'Pong'
client.send(data)
except KeyboardInterrupt:
print ("Exit!")
finally:
sock.close()
del sock

Documentation feedback | Developer Zone | Subscribe | Updated