Testing FreeRTOS+TCP stack APIs - FreeRTOS_bind() returning -pdFREERTOS_ERRNO_EINVAL

Hello,

I am using this open source FreeRTOS+TCP library in my FreeRTOS project. I followed the porting steps, as well as configuration steps. Apart from this i have written my Network interface part. I want to create socket and send messages from that. basically i want to test the working of the APIs. socket creation is done successfully, but when i m calling FreeRTOS_bind to bind the socket, its returning error - pdFREERTOS_ERRNO_EINVAL.

This is how I m calling the function.

                xRemoteAddress.sin_port = FreeRTOS_htons( 15000);
	    xRemoteAddress.sin_addr = FreeRTOS_inet_addr_quick( 192, 168, 0, 200 );
	    socklen_t xSize = sizeof( xRemoteAddress );
	    retv = FreeRTOS_bind( xSocket, &xRemoteAddress, xSize );
	      printf("\nBind val %d\n",retv); // -22 is coming

And as I debugged this value is coming from the below line in function.

if (!socketSOCKET_IS_BOUND(pxSocket)) {
xReturn = -pdFREERTOS_ERRNO_EINVAL;
}

// this is last line of the function.

Please let me know if i am doing something wrong or something I m missing.

Thanks & Regards
Priyanka

If you are referring to the code below, you can see it says in the comment that you cannot bind a socket to another port number:

    /* Once a socket is bound to a port, it can not be bound to a different
     * port number */
    else if( socketSOCKET_IS_BOUND( pxSocket ) )
    {
        /* The socket is already bound. */
        FreeRTOS_debug_printf( ( "vSocketBind: Socket already bound to %d\n", pxSocket->usLocalPort ) );
        xReturn = -pdFREERTOS_ERRNO_EINVAL;
    }

Are you trying to connect to a server or are you trying to run a server? If former, you do not need to call bind. You can take a look at this example - FreeRTOS/TCPEchoClient_SingleTasks.c at main · FreeRTOS/FreeRTOS · GitHub

1 Like

Thank you so much I will look into it.