AnsweredAssumed Answered

Keepalive sock option

Question asked by Emanuele Trapani on May 20, 2015
Latest reply on May 26, 2015 by Emanuele Trapani

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

        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);

  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);


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.