Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Altera_Forum
Honored Contributor I
881 Views

Check the TCP connection still active

Hi, 

I use the Iniche stack and i started from the SimpleSocketServer example. 

 

When a client (PC) open a connection for example with Telnet and not close this TCP connection, no connection is possible after that !!  

Of course if in the menu the PC choose Q to close connection it works fine and can re-connect. 

 

I've found flags that do a KEEPALIVE chek with TCP_timer and so on. I can't do it work. 

Shurely i didn't understand everything. 

Is anybody already use it in Iniche Stak ? 

 

Thanks for your experience. 

 

Bvincent1
0 Kudos
4 Replies
Altera_Forum
Honored Contributor I
43 Views

I never used the KEEPALIVE options. 

 

In cases similar to yours I manage the issue in an alternate way,  

allowing the server to force closing the connection when the client is no longer using it. 

Since you use the SimpleSocketServer example, this can be easily done by setting to FALSE the state field of SSSConn struct. Just remember to save the SSSConn pointer to a static copy when the connection is established, so that you can use it when you need it. 

 

Which way you use to detect the 'dead connection' condition (and then force it to close) 

depends on your application. For example you can define your application communication protocol so that you add dummy data transfers which act as keep alive messages. 

If server doesn't receive at least one of them after a predefined time, it closes the connection in the above manner. 

Another option I sometimes used, is to use an additional socket on another port whose only purpose is to act as backdoor to unlock the main connection. 

This is not the best solution, but it has the advantage it is very easy to implement. 

 

Regards
Altera_Forum
Honored Contributor I
43 Views

Thank you Chris72 of your interest. 

I have found some start of way in : 

/altera.components/Nios II Software Packages/altera_iniche/UCOSII/src/tcp/tcp_timr.h 

 

There are few# define like : 

#define TCPTV_KEEP_INIT (75*PR_SLOWHZ) /* initial connect keep alive */ # define TCPTV_KEEP_IDLE (120*60*PR_SLOWHZ) /* dflt time before probing */ # define TCPTV_KEEPINTVL (75*PR_SLOWHZ) /* default probe interval */ # define TCPTV_KEEPCNT 8 /* max probes before drop */  

I'have try to change the TCPTV_KEEP_IDLE value to 5*60 (from 2h to 5 minutes) 

and add the lines : 

if (setsockopt(fd_listen, SOL_SOCKET, SO_KEEPALIVE, &sockval, sizeof(sockval)) == -1) { printf("Warning : KeepAlive option set failed\n"); } 

after the socket() call. 

But it doesn't work, or not like i hope. 

 

To be countinued...
Altera_Forum
Honored Contributor I
43 Views

Altera_Forum
Honored Contributor I
43 Views

Hi, 

 

It works well !! 

code is : 

if ((fd_listen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { alt_NetworkErrorHandler(EXPANDED_DIAGNOSIS_CODE," Socket creation failed"); } sockval = 1; if (setsockopt(fd_listen, SOL_SOCKET, SO_KEEPALIVE, &sockval, sizeof(sockval)) == -1) { printf("Warning : KeepAlive option set failed\n"); } 

 

and when the function is call recv(), capture the rx_code : if ==-1 

rx_code = recv(conn->fd, conn->rx_wr_pos, SSS_RX_BUF_SIZE - (conn->rx_wr_pos - conn->rx_buffer) -1, 0); if(rx_code > 0) { conn->rx_wr_pos += rx_code; /* Zero terminate so we can use string functions */ *(conn->rx_wr_pos+1) = 0; } else if(rx_code == -1) // Case of timeout lost TCP connection { conn->close = 1; } 

 

My mistake was in not capture the returned code from recv(). 

 

I hope it will help somebody else.
Reply