{"action":"created","issue":{"url":"https://api.github.com/repos/esp8266/Arduino/issues/2552","repository_url":"https://api.github.com/repos/esp8266/Arduino","labels_url":"https://api.github.com/repos/esp8266/Arduino/issues/2552/labels{/name}","comments_url":"https://api.github.com/repos/esp8266/Arduino/issues/2552/comments","events_url":"https://api.github.com/repos/esp8266/Arduino/issues/2552/events","html_url":"https://github.com/esp8266/Arduino/issues/2552","id":179394864,"number":2552,"title":"TCP connection always Accidental disconnection","user":{"login":"kingxf1995","id":7127170,"avatar_url":"https://avatars.githubusercontent.com/u/7127170?v=3","gravatar_id":"","url":"https://api.github.com/users/kingxf1995","html_url":"https://github.com/kingxf1995","followers_url":"https://api.github.com/users/kingxf1995/followers","following_url":"https://api.github.com/users/kingxf1995/following{/other_user}","gists_url":"https://api.github.com/users/kingxf1995/gists{/gist_id}","starred_url":"https://api.github.com/users/kingxf1995/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kingxf1995/subscriptions","organizations_url":"https://api.github.com/users/kingxf1995/orgs","repos_url":"https://api.github.com/users/kingxf1995/repos","events_url":"https://api.github.com/users/kingxf1995/events{/privacy}","received_events_url":"https://api.github.com/users/kingxf1995/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-09-27T04:27:21Z","updated_at":"2016-12-09T20:49:38Z","closed_at":null,"body":"#### Hardware\n\nHardware:           ESP-12E\nCore Version:       2.3.0\n### Description\n\nI found esp826612e network may have some problem, so I write a simple code to test it.\nIn 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\n### Sketch\n\n``` cpp\n\n#include <ESP8266WiFi.h>\n\nWiFiServer server(23333);\nWiFiClient client;\nbool connected = false;\n\nconst char *ssid = \"xxx\";\nconst char *password = \"yyy\";\n\nvoid setup() {\n  Serial.begin(115200);\n  Serial.println();\n  server.begin();\n  WiFi.mode( WIFI_STA );\n  WiFi.begin ( ssid, password );\n    // Wait for connection\n    while ( WiFi.status() != WL_CONNECTED ) {\n        delay(500);\n    Serial.print('.');\n    }\n    Serial.print(\"\\nIPAddress: \");\n    Serial.println ( WiFi.localIP() );\n}\n\nvoid loop() {\n  if (client.connected()) {\n    if (!connected) {\n      connected = true;\n      Serial.println(\"\\ntcp connected\");\n    }\n    int ch;\n    if ((ch=client.read()) != -1) {\n      Serial.printf(\"%c\", ch);\n      client.write((char)ch);\n    }\n  }\n  else {\n    if (connected) {\n      connected = false;\n      Serial.println(\"\\ntcp disconnected\");\n    }\n    client = server.available();\n    client.setNoDelay(true);\n  }\n}\n```\n### test script\n\n``` python\nimport socket  \nimport time\n\nhost    = '192.168.31.215'\nport    = 23333\naddr    = (host, port)\n\nclient=socket.socket(socket.AF_INET, socket.SOCK_STREAM)\nclient.connect(addr)\nclient.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)\nlast = time.time()\n\nwhile True:\n  client.send('x')\n  data=client.recv(1)\n  if data == 'x':\n    cur = time.time()\n    print '%4d ms' % ((cur - last) * 1000)\n    time.sleep(0.05)\n\n    last = time.time()\n```\n"},"comment":{"url":"https://api.github.com/repos/esp8266/Arduino/issues/comments/266119481","html_url":"https://github.com/esp8266/Arduino/issues/2552#issuecomment-266119481","issue_url":"https://api.github.com/repos/esp8266/Arduino/issues/2552","id":266119481,"user":{"login":"kentaylor","id":339520,"avatar_url":"https://avatars.githubusercontent.com/u/339520?v=3","gravatar_id":"","url":"https://api.github.com/users/kentaylor","html_url":"https://github.com/kentaylor","followers_url":"https://api.github.com/users/kentaylor/followers","following_url":"https://api.github.com/users/kentaylor/following{/other_user}","gists_url":"https://api.github.com/users/kentaylor/gists{/gist_id}","starred_url":"https://api.github.com/users/kentaylor/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/kentaylor/subscriptions","organizations_url":"https://api.github.com/users/kentaylor/orgs","repos_url":"https://api.github.com/users/kentaylor/repos","events_url":"https://api.github.com/users/kentaylor/events{/privacy}","received_events_url":"https://api.github.com/users/kentaylor/received_events","type":"User","site_admin":false},"created_at":"2016-12-09T20:49:38Z","updated_at":"2016-12-09T20:49:38Z","body":"@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.\r\n\r\nHowever, there is [circumstances](https://github.com/esp8266/Arduino/issues/2750) 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."}}
{"actor":{"display_login":"kentaylor"}}