Skip to content

ESP32 TCP Client Connection Refused

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about ESP32 TCP Client Connection Refused. We cover key concepts, practical examples, and best practices.

The Problem

ESP32 TCP client cannot connect to a TCP server — connection refused or timeout.

Quick Fix

Wrong

WiFiClient client;
client.connect("192.168.1.100", 5000);
client.print("Hello");
[E][WiFiClient.cpp:315] connect(): Connection refused errno 111
WiFiClient client;
if (client.connect("192.168.1.100", 5000, 10000)) {
  client.println("Hello TCP Server");
  while (client.connected()) {
    if (client.available()) {
      String resp = client.readString();
      Serial.println(resp);
    }
  }
} else { Serial.println("Connection failed"); }
Connected to server
Server response: ACK received

Prevention

Check return value of connect(). Set a timeout (3rd parameter). Verify server is reachable. Check firewall rules. Ensure port is correct.

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

FAQ

### What causes TCP connection refused?

No service on the target port, firewall blocking, or server down. Verify with telnet from another machine.

How to handle partial TCP reads?

TCP is a stream protocol. Data may arrive in fragments. Wait for available() and read gradually.

Can ESP32 TCP reconnect automatically?

Implement reconnection in loop(): check connected(), if false close and reconnect with delay.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro