cancel
Showing results for 
Search instead for 
Did you mean: 

TCP Client on NUCLEO-F746ZG can't connect to a TCP Server on my PC using Hercules

Necrotos
Associate II

Hello,

I want to transmit data between two boards using TCP. To do this, one will run a client and the other will run a server. Both of these implementations follow the provided examples. The server works fine, I can connect to it from Hercules and transfer data.

The client implementation is causing me problems, as it can't seem to establish a connection to a TCP server. I can however ping it just fine. Both boards are assigned an IP via DHCP, but the address is always the same and matches with what I expect. Here is the code:

static void tcpecho_client_thread(void const *arg)
{
  struct netconn *xNetConn = NULL;
 
  err_t bind_err, connect_err;
 
  char* b_data = "OK"; // Data to be sent
  uint16_t b_len = sizeof ( b_data );
 
  IP4_ADDR(&local_ip, IP_ADDR0_CLIENT, IP_ADDR1_CLIENT, IP_ADDR2_CLIENT, IP_ADDR3_CLIENT);
  IP4_ADDR(&pc_ip, IP_ADDR0_PC, IP_ADDR0_PC, IP_ADDR2_PC, IP_ADDR3_PC);
 
  xNetConn = netconn_new ( NETCONN_TCP );
  if (xNetConn != NULL){
      bind_err = netconn_bind ( xNetConn, &local_ip, TCP_PORT_NETCONN );
 
      if(bind_err == ERR_OK){
 
          // Try to connect to server
          for(;;){
              connect_err = netconn_connect ( xNetConn, &pc_ip, TCP_PORT_NETCONN);
 
              if (connect_err == ERR_OK){
                  // We are connected
                  while(1){
                      BSP_LED_On(LED1);
                      netconn_write(xNetConn, b_data, b_len, NETCONN_COPY);
                      vTaskDelay(1000); // To see the result easily in Comm Operator
                  }
              }
          }
      }else{
          // Failed to bind the connection
          BSP_LED_On(LED3);
      }
  }else{
      // Failed to allocate a new connection
      BSP_LED_On(LED3);
  }
 
}

As I can ping it, I don't think something is fundamentally wrong. Is there something I am missing here? In the for loop, it should try to establish a connection until it succeeds, but it never actually manages to connect to something.

5 REPLIES 5
Pavel A.
Evangelist III

Is the server a Windows PC? Firewall enabled?

I have tried with the server on my Windows PC using Hercules. In that there should be at least the default Windows Firewall (at least to my knowledge).

I have also tried running a simple server on a second board. In that case, both are in the same subnet so the Firewall shouldn't play a role in that case.

Well, then it's the time for wireshark.

With Wireshark I can see the responses to my pings, but I can't see anything indicating that the board is trying to connect my PC. Should there also be a package with that information or is that just not something I can see?

With Wireshark I can see the responses to my pings, but I can't see anything indicating that the board is trying to connect my PC. Should there also be a package with that information or is that just not something I can see?