Skip to content

TCP connection always Accidental disconnection #2552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kingxf1995 opened this issue Sep 27, 2016 · 6 comments
Closed

TCP connection always Accidental disconnection #2552

kingxf1995 opened this issue Sep 27, 2016 · 6 comments

Comments

@kingxf1995
Copy link

Hardware

Hardware: ESP-12E
Core Version: 2.3.0

Description

I found esp826612e network may have some problem, so I write a simple code to test it.
In short, ESP826612e act as station join my router wifi, and act as a tcp server, accept only one connection, my computer act as a TCP client. Client send a byte then server recived it and send back a same byte. In loop, sometime ESP8266 think TCP connection is break, computer found tcp connection is break after a short time

Sketch

#include <ESP8266WiFi.h>

WiFiServer server(23333);
WiFiClient client;
bool connected = false;

const char *ssid = "xxx";
const char *password = "yyy";

void setup() {
  Serial.begin(115200);
  Serial.println();
  server.begin();
  WiFi.mode( WIFI_STA );
  WiFi.begin ( ssid, password );
    // Wait for connection
    while ( WiFi.status() != WL_CONNECTED ) {
        delay(500);
    Serial.print('.');
    }
    Serial.print("\nIPAddress: ");
    Serial.println ( WiFi.localIP() );
}

void loop() {
  if (client.connected()) {
    if (!connected) {
      connected = true;
      Serial.println("\ntcp connected");
    }
    int ch;
    if ((ch=client.read()) != -1) {
      Serial.printf("%c", ch);
      client.write((char)ch);
    }
  }
  else {
    if (connected) {
      connected = false;
      Serial.println("\ntcp disconnected");
    }
    client = server.available();
    client.setNoDelay(true);
  }
}

test script

import socket  
import time

host    = '192.168.31.215'
port    = 23333
addr    = (host, port)

client=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(addr)
client.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
last = time.time()

while True:
  client.send('x')
  data=client.recv(1)
  if data == 'x':
    cur = time.time()
    print '%4d ms' % ((cur - last) * 1000)
    time.sleep(0.05)

    last = time.time()
@kentaylor
Copy link

@kingxf1995, if the WiFi connection is unreliable, for example due to radio interference, then the behaviour you report is exactly what you would want, that is, both ends are aware there is no longer a TCP connection.

However, there is circumstances where the ESP thinks it is connected so that client.connected() will report true when it is not connected and your sketch would get stuck in an endless loop.

@mtnbrit
Copy link

mtnbrit commented Dec 9, 2016 via email

@kentaylor
Copy link

When @mtnbrit says

stuck in this state

which state is meant?

If the query relates to client.connected() reporting connection state incorrectly please ask in #2750

@mtnbrit
Copy link

mtnbrit commented Dec 9, 2016 via email

@kingxf1995
Copy link
Author

Thank you @kentaylor @mtnbrit , I have solved this problem. In fact I used a bad router which led to packet loss seriously.

@devyte
Copy link
Collaborator

devyte commented Oct 12, 2017

Per previous comment, closing as resolved.

@devyte devyte closed this as completed Oct 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants