Here is some more information about the behaviour of
FreeRTOS_connect()
:
FreeRTOS_connect()
starts by looking-up the MAC-address of the target. If the IP-address has an entry in the ARP table, this happens very quickly. When an ARP look-up is needed, it will take at least half a second.
The number of attempts ( sending a
SYN
), and the time-outs between them are fixated in the library source code:
~~~
0.000 sec SYN-1
3.000 sec SYN-2
9.000 sec SYN-3
~~~
You can either
connect()
to a remote device in a blocking way, or by polling.
Blocking: the socket option
RCVTIMEO
determines the maximum time that
FreeRTOS_connect()
will wait for a connection:
~~~
/* Wait at most 5 seconds. */
TickType_t xTimeout = pdMS_TO_TICKS( 5000ul );
FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_RCVTIMEO, ( void * ) &xTimeout, sizeof( BaseType_t ) );
~~~
Mind that a useful value for
RCVTIMEO
is either +/- 2, 5, or 11 seconds.
When using
FreeRTOS_select()
, a successful
FreeRTOS_connect()
will be notified as a
eSELECT_WRITE
event. A successful acceptance of a new client triggers a
eSELECT_READ
event.
You can find complete examples of using
FreeRTOS_select()
in the “protocols” directory ( FTP and HTTP server ).
When using “non-blocking” ways to connect, you can call this function to see if a connection was established:
~~~
/* returns pdTRUE if TCP socket is connected */
BaseType_t FreeRTOS_issocketconnected( Socket_t xSocket );
~~~
You may also call
FreeRTOS_recv()
before the connection is established, to see any errors that might occurs.
If you do not want to block on
FreeRTOS_connect()
, and you don’t want to use
FreeRTOS_select()
, there is a third way. Let your task block on a semaphore, and bind that semaphore to all sockets that it owns:
~~~
/* Define this in you FreeRTOSIPConfig.h */
define ipconfigSOCKETHASUSER_SEMAPHORE 1
/* Bind a semaphore to a socket. */
FreeRTOS_setsockopt( xSocket, 0, FREERTOS_SO_SET_SEMAPHORE, ( void * ) &xMySemaphore, sizeof( xMySemaphore ) );
~~~
The semaphore will be given to for every event of interest: RX, TX, connect succeeded, acceptance of a client.
The same semaphore can be “bound” to many sockets of all types.
I attached a working “hello world” example of this method: “socket
usingsemaphore.c”