nRF5 IoT SDK  v0.9.0
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
TCP Client

Overview

This example demonstrates how the BSD Socket Interface can be 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 devkit running the Server example.

Since this example can run on a PC, it is not using any LEDs or buttons on the nRF5x 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.linux

to compile it.

Common Modules Dependency and Usage

This section summarizes the usage of nRF5x 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 buttons are used.
Adv Data Encoder Yes The device name used is 'TCP_Socket_Client', IPSP Service UUID is included in the UUID list.
Scheduler No Scheduler is not used for processing stack events.
UART Trace Included not enabled Tracing is included but not enabled by default.
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

The source code and project file of the example can be found at: <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. Observe that the advertising LED is lit.
  2. Prepare the Linux router device by initializing the 6LoWPAN module.
  3. Discover the advertising device by using the hcitool lescan command.
  4. Connect to the discovered device from the Linux console by using the Bluetooth 6LoWPAN connect command.
  5. Run the Wireshark or hcidump program and observe the btX interface.
  6. Use a TCP Server to listen on TCP port number 9000.
  7. On succesful TCP connection, observe data is sent from the kit to the server in the format specified in TCP Request Format.
  8. 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

Below 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
#This example assumes the server's random static BD address to be C0:AA:BB:CC:DD:EE and the global prefix used to be 2004::/64.
#Therefore the global IPv6 address of the server will be 2004::C0AA:BBFF:FECC:DDEE.
SERVER_ADDR = '2004::C0AA:BBFF:FECC:DDEE'
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)
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)
sock.close()
del sock