esp-idf/examples/protocols/sockets/tcp_client
David Cermak b2af4d9689 lwip/linux: Add lwip support for networking component under linux
linux/lwip: Wrap some IO posix functions
* to workaourd the FreeRTOS EINTR issue (when building without lwip)
* to correctly choose the sub-system based on fd (when building with
lwip) -- passing control to either linux/system or to lwip
This commit also addapts tapio-if to provide DHCP client by default and
configurable settings for static IP
2023-05-05 05:03:39 +00:00
..
main lwip/linux: Add lwip support for networking component under linux 2023-05-05 05:03:39 +00:00
CMakeLists.txt lwip/linux: Add lwip support for networking component under linux 2023-05-05 05:03:39 +00:00
pytest_tcp_client.py CI: enable more wifi cases 2023-01-06 06:03:52 +00:00
README.md examples: enable build esp32c6 for wifi and network examples 2023-01-12 14:50:06 +08:00
sdkconfig.ci CI: update test cases to use different environment variables 2022-07-15 01:16:35 +08:00
sdkconfig.defaults tcp_client example: support for Linux target. 2022-08-16 23:48:18 +05:30

Supported Targets ESP32 ESP32-C2 ESP32-C3 ESP32-C6 ESP32-S2 ESP32-S3 Linux

TCP Client example

(See the README.md file in the upper level 'examples' directory for more information about examples.)

The application creates a TCP socket and tries to connect to the server with predefined IP address and port number. When a connection is successfully established, the application sends message and waits for the answer. After the server's reply, application prints received reply as ASCII text, waits for 2 seconds and sends another message.

Configure the project

This example can be configured to run on ESP32 and Linux target to communicate over IPv4 and IPv6.

idf.py menuconfig

Set following parameters under Example Configuration Options:

  • Set IP version of example to be IPV4 or IPV6.

  • Set IPV4 Address in case your chose IP version IPV4 above.

  • Set IPV6 Address in case your chose IP version IPV6 above.

    • For IPv6 there's an additional option for Interface selection.
    • Enter the name of the interface to explicitely establish communication over a specific interface.
    • On selecting Auto the example will find the first interface with an IPv6 address and use it.
  • Set Port number that represents remote port the example will connect to.

Configure Wi-Fi or Ethernet under "Example Connection Configuration" menu. See "Establishing Wi-Fi or Ethernet Connection" section in examples/protocols/README.md for more details.

Note: please replace 192.168.0.167 3333 with desired IPV4/IPV6 address (displayed in monitor console) and port number in the following command.

How to use example:

In order to create TCP server that communicates with TCP Client example, choose one of the following options.

There are many host-side tools which can be used to interact with the UDP/TCP server/client. One command line tool is netcat which can send and receive many kinds of packets.

Ref to the upper level README for more information.

TCP server using netcat

nc -l 192.168.0.167 3333

Hardware Required

This example can be run on any commonly available ESP32 development board. This example can also run on any Linux environment.

Build and Flash

Build the project and flash it to the board, then run monitor tool to view serial output:

idf.py -p PORT flash monitor

(To exit the serial monitor, type Ctrl-].)

See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.

Troubleshooting

Start server first, to receive data sent from the client (application).