Unable to run the tutorial FreeRTOS + TCP xConnectedSocket FreeRTOS_accept() does not work

Hello,
as highlighted in 2015 and 2018,
forums freertos org t freertos-accept-does-not-return-unless-timed-out-stm32f7 7122

following the tutorial example

freertos org FreeRTOS-Plus FreeRTOS_Plus_TCP TCP_Networking_Tutorial_TCP_Client_and_Server html

I came to launch this program.

int main( void )
{
    BaseType_t xReturn = pdFALSE;

  /* 
   *Initialise the RTOS's TCP/IP stack.  The tasks that use the network
   *   are created in the vApplicationIPNetworkEventHook() hook function
   *  below.  The hook function is called when the network connects. 
   * */
    
FreeRTOS_IPInit( ucIPAddress,
                     ucNetMask,
                     ucGatewayAddress,
                     ucDNSServerAddress,
                     ucMACAddress );

    xReturn = xTaskCreate( vCreateTCPServerSocket,
				 ( const char * ) "GB",
				 configMINIMAL_STACK_SIZE*5,
				 NULL,
				 tskIDLE_PRIORITY + 1,
				 &xRxTask );
    /*
     * Other RTOS tasks can be created here.
     */

    /* Start the RTOS scheduler. */
    vTaskStartScheduler();

    /* If all is well, the scheduler will now be running, and the following
    line will never be reached.  If the following line does execute, then
    there was insufficient FreeRTOS heap memory available for the idle and/or
    timer tasks to be created. */
    for( ;; );
}

/***************************************************************************/
void vCreateTCPServerSocket( void * pvParameters ) {
	struct freertos_sockaddr xClient, xBindAddress;
	Socket_t xListeningSocket, xConnectedSocket;
	socklen_t xSize = sizeof(xClient);
	static const TickType_t xReceiveTimeOut = portMAX_DELAY;
	const BaseType_t xBacklog = 20;

	/* Attempt to open the socket. */
	xListeningSocket = FreeRTOS_socket(FREERTOS_AF_INET,
	FREERTOS_SOCK_STREAM, /* SOCK_STREAM for TCP. */
	FREERTOS_IPPROTO_TCP);

	/* Check the socket was created. */
	configASSERT(xListeningSocket != FREERTOS_INVALID_SOCKET);

	/* If FREERTOS_SO_RCVBUF or FREERTOS_SO_SNDBUF are to be used with
	 FreeRTOS_setsockopt() to change the buffer sizes from their default then do
	 it here!.  (see the FreeRTOS_setsockopt() documentation. */

	/* If ipconfigUSE_TCP_WIN is set to 1 and FREERTOS_SO_WIN_PROPERTIES is to
	 be used with FreeRTOS_setsockopt() to change the sliding window size from
	 its default then do it here! (see the FreeRTOS_setsockopt()
	 documentation. */

	/* Set a time out so accept() will just wait for a connection. */
	FreeRTOS_setsockopt(xListeningSocket, 0,
	FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof(xReceiveTimeOut));

	/* Set the listening port to 10000. */
	xBindAddress.sin_port = (uint16_t) 10000;
	xBindAddress.sin_port = FreeRTOS_htons(xBindAddress.sin_port);

	/* Bind the socket to the port that the client RTOS task will send to. */
	FreeRTOS_bind(xListeningSocket, &xBindAddress, sizeof(xBindAddress));

	/* Set the socket into a listening state so it can accept connections.
	 The maximum number of simultaneous connections is limited to 20. */
	FreeRTOS_listen(xListeningSocket, xBacklog);

	for (;;) {
		/* Wait for incoming connections. */
		xConnectedSocket = FreeRTOS_accept(xListeningSocket, &xClient, &xSize);
		configASSERT(xConnectedSocket != FREERTOS_INVALID_SOCKET);

		/* Spawn a RTOS task to handle the connection. */
		xTaskCreate(prvServerConnectionInstance, "EchoServer", usUsedStackSize,
				(void *) xConnectedSocket,
				tskIDLE_PRIORITY,
				NULL);
	}
}

/****************************************************************/

