how create TCP server by ESP8266?

后端 未结 1 1029
囚心锁ツ
囚心锁ツ 2021-02-10 15:12

I want to create a simple Wifi TCP server by ESP8266 in Arduino IDE. But I have a big problem: when I send a character or string from client I can\'t receive it on the server.

1条回答
  •  北荒
    北荒 (楼主)
    2021-02-10 16:12

    In the loop, you are closing the client connection as soon as it is established deleting the WiFiClient object.

    In order to keep the connection open you could modify the loop like this :

    WiFiClient client;
    void loop() 
    {
        if (!client.connected()) {
            // try to connect to a new client
            client = server.available();
        } else {
            // read data from the connected client
            if (client.available() > 0) {
                Serial.write(client.read());
            }
        }
    }
    

    When client is not connected it tries to connect one and when a client is connected, it reads incoming data.

    0 讨论(0)
提交回复
热议问题