cancel
Showing results for 
Search instead for 
Did you mean: 

Connection forcibly closed by the remote host!

Mujtaba
Senior

Dear friends,

I am working on a TCP Server taks running on STM32f407VGt6 MCU and NetConn APIs.

Sometimes when a Client tries to connect to the Server, the Connection is closed by the Server and the error is Connection forcibly closed by the remote host and if I don't reset the MCU, this error won't stop.

What happens in the Server that this error occurs? How to fix it?

  /* Create a new connection identifier. */
  conn = netconn_new(NETCONN_TCP);
  
  if (conn!=NULL)
  {  
    /* Bind connection to well known port number 7. */
    err = netconn_bind(conn, NULL, TCP_ECHO_SERVER_PORT_NUMBER);
    
    if (err == ERR_OK)
    {
      /* Tell connection to go into listening mode. */
      netconn_listen(conn);
    
      while (1) 
      {
        /* Grab new connection. */
         accept_err = netconn_accept(conn, &newconn);
    
        /* Process the new connection. */
        if (accept_err == ERR_OK) 
        {
          recv_err = netconn_recv(newconn, &buf);
					while ( recv_err == ERR_OK) 
          {
            do 
            {
              netbuf_data(buf, &data, &len);
              netconn_write(newconn, data, len, NETCONN_COPY);
          
            } 
            while (netbuf_next(buf) >= 0);
          
            netbuf_delete(buf);
						recv_err = netconn_recv(newconn, &buf);
          }
        
          /* Close connection and discard connection identifier. */
          netconn_close(newconn);
          netconn_delete(newconn);
        }
      }
    }
    else
    {
      netconn_delete(newconn);
      printf(" can not bind TCP netconn");
    }
  }
  else
  {
    printf("can not create TCP netconn");
  }

It is written in the Server code to close the connection when there in no data to receive close the connection and discard the connection.

Any helps would be appreciated in advance.

Kind regards,

Mujtaba

0 REPLIES 0