AnsweredAssumed Answered

lwip binds only the first time

Question asked by biafra on Mar 7, 2017
Latest reply on Mar 13, 2017 by xiangjun.rong

Hi everyone,

 

I'm working on a custom board using MK65FX1M0VM18 device with KSDK 2.1.

The application works fine but there is a problem that stops me: a task is a TCP server that receives connection and commands from the network. When the application runs I can successful connect to the server and send the commands that are executed. But if I disconnect and try to connect again, I cannot connect until the board is reset.

 

This is some snippet of code:

 

while( 1 )

{

      sock = socket( AF_INET, SOCK_STREAM, 0 );

      if( sock < 0 )

      {

         /* error */

      }

 

      memset( &socka, 0, sizeof( socka ));
      socka.sin_family = AF_INET;
      socka.sin_port = htons( SOCKET_PORT );
      socka.sin_addr.s_addr = htonl( INADDR_ANY );

 

      if( bind( sock, ( struct sockaddr * )&socka, sizeof( socka )) < 0 )

      {

         /* error */

      }

 

      if( listen( sock, 5 ) < 0 )
      {

         /* error */

      }

 

      socksize = sizeof( socka );

      conn = accept( sock, ( struct sockaddr * )&socka, &socksize );
      if( conn == < 0 )
      {

         /* error */

      }

      while( recv( conn, buff_cmd, sizeof( buff_cmd ), 0 ) > 0 )

      {

            /* execute commands */

            if( command == disconnect )

                  break;

      }

      shutdown( conn, SHUT_RDWR );

      shutdown( sock, SHUT_RDWR );

}

 

The first cycle everything is ok: the command processing is working fine. When I send the disconnect command, the command is executed and the two shutdown() function are called. The cycle is then repeated: the socket() function is executed fine, but the following bind() function exits with negative code (fails).

 

Which is the reason for this behaviour?

Is there anyone that can point me in the right direction?

 

Many thanks

Biafra

Outcomes