heinbali01 wrote on Tuesday, March 19, 2019:
Thanks you, now it is clear to me.
Can you confirm that this logging statement is triggered in FreeRTOS_Socket.c
, around line 1020:
FreeRTOS_debug_printf( ( "vSocketBind: %sP port %d in use\n",
pxSocket->ucProtocol == ( uint8_t ) FREERTOS_IPPROTO_TCP ? "TC" : "UD",
FreeRTOS_ntohs( pxAddress->sin_port ) ) );
xReturn = -pdFREERTOS_ERRNO_EADDRINUSE;
In your case, it would print ‘vSocketBind: TCP port 502 in use’
When the application gets here, I’d say there must have been an earlier call to FreeRTOS_bind()
, binding a TCP socket to port 502. And if not, it looks like xBoundTCPSocketsList
is corrupted.
Can you make some logging about all calls to FreeRTOS_bind()
?
This is another location where EADDRNOTAVAIL
is returned:
xReturn = -pdFREERTOS_ERRNO_EADDRNOTAVAIL;
FreeRTOS_debug_printf( ( "vSocketBind: Socket no addr\n" ) );
Sure it doesn’t get there?
Note also that you can define socketAUTO_PORT_ALLOCATION_START_NUMBER
in your FreeRTOSIPConfig.h
, it defaults to 1024
#define socketAUTO_PORT_ALLOCATION_START_NUMBER ( ( uint16_t ) 0x0400 )
I would recommend choosing your server port numbers under this value, under 1024. Port 502 would be OK.
Another thing is about vApplicationIPNetworkEventHook()
: the application hook is called when there is a change in the network status:
void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent )
{
/* If the network has just come up...*/
if( eNetworkEvent == eNetworkUp )
{
/* Create the tasks that use the IP stack if they have not already been
created. */
if( xTasksAlreadyCreated == pdFALSE )
{
/* Create tasks, and/or set a trigger to create sockets. */
xCreateSockets = pdTRUE;
xTasksAlreadyCreated = pdTRUE;
}
}
}
As socket and tasks must be created only once, it is important to use a variable like xTasksAlreadyCreated
, which will be set to true.
It is important not to call FreeRTOS+TCP API’s from within the application hook, because the code is running from within the IP-task. The IP-task may not call many of it’s own API’s.