static void prvServerConnectionInstance( void *pvParameters )
{
int32_t lBytes, lSent, lTotalSent;
uint8_t cReceivedString[ ipconfigTCP_MSS ];
Socket_t xConnectedSocket;
static const TickType_t xReceiveTimeOut = pdMS_TO_TICKS( 5000 );
static const TickType_t xSendTimeOut = pdMS_TO_TICKS( 5000 );
TickType_t xTimeOnShutdown;

	ulConnectionCount++;
	xConnectedSocket = ( Socket_t ) pvParameters;
	FreeRTOS_setsockopt( xConnectedSocket, 0, FREERTOS_SO_RCVTIMEO, &xReceiveTimeOut, sizeof( xReceiveTimeOut ) );
	FreeRTOS_setsockopt( xConnectedSocket, 0, FREERTOS_SO_SNDTIMEO, &xSendTimeOut, sizeof( xReceiveTimeOut ) );

	for( ;; )
	{
		/* Zero out the receive array so there is NULL at the end of the string
		when it is printed out. */
		memset( cReceivedString, 0x00, sizeof( cReceivedString ) );

		/* Receive data on the socket. */
		lBytes = FreeRTOS_recv( xConnectedSocket, cReceivedString, sizeof( cReceivedString ), 0 );

		/* If data was received, echo it back. */
		if( lBytes >= 0 )
		{
			lSent = 0;
			lTotalSent = 0;

			/* Call send() until all the data has been sent. */
			while( ( lSent >= 0 ) && ( lTotalSent < lBytes ) )
			{
				lSent = FreeRTOS_send( xConnectedSocket, cReceivedString, lBytes - lTotalSent, 0 );
				lTotalSent += lSent;
			}

			if( lSent < 0 )
			{
				/* Socket closed? */
				break;
			}
		}
		else
		{
			/* Socket closed? */
			break;
		}
	}

	/* Initiate a shutdown in case it has not already been initiated. */
	FreeRTOS_shutdown( xConnectedSocket, FREERTOS_SHUT_RDWR );

	/* Wait for the shutdown to take effect, indicated by FreeRTOS_recv()
	returning an error. */
	xTimeOnShutdown = xTaskGetTickCount();
	do
	{
		if( FreeRTOS_recv( xConnectedSocket, cReceivedString, ipconfigTCP_MSS, 0 ) < 0 )
		{
			break;
		}
	} while( ( xTaskGetTickCount() - xTimeOnShutdown ) < tcpechoSHUTDOWN_DELAY );

	/* Finished with the socket and the task. */
	FreeRTOS_closesocket( xConnectedSocket );
	vTaskDelete( NULL );
}

After successfully pinging and assigning the port I perform all the steps and put the zedboard (I am using) in the listening condition, at this point I launch the FreeRTOS_accept() (as showed in the code) command, at this point the pointer goes in the idle task not recognizing the client … I’m simulating the client first with iperf then with ncat. I need support

what are the return values of your calls to bind and listen? Make it a habit to check all of them… what is the value of the address field passed to bind?

bind and listen return variables are both 0

pxClientSocket this variable is 0 looks the board cannot read the pc

