Multiple TCP Clients

This application uses the Gecko OS TCP client command to open 4 simultaneous TCP connections to a TCP echo server on the internet (Gecko OS actually supports up to 8 simultaneous connections).

The TCP echo server, located on the internet at test.zentri.com:50007 , simply echoes all received data back to the Gecko OS TCP client. Once each connection is established, stream_write is used to send data to each connection stream in turn. With all data sent, stream_poll is used to check for received data on connection streams, and stream_read is used to read data waiting on a stream.

You will only need one Silabs evaluation board for this example.


Connecting

Let's get started by connecting your board to a Wi-Fi network and opening each of the 4 TCP connection streams in turn. You can simply cut and paste the commands in the following table, but DON'T FORGET to replace your Wi-Fi network name and password for the defaults.

Gecko OS Commands Description

set wlan.ssid    YOUR_AP_NAME
set wlan.passkey YOUR_AP_PASSWORD
tcp_client test.zentri.com 50007
tcp_client test.zentri.com 50007
tcp_client test.zentri.com 50007
tcp_client test.zentri.com 50007

<- Set the name of your Wi-Fi AP
<- Set the password for your Wi-Fi AP
<- Open TCP client session 0
<- Open TCP client session 1
<- Open TCP client session 2
<- Open TCP client session 3

There is no need to bring up the WLAN network interface prior to opening a TCP connection, the TCP client (just like all commands that need the network) automatically connects to the network if a connection has not already been established.

An example showing what the session looks like if you entered the above commands successfully is shown below. Notice that each new TCP connection is assigned a stream handle, starting at 0 for the first TCP connection.

Session log when opening 4 TCP client connections

> tcpc test.zentri.com 50007
[2014-05-01 | 12:34:45: Associating to YOUR_AP_NAME]
Obtaining IPv4 address via DHCP
IPv4 address: 192.168.0.79
[2014-05-01 | 12:34:48: Associated]
Resolving host: test.zentri.com
[2014-05-01 | 12:34:48: Opening: test.zentri.com:50007]
Connecting (TCP): test.zentri.com:50007
[2014-05-01 | 12:34:48: Opened: 0]
0
> tcpc test.zentri.com 50007
Resolving host: test.zentri.com
[2014-05-01 | 12:34:49: Opening: test.zentri.com:50007]
Connecting (TCP): test.zentri.com:50007
[2014-05-01 | 12:34:49: Opened: 1]
1
> tcpc test.zentri.com 50007
Resolving host: test.zentri.com
[2014-05-01 | 12:34:49: Opening: test.zentri.com:50007]
Connecting (TCP): test.zentri.com:50007
[2014-05-01 | 12:34:49: Opened: 2]
2
> tcpc test.zentri.com 50007
Resolving host: test.zentri.com
[2014-05-01 | 12:34:50: Opening: test.zentri.com:50007]
Connecting (TCP): test.zentri.com:50007
[2014-05-01 | 12:34:50: Opened: 3]
3

Check the status of open streams before continuing. Use the stream_list command to get a list of open streams. The number in brackets appended to each line is the local port used for the TCP connection stream.

> stream_list
!# Type  Info
#0 TCPC  test.zentri.com:50007 (41713)
#1 TCPC  test.zentri.com:50007 (46714)
#2 TCPC  test.zentri.com:50007 (51715)
#3 TCPC  test.zentri.com:50007 (56716)

Writing Data

Writing data to a stream is easy using the stream_write command. Copy and paste the following commands to write data to each open stream.

Gecko OS Commands Description

stream_write 0 23
Hello from stream 0

stream_write 1 23
Hello from stream 1

stream_write 2 23
Hello from stream 2

stream_write 3 23
Hello from stream 3


<- Prepare to write 32-characters to TCP connection stream 0
<- Data to write

<- Prepare to write 32-characters to TCP connection stream 1
<- Data to write

<- Prepare to write 32-characters to TCP connection stream 2
<- Data to write

<- Prepare to write 32-characters to TCP connection stream 3
<- Data to write

Checking for Data

To check for data on any stream (or all streams at once), use the stream_poll command as shown in the following session. If you don't want to poll for data, try assigning a GPIO to a stream to indicate when data is available. The tcp_client command describes how to assign a GPIO.

Checking for data

> stream_poll 0
1
> stream_poll 2
1
> stream_poll all
0,1|1,1|2,1|3,1|

Reading Data

To read available data from any stream, use the stream_read command as shown in the following session.

Checking for data

> stream_read 0 100
Hello from stream 0

> stream_read 1 100
Hello from stream 1

> stream_read 2 100
Hello from stream 2

> stream_read 3 100
Hello from stream 3

Change Log

Modified Changes Gecko OS Version
Required
2014-May-08 Created 1.1+