Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

TCP KEEP_ALIVE

Altera_Forum
Honored Contributor II
1,451 Views

Hi, 

 

Is there a restriction on the TCP stack on the use of KEEP_ALIVE option ? 

 

I didn't success in using this option : There is no error message, but I can't see any additional exchanges on the TCP exchanges and I get no indication when I disconnect the cable. 

 

Maybe I'm wrong in my code, here is an extract : 

 

int sockval; int length;   /* Create the TCP socket */ if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)     {     TRACE_CONNECT "Error : cannot create the control socket\n");     return -1;     } sockval = 1; length = sizeof(sockval); if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &sockval, length) == -1)     {     TRACE_CONNECT "Warning : KeepAlive option set failed\n");     } memset(&adsock_server, 0, sizeof(adsock_server)); adsock_server.sin_family      = AF_INET; adsock_server.sin_port        = htons(G_JGXEOEinfo.omcpTCPPort); adsock_server.sin_addr.s_addr = htonl(G_JGXEOEinfo.omcpIPAddress);   if (connect(fd, (struct sockaddr *)&adsock_server, sizeof(adsock_server)) == -1)                 {     TRACE_CONNECT "Warning : connection to server (IP=%08X Port=%04X) failed\n", (unsigned int)G_JGXEOEinfo.omcpIPAddress, G_JGXEOEinfo.omcpTCPPort);     } else     {     //TRACE_CONNECT "Control Application connected to server (IP=%08X Port=%04X)\n", (unsigned int)G_JGXEOEinfo.omcpIPAddress, G_JGXEOEinfo.omcpTCPPort);     return fd;     } 

 

Thank you for your contribution.
0 Kudos
3 Replies
Altera_Forum
Honored Contributor II
671 Views

Hi, 

 

I took some testing but the keepalive works as it is suppose to. The default timer is 2 hours so unless you change it you will be waiting a long time before anything happens. If you build the kernel with sysctl support you can change the values through the proc file system. They live in /proc/sys/net/ipv4 you should change both tcp_keepalive_time & tcp_keepalive_intvl to shorter values. If you change these values it is global so all sockets opened after the change will use the new values. If you want the change to be only for a specific socket use the setsockopt with the level IPPROTO_TCP and the option name TCP_KEEPIDLE & TCP_KEEPINTVL. You could also change the option TCP_KEEPCNT to change the number of probes before it take action (default is 9). 

 

To detect a failure you need have a read posted and you will receive a read timeout error. 

 

Hope this helps.
0 Kudos
Altera_Forum
Honored Contributor II
671 Views

Hi, 

 

OK it works fine. The main difference with "standard" code that I could seen, is the need to set IPPROTO_TCP in the socket function call (instead of 0). 

 

For convenience, I give the final code that works fine :  

 

<div class='quotetop'>QUOTE </div> 

--- Quote Start ---  

if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) 

  { 

  TRACE_CONNECT "Error : cannot create the control socket\n"); 

  return -1; 

  } 

 

/* Set options of that socket */ 

sockval = TCP_KEEPIDLE_VALUE; 

if (setsockopt (fd, SOL_TCP, TCP_KEEPIDLE, (void *)&sockval, sizeof(sockval)) == -1) 

  { 

  TRACE_CONNECT "Warning : TCP_KEEPIDLE option set failed\n"); 

  } 

 

sockval = TCP_KEEPCNT_VALUE; 

if (setsockopt (fd, SOL_TCP, TCP_KEEPCNT, (void *)&sockval, sizeof(sockval)) == -1) 

  { 

  TRACE_CONNECT "Warning : TCP_KEEPCNT option set failed\n"); 

  } 

 

sockval = TCP_KEEPINTVL_VALUE; 

if (setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &sockval, sizeof(sockval)) == -1) 

  { 

  TRACE_CONNECT "Warning : TCP_KEEPINTVL option set failed\n"); 

  } 

 

sockval = SO_KEEPALIVE_VALUE; 

if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &sockval, sizeof(sockval)) == -1) 

  { 

  TRACE_CONNECT "Warning : KeepAlive option set failed\n"); 

  } 

 

/* Permanent try of connection */ 

while (1)  

  { 

  memset(&adsock_server, 0, sizeof(adsock_server)); 

  adsock_server.sin_family      = AF_INET; 

  adsock_server.sin_port        = htons(TCPPort); 

  adsock_server.sin_addr.s_addr = htonl(IPAddress); 

   

    if (connect(fd, (struct sockaddr *)&adsock_server, sizeof(adsock_server)) == -1) 

TRACE_CONNECT "Warning : connection to server failed\n"); 

}[/b] 

--- Quote End ---  

0 Kudos
Srirams
Employee
591 Views

Hi I am from Bangalore, India team. I am trying to implement Keep alive to get some advantage in Test time. I am facing some challenges with respect to Vmin forwarding.  Is there any one who could help me out in this. Thank you very much. 

0 Kudos
Reply