4

I'm using WeMos D1 R2. I'm trying to listen to both ports 1992 and 1993. This is my current code: #include

WiFiServer server_1992(1992);
WiFiServer server_1993(1993);

void setup() {
    Serial.begin(9600);
    WiFi.begin("MySSID", "MyPassword");
    while (WiFi.status() != WL_CONNECTED){
        delay(500);
        Serial.print(".");
    }
    Serial.println(" connected");

    server_1992.begin();
    server_1992.setNoDelay(true);
    server_1993.begin();
    server_1993.setNoDelay(true);
}

void check_server(WiFiClient* client, WiFiServer* server, const char* text){
    if (client->connected()) { 
        if (client->available()){
            unsigned char c[250];
            client->readBytes(c, 1);
            Serial.print(c[0]);
            Serial.print(" ");
        } 
    } else {
        client->stop(); //Adding it here like Juraj said. Not tested.
        *client = server->available();     
        if (*client) Serial.print(text);        
    }
}

void loop() {
    WiFiClient client_1992, client_1993; 
    while (1){
        check_server(&client_1992, &server_1992, "1992 connected \n");
        check_server(&client_1993, &server_1993, "1993 connected \n");       
    }
}

The problem is when the client(s) disconnect from the hotspot, my code keeps waiting for data forever without trying to acquire new client (with client = server->available();).

Solution: for now I ask the client "Hey are we still connected?" every few seconds and manually kick the client out if it doesn't reply. More on this

Sorry if it's wrong to link other question here, I just hope people who have the same problem as mine can find the solution.

9
  • stop the client if it is not connected
    – Juraj
    Commented May 10, 2018 at 10:16
  • If i understand it correctly, you mean close the client from the client side right (not the server side)? The disconnection happens when my phone turn off / disconnect from wifi so I don't think I can close the connection properly?
    – MoonFan
    Commented May 10, 2018 at 11:02
  • no. stop the esp8266 side of the socket when it is not connected, before you ask if new client is available. there are more tcp states of the connection and after the client side disconnects, it is not established (what connected tests), but not closed either
    – Juraj
    Commented May 10, 2018 at 11:05
  • I will test it when I can get back home, is it like the one I added to the code ?(before the *client = server->available(); )
    – MoonFan
    Commented May 10, 2018 at 11:12
  • yes, that is what I meant. I am not sure if it helps, so I do not write it as answer until you test it. I use extensively esp8266 from Atmega over WiFi Link at it has it's own tricks. there the stop() solves this kind of problem
    – Juraj
    Commented May 10, 2018 at 11:22

1 Answer 1

-2

enter image description hereUse the client.status() function to check if the client is connected Returns 4 if connected and 0 if not I don't have detailed information about its use and I found it by chance You can check other output modes of this function If you get any other information, please let me know.

void loop()
{

  WiFiClient client_8080;
  WiFiClient client_8081;


  delay(200);
  Serial.println(client_8080.status());
  Serial.println();
  Serial.println();

  while (1)
  {


    if(client_8080.connected())
    {

      delay(200);
      Serial.println(client_8080.status());
      Serial.println();
      Serial.println();

      while (client_8080.available())
      { 
        
        delay(200);
        Serial.println(client_8080.status());
        Serial.println();
        Serial.println();

        Serial.print(client_8080.available());
        char Buffer_8080 = client_8080.read();
        Serial.println(Buffer_8080);
        client_8080.println(Buffer_8080);


      }

       while (Serial.available())        
      {
        client_8080.print(Serial.available());
        client_8081.print(Serial.available());
        char Buffer_Serial = Serial.read();

        client_8080.println(Buffer_Serial);
        client_8081.println(Buffer_Serial);
      }

    
    }else if (!client_8080.connected())
    {

       Serial.println(client_8080.status());
       Serial.println();
       Serial.println();

      client_8080 = server1.available();


       Serial.println(client_8080.status());
       Serial.println();
       Serial.println();

      Serial.println("server lisening to port 8080 ");

    }  

    //_______________________________________________

    if(client_8081.connected())
    {
      while (client_8081.available())
      {
        
        Serial.print(client_8081.available());
        char Buffer_8081 = client_8081.read();
        Serial.println(Buffer_8081);
        client_8081.println(Buffer_8081);


      }


      while (Serial.available())        
      {
        client_8080.print(Serial.available());
        client_8081.print(Serial.available());
        char Buffer_Serial = Serial.read();

        client_8080.println(Buffer_Serial);
        client_8081.println(Buffer_Serial);
      }

    
    }else if (!client_8081.connected())
    {
      client_8081 = server2.available();

      Serial.println("server lisening to port 8081 ");

    }  
      
  }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.