-->
Page 1 of 2

ESP8266 - Catch incoming TCP SYN / ACK packets

PostPosted: Thu Jul 06, 2017 3:07 pm
by Tjeerd
Hi everyone! For a project I am looking for a way to see when I get in incoming TCP connection packet without receiving actual data. I am trying to receive data from an alarm panel and upon connection from the panel, I need to send the first packet as a reply to get transmission going.

Code: Select all  if (!TcpServerClient.connected()) {
    TcpServerClient = TcpServer.available();
  } else {
    Serial.println("Incoming connection");
    while (TcpServerClient.connected())
    {
      Serial.println("Connection available");
      while (TcpServerClient.available())
      {
        Serial.println("Receiving incomming TCP packet");
        TcpServerClient.read(TCPpacketBuffer, TCP_TX_PACKET_MAX_SIZE);



None of the Serial.println's get displayed upon the initial connection, only when receiving a complete data packet.

I found a project on GitHub to implement sockets https://github.com/i-n-g-o/esp-socket/tree/master/examples/esp8266ServerCallbacks but to get it to work like it is, is way over my head ...

Does someone know if there's a callback or something similar to: server.onConnected() ?

Attached is a screenshot of the sequence.

Re: ESP8266 - Catch incoming TCP SYN / ACK packets

PostPosted: Fri Jul 07, 2017 8:17 pm
by gdsports
Use hasClient() to detect new incoming TCP connections.

Code: Select all    // Check for new clients.
    if (server.hasClient()) {
        newClient = server.available();
        Serial.print("New client: "); Serial.println(newClient.remoteIP());
        newClient.println("Hello, new client");
    }  // ...

Re: ESP8266 - Catch incoming TCP SYN / ACK packets

PostPosted: Sun Jul 09, 2017 5:30 am
by Tjeerd
I've tried it but unfortunately it's not sending the first packet although it seems it's doing. I can see the delay, in the packets, but it closed immediately without receiving.

Code: Select all  if (server.hasClient()) {
    Serial.println("We received a client connection");
    client = server.available();

    if (client && sent == false) {
      Serial.println("Transmitting conn packet");
      // Send first recon packet since we didn't receive anything yet
      sent = true;
      byte sendBuffer[] = {0x0, 0x4, 0x85, 0x34};
      client.write((const uint8_t*) sendBuffer, sizeof(sendBuffer));
    }     

    delay(500);

    while (client.available()) {
      Serial.println("Getting response");
      client.read(TCPpacketBuffer, TCP_TX_PACKET_MAX_SIZE);
      debugPacket(TCPpacketBuffer, TCP_TX_PACKET_MAX_SIZE);
    }
  }


Any suggestions?

Re: ESP8266 - Catch incoming TCP SYN / ACK packets

PostPosted: Mon Jul 10, 2017 4:04 am
by gdsports
Could be a problem on the client side. Use netcat or Telnet to connect to the ESP. Note hasClient only returns true when there is an unaccepted client. When the client is accepted by calling server.available(), hasClient returns false. I suggest looking at a fully working example which use hasClient.

https://github.com/esp8266/Arduino/blob ... Serial.ino