Keepalive sock option

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Keepalive sock option

831 Views
emanueletrapani
Contributor III

hi, i have codewarrior 10.6 and mqx 4.1. i use frdmk64f demo board.

i modified the eth_to_serial demo to have multiple sock that send messages from ethernet to serial and the serial send messages on every active sock . i would to know if it is possible to individuate when i lost the connection (as would happen if a cable breaks, for example) so i can shutdown the opportune sock and close the relative task. for this purpouse i try to set OPT_KEEPALIVE with the function setsockopt() and i use optval=1 so i expect to notice something after 1 minute since i unplug the cable. but i notice an error on the recv() function after that 8 minutes are passed (that i reduced to 3 minute setting the option OPT_CONNECT_TIMEOUT). i set true the opt_receive_nowait option on every sockets i accepted. i see that the recv() give the error RTCSERR_TCP_CONN_ABORTED (software caused connection abort).  i read the rtcs user guide but i have not found an exaustive explanation of this keepalive option. can you help me on this?

on the other side i read that, in ENET_STATS, exist ST_TX_SQE (Heartbeats lost), and ST_TX_CARRIER (Carrier sense lost), physical errors/conditions that maybe they do right for me. but i check them with ENET_get_stats() and i see they are fixed to 0 even if i unplug the cable. maybe you know these physical errors/conditions and you can tell me how i can use them.

here are some code:

main task

       ...

       option = 1;

       retval = setsockopt(sockets.sock4, SOL_TCP, OPT_KEEPALIVE, &option, sizeof(option));

       ...

       option = 181000;

       retval = setsockopt(sockets.sock4, SOL_TCP, OPT_CONNECT_TIMEOUT, &option, sizeof(option));

          ...

trasmission task

...

while(1)

    {

        count = recv(sock_local, buff, RX_BUFFER_SIZE, 0);

        if (count != RTCS_ERROR)        {

            count = fwrite(buff, 1, count, ser_device_local);

       }         else         {

               ...

              status = shutdown(sock_local, 0);

        }...

     }

it work fine. i would have some advice

i get the enet stats with

control task

ihandle = ipcfg_get_ihandle(BSP_DEFAULT_ENET_DEVICE);

  if(ihandle != NULL)

  {

  ehandle = RTCS_get_enet_handle(ihandle);

  fprintf(stdout, "\n\rCTRL: ehandle is: 0x%x", ehandle);

  }

  optlen = sizeof(uint32_t);

  while(1) {

  retval = getsockopt(client, SOL_SOCKET, OPT_SOCKET_ERROR, &opt, optlen);

  if (retval != RTCS_OK)

  {

    fputs("Fatal Error CTRL: Unable to get socket options.", stderr);

    _task_block();

  }

  fprintf(stdout, "\n\rCTRL: sock error is: 0x%x", opt);

  fprintf(stdout, "\t 0x%x", RTCS_geterror(client));

  enet_stat_ptr = ENET_get_stats(ehandle);

  fprintf(stdout, "\n\rCTRL: enet stat sqe is: %d", enet_stat_ptr->ST_TX_SQE);

  fprintf(stdout, "\n\rCTRL: carrier is: %d", enet_stat_ptr->ST_TX_CARRIER);

  fprintf(stdout, "\n\rCTRL: Ethernet packets received: %d", enet_stat_ptr->COMMON.ST_RX_TOTAL);

  enet_stat_ptr = ENET_get_stats(ehandle);

  if (temp != enet_stat_ptr->ST_TX_SQE) {

  temp = enet_stat_ptr->ST_TX_SQE;

  fprintf(stdout, "\n\rCTRL: enet stat sqe is: %d ; different from previous", enet_stat_ptr->ST_TX_SQE);

  }

  if (temp2 != enet_stat_ptr->ST_TX_CARRIER) {

  temp2 = enet_stat_ptr->ST_TX_CARRIER;

  fprintf(stdout, "\n\rCTRL: enet stat carrier is: %d ; different from previous", enet_stat_ptr->ST_TX_CARRIER);

  }

  _time_delay(1);

  }

i read in another question on this forum about to check the opt socket error for the socket in which i active the keepalive option, but it does not run on my app.

thank you for the patience about my english and my explanation.

0 Kudos
4 Replies

332 Views
Luis_Garabo
NXP TechSupport
NXP TechSupport

Hi Emanuele,

Try setting the OPT_RECEIVE_TIMEOUT to a low value. While you are in the recv() you should have this time out running and expiring according with the data you provide.

For instance:

/* Wait 250ms then check if session is still connected. */

option = 250;

error = setsockopt(sock, SOL_TCP, OPT_RECEIVE_TIMEOUT, &option, sizeof(option));

if (error != RTCS_OK)

{

    return(-1);

}


Have a great day,
Garabo

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

332 Views
emanueletrapani
Contributor III

Hi Garabo,

thank you  for the advice. it can be an useful solution. but can you give me some information, or some docs, about OPT_KEEPALIVE and ENET_STATS. are these useful in my case?

0 Kudos

332 Views
Luis_Garabo
NXP TechSupport
NXP TechSupport

Hi Emanuele,

This is the documentation that we have about Keep alive:

pastedImage_1.png

Regarding ENET_STAT you can find the description of the structure in the section 9.2.9 ENET_STATS from the RTCS user's guide located in the C:\Freescale\Freescale_MQX_4_2\doc\rtcs directory.

Have a great day,
Garabo

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

332 Views
emanueletrapani
Contributor III

thank you Garabo. i read this docs yet. i try to set OPT_KEEPALIVE with the function setsockopt() and i use optval=1 so i expect to notice something after 1 minute since i unplug the cable. but i se the error "connection aborted" after 8 minute. i read these docs, so i know in ENET_STATS, exist ST_TX_SQE (Heartbeats lost), and ST_TX_CARRIER (Carrier sense lost), physical errors/conditions that maybe they do right for me. but i check them with ENET_get_stats() and i see they are fixed to 0 even if i unplug the cable. maybe somebady know how they work.

0 Kudos