Socket_t FreeRTOS_accept( Socket_t xServerSocket,
                              struct freertos_sockaddr * pxAddress,
                              socklen_t * pxAddressLength )
    {
        FreeRTOS_Socket_t * pxSocket = ( FreeRTOS_Socket_t * ) xServerSocket;
        FreeRTOS_Socket_t * pxClientSocket = NULL;
        TickType_t xRemainingTime;
        BaseType_t xTimed = pdFALSE, xAsk = pdFALSE;
        TimeOut_t xTimeOut;
        IPStackEvent_t xAskEvent;

        if( prvValidSocket( pxSocket, FREERTOS_IPPROTO_TCP, pdTRUE ) == pdFALSE )
        {
            /* Not a valid socket or wrong type */
            pxClientSocket = FREERTOS_INVALID_SOCKET;
        }
        else if( ( pxSocket->u.xTCP.bits.bReuseSocket == pdFALSE_UNSIGNED ) &&
                 ( pxSocket->u.xTCP.ucTCPState != ( uint8_t ) eTCP_LISTEN ) )
        {
            /* Parent socket is not in listening mode */
            pxClientSocket = FREERTOS_INVALID_SOCKET;
        }
        else
        {
            /* Loop will stop with breaks. */
            for( ; ; )
            {
                /* Is there a new client? */
                vTaskSuspendAll();
                {
                    if( pxSocket->u.xTCP.bits.bReuseSocket == pdFALSE_UNSIGNED )
                    {
                        pxClientSocket = pxSocket->u.xTCP.pxPeerSocket;
                    }
                    else
                    {
                        pxClientSocket = pxSocket;
                    }

                    if( pxClientSocket != NULL )
                    {
                        pxSocket->u.xTCP.pxPeerSocket = NULL;

                        /* Is it still not taken ? */
                        if( pxClientSocket->u.xTCP.bits.bPassAccept != pdFALSE_UNSIGNED )
                        {
                            pxClientSocket->u.xTCP.bits.bPassAccept = pdFALSE;
                        }
                        else
                        {
                            pxClientSocket = NULL;
                        }
                    }
                }
                ( void ) xTaskResumeAll();

                if( pxClientSocket != NULL )
                {
                    if( pxAddress != NULL )
                    {
                        /* IP address of remote machine. */
                        pxAddress->sin_addr = FreeRTOS_ntohl( pxClientSocket->u.xTCP.ulRemoteIP );

                        /* Port on remote machine. */
                        pxAddress->sin_port = FreeRTOS_ntohs( pxClientSocket->u.xTCP.usRemotePort );
                    }

                    if( pxAddressLength != NULL )
                    {
                        *pxAddressLength = sizeof( *pxAddress );
                    }

                    if( pxSocket->u.xTCP.bits.bReuseSocket == pdFALSE_UNSIGNED )
                    {
                        xAsk = pdTRUE;
                    }
                }

                if( xAsk != pdFALSE )
                {
                    /* Ask to set an event in 'xEventGroup' as soon as a new
                     * client gets connected for this listening socket. */
                    xAskEvent.eEventType = eTCPAcceptEvent;
                    xAskEvent.pvData = pxSocket;
                    ( void ) xSendEventStructToIPTask( &xAskEvent, portMAX_DELAY );
                }

                if( pxClientSocket != NULL )
                {
                    break;
                }

                if( xTimed == pdFALSE )
                {
                    /* Only in the first round, check for non-blocking */
                    xRemainingTime = pxSocket->xReceiveBlockTime;

                    if( xRemainingTime == ( TickType_t ) 0 )
                    {
                        break;
                    }

                    /* Don't get here a second time */
                    xTimed = pdTRUE;

                    /* Fetch the current time */
                    vTaskSetTimeOutState( &xTimeOut );
                }

                /* Has the timeout been reached? */
                if( xTaskCheckForTimeOut( &xTimeOut, &xRemainingTime ) != pdFALSE )
                {
                    break;
                }

                /* Go sleeping until we get any down-stream event */
                ( void ) xEventGroupWaitBits( pxSocket->xEventGroup, ( EventBits_t ) eSOCKET_ACCEPT, pdTRUE /*xClearOnExit*/, pdFALSE /*xWaitAllBits*/, xRemainingTime );
            }
        }

        return pxClientSocket;
    }

first of all thanks for reply.
I saw that there is an alternation between the task
prvIPTask,
vCreateTCPServerSocket,
“”""“I put the same priority”"""""
I have doubts about the design of the project.
I wanted to ask for help for this too … in your opinion is it okay as I wrote the main?

int main( void )
{
	BaseType_t xReturn = pdFALSE;
    /* Initialise the RTOS's TCP/IP stack.  The tasks that use the network
    are created in the vApplicationIPNetworkEventHook() hook function
    below.  The hook function is called when the network connects. */
    FreeRTOS_IPInit( ucIPAddress,
                     ucNetMask,
                     ucGatewayAddress,
                     ucDNSServerAddress,
                     ucMACAddress );

   xTaskCreate( vCreateTCPServerSocket,
				 ( const char * ) "GB",
				 ipconfigIP_TASK_STACK_SIZE_WORDS,
				 NULL,
				 ipconfigIP_TASK_PRIORITY,
				 &xRxTask );
    /*
     * Other RTOS tasks can be created here.
     */

    /* Start the RTOS scheduler. */
    vTaskStartScheduler();

    /* If all is well, the scheduler will now be running, and the following
    line will never be reached.  If the following line does execute, then
    there was insufficient FreeRTOS heap memory available for the idle and/or
    timer tasks to be created. */
    for( ;; );
}

@itgialm1 Hint: When posting code blocks please enclose them in 3 tildas ‘~~~’ or backticks ‘```’ for better readability. There is also a button </> for inline quoting.
Thanks !

In most (but not all cases) it is best to have any tasks that executed deferred interrupt processing (say of MAC interrupts) at the highest priority, the task that runs the TCP stack at a lower priority, and tasks that use the TCP/IP stack at a lower priority again.

any advice to simulate a client sending packets?

Giuseppe, I already mentioned that not all members of your local variable xBindAddress are initialized correctly. Fix that first, it might solve your problem.

could you be more precise? what variables are you talking about? I copied the variables from the tutorial on the official site

Normally there is an address member in the struture that determines for multi interface stacks on which interfaces to listen to. It is generally set to ANY (I don’t know what the symbolic eqiuvalent is in FreeRTOS). If you leave that field uninitialized, it may contain garbage-