Issue ESP32 as TCP client sending undesirable messages to TCP server

mreyesp
Posts: 1
Joined: Mon Nov 27, 2017 7:20 pm

Issue ESP32 as TCP client sending undesirable messages to TCP server

Postby mreyesp » Mon Nov 27, 2017 8:05 pm

Hi there,

i am trying to establish a TCP connection between a pc as server and the esp32 as client (bidirectional messaging).I can establish a TCP connection, the issue appears at the moment of send a message from the ESP32 to the server, the server receives the proper message but then arrives a lot of undesirable data (seems to be just null characters).
I was thinking that maybe this problem is related to the output buffer, but i could not solve the problem jet.
I let you the code of the client and server and also the resulting problem.

The desired behaviour on the TCP server side is this:
192.168.2.104
[*] Started listening on 192.168.2.104 : 1234
Received connection: 192.168.2.112
[*] Started listening on 192.168.2.104 : 1234
[*] Received ' b'Hello server' 192.168.2.112 ' from the client
Processing done.
[*] Reply sent

But then start to print a lot of this lines over and over again:
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent

NOTE: The server was programed in python and was already tested against other PC's.

Can you help me to figure out what is going wrong? Thanks :)

Source Code TCP CLient ESP32

Code: Select all

#include <WiFi.h>

void setup()
{
    Serial.begin(115200);
    delay(10);
    
    const char* ssid     = "xxxx";
    const char* password = "yyyy";

    // We start by connecting to a WiFi network

    Serial.println();
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);

    // We start by connecting to a WiFi network
    WiFi.begin(ssid, password);

    while(WiFi.status() != WL_CONNECTED) {
        Serial.print(".");
        delay(500);
    }

    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());

    delay(500);

    const uint16_t port = 1234;
    const char * host = "192.168.2.104"; // ip or dns

    Serial.print("connecting to ");
    Serial.println(host);

    // Use WiFiClient class to create TCP connections
    WiFiClient client;

    if (!client.connect(host, port)) {
        Serial.println("connection failed");
        Serial.println("wait 5 sec...");
        delay(5000);
        return;
    }

    client.print("Hello server");
    delay(5000);
    String line = client.readStringUntil('\r');
    Serial.println(line);
    delay(5000);
    client.stop();
    delay(5000);
}

void loop(){}


---------------------------------------------------------------------------------------------------------------------------------------------------------------

Source Code TCP Server PC (python)

Code: Select all

import socket
import threading

class ClientThread(threading.Thread):
    def __init__(self, client, details):
        self.client = client
        self.details = details
        threading.Thread.__init__ ( self )

    def run(self):
        print('Received connection:', self.details[0])

        while True:

            data = self.client.recv(1024)
            client_details = self.details[0]
            print("[*] Received '", data, client_details,
                  "' from the client")
            strhello = "Hello server"
            strdisconnect = "disconnect"

            if data == strhello.encode('utf-8'):
                server_reply = 'Hello client'
                self.client.sendall(
                    server_reply.encode('utf-8'))
                print("Processing done.\n[*] Reply sent")
            elif data == strdisconnect.encode('utf-8'):
                server_reply = 'Goodbye'
                self.client.sendall(
                    server_reply.encode('utf-8'))
                self.client.close()
                print('Closed connection:',
                      self.details[0])
                break
            else:
                server_reply = 'Invalid data'
                self.client.sendall(
                    server_reply.encode('utf-8'))
                print("Processing done Invalid data.\n[*] "
                      "Reply sent")

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = socket.gethostbyname(socket.gethostname())
print (ip)
port = 1234
address = (ip,port)
server.bind(address)
server.listen(5)

while True:
    print("[*] Started listening on ", ip, ":", port)
    client, details = server.accept()
    ClientThread(client, details).start()
----------------------------------------------------------------------------------------------------------------------------------------------------------------

ESP32 results

Connecting to M&M_Wi-Fi
.............
WiFi connected
IP address:
192.168.2.112
connecting to 192.168.2.104
Hello client

----------------------------------------------------------------------------------------------------------------------------------------------------------------

PC results

192.168.2.104
[*] Started listening on 192.168.2.104 : 1234
Received connection: 192.168.2.112
[*] Started listening on 192.168.2.104 : 1234
[*] Received ' b'Hello server' 192.168.2.112 ' from the client
Processing done.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Processing done Invalid data.
[*] Reply sent
[*] Received ' b'' 192.168.2.112 ' from the client
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\Hause\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "F:/BeagleBone Black/Python_Projects/Py_TCP_Server_v2/Py_TCP_Server_v2.py", line 41, in run
server_reply.encode('utf-8'))
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

Who is online

Users browsing this forum: No registered users and 27 guests