Skip to content

ESP8266 esp-12 tcp server disconnect from client #7274

@sajjadEsp

Description

@sajjadEsp

-hi 4 all
I wrote a program that esp8266 work as a ap and i initialize a tcp server on it
When a client connect to it, the client disconnected immediatly from server

#include <ESP8266WiFi.h>
#include <WiFiClient.h>

#ifndef APSSID
#define APSSID "ESPap"
#define APPSK  "thereisnospoon"
#endif

// Hardcode WiFi parameters as this isn't going to be moving around.
const char *ssid = APSSID;
const char *password = APPSK;

// Start a TCP Server on port 5045
WiFiServer server(5045);
WiFiClient client;

//boolean alreadyConnected=false;

void setup() {
  delay(1000);
  Serial.begin(9600);
  WiFi.softAP(ssid, password);

  IPAddress myIP = WiFi.softAPIP();

  Serial.print("IP Address: "); Serial.println(myIP);
 
  // Start the TCP server
  server.begin();
  Serial.println("TCP server started");
}

void loop() {
  // listen for incoming clients
  client = server.available();
  if (client){
//    if (!alreadyConnected)
//   {
//      client.flush();
//      Serial.println("We have a new client");
//      client.println("Hello client");
//      alreadyConnected=true;
//    }
    Serial.println("new client connected");
    while (client.connected() || client.available()){
      if (client.available())
      {
        // Read the incoming TCP command
        char command = client.read();
        // Debugging display command
        Serial.print(command);
        
      }
    }
     if (!client.connected()) 
     {
       Serial.println();
       Serial.println("disconnecting.");
       client.stop();
    }
  }
 }

i has wireshark capture of it

Untitled

please hekp me
thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions