Arm Mbed OS support forum

Unexpected data in socket recv method over wifi

Hello,

I am evaluating the use of Mbed in a real production project after many months testing Mbed with several boards.

In the real project I will have to get the time from a NTP server and I have been playing with the several examples without luck.

Once I do the UDP request, the recv method returns 2 bytes many times until the actual response is received. Then it keeps returning these unexpected 2 bytes (0xAD).

To help isolate the problem I am now trying a very standard HTTP call to www.arm.com with the following code in the https://os.mbed.com/platforms/ST-Discovery-L475E-IOT01A/ board and I have similar issues.

Code:

#include "TCPSocket.h"

#include "mbed.h"

#define WIFI_IDW0XX1 2

#include "ISM43362Interface.h"

DigitalOut led(LED3);

ISM43362Interface wifi;

bool connect_wifi(const int retry_times) {

  printf("Connecting to %s...\n\n", MBED_CONF_APP_WIFI_SSID);

  for (int i = 0; i < retry_times; i++) {

    int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD,

                           NSAPI_SECURITY_WPA_WPA2);

    if (ret == 0) {

      return true;

    }

  }

  return false;

}

nsapi_error_t resolve_dns(NetworkInterface *iface, const int retry_times,

                          const char *hostname, SocketAddress &sock_addr) {

  printf("Resolving DNS for %s...\n\n", hostname);

  for (int i = 0; i < retry_times; i++) {

    nsapi_error_t ret = iface->gethostbyname(hostname, &sock_addr);

    if (ret == 0) {

      return true;

    }

  }

  return false;

}

bool TCP_test(NetworkInterface *iface) {

  printf("\nTCP test GET request to wwww.arm.com\n\n");

  SocketAddress sock_addr;

  // Resolve DNS

  const bool dns_resolved = resolve_dns(iface, 5, "www.arm.com", sock_addr);

  if (!dns_resolved) {

    printf("Error resolving DNS\n");

    return false;

  }

  sock_addr.set_port(80);

  printf("Socket address %s:%d\n", sock_addr.get_ip_address(),

         sock_addr.get_port());

  // Connect socket

  TCPSocket socket;

  socket.open(iface);

  nsapi_error_t res;

  res = socket.connect(sock_addr);

  if (res != NSAPI_ERROR_OK) {

    printf("Error connecting to socket\n");

    socket.close();

    return false;

  }

  // Send a http request

  char tx_buffer[] =

      "GET / HTTP/1.1\r\nHost: www.arm.com\r\nConnection: close\r\n\r\n";

  nsapi_size_t size = strlen(tx_buffer);

  res = 0;

  while (size) {

    res = socket.send(tx_buffer + res, size);

    if (res < 0) {

      printf("Error sending data: %d\n", res);

      socket.close();

      return false;

    } else {

      size -= res;

      printf("Sent %d bytes: [%.*s]\n", res,

             strstr(tx_buffer, "\r\n") - tx_buffer, tx_buffer);

    }

  }

  // Recieve http response

  char rx_buffer[64];

  nsapi_size_or_error_t rx_count = 0;

  do {

    rx_count = socket.recv(rx_buffer, sizeof rx_buffer);

    if (rx_count < 0) {

      printf("Error receiving data: %d\n", rx_count);

    } else {

      printf("recv %d [%.*s]\n", rx_count,

             strstr(rx_buffer, "\r\n") - rx_buffer, rx_buffer);

    }

  } while (rx_count > 0);

  socket.close();

  return true;

}

int main() {

  printf("Mbed OS version: %d.%d.%d\n\n", MBED_MAJOR_VERSION,

         MBED_MINOR_VERSION, MBED_PATCH_VERSION);

  // Connect to WiFi

  bool connected = connect_wifi(5);

  if (!connected) {

    printf("\nCould not connect to WiFi. Exiting program\n");

    exit(-1);

  }

  printf("MAC: %s\n", wifi.get_mac_address());

  printf("IP: %s\n", wifi.get_ip_address());

  printf("Netmask: %s\n", wifi.get_netmask());

  printf("Gateway: %s\n", wifi.get_gateway());

  printf("RSSI: %d\n", wifi.get_rssi());

  ThisThread::sleep_for(1s);

  const bool ok = TCP_test(&wifi);

}

And this is what I see in the Serial Monitor:

Mbed OS version: 6.3.0

Connecting to SharkPoint...

MAC: C4:7F:51:05:F7:45
IP: 192.168.50.83
Netmask: 255.255.255.0
Gateway: 192.168.50.1
RSSI: -73

TCP test GET request to wwww.arm.com

Resolving DNS for www.arm.com...

Socket address 104.83.183.180:80
socket.connect => 0
Sent 56 bytes: [GET / HTTP/1.1]
recv 2 []
recv 2 []
recv 2 []
recv 2 []
recv 2 []
recv 64 [HTTP/1.1 301 Moved Permanently]
recv 64 [gth: 0]
recv 64 []
recv 64 [ 12:20:03 GMT]
recv 36 [=HIT]
recv 0 [=HIT]

As you can see, before starting to receive the response (“HTTP/1.1 301 …”) there are several 2 bytes being received always with 0xDA values. It happens with both UDP and TCP sockets and is driving me crazy.

Sometimes, these bytes are received at the end, causing an endless loop as I am waiting for receive size to be 0. It is even more annoying with the UDP socket.

Does anyone know why this would happen? After spending lots of hours debugging I am out of ideas…

Thanks in advance,
Lluis