Operating System - HP-UX
1821642 Members
2819 Online
109633 Solutions
New Discussion юеВ

Using SO_KEEPALIVE option in setsockopt to detect cable break

 
phil atkinson
Advisor

Using SO_KEEPALIVE option in setsockopt to detect cable break

I have a java 1.2 client application on a PC which is communicating to a C++ server application hosted on HPUX 10.20 or HPUX 11.00

If I disconnect the PC by pulling the ethernet cable while the java client is connected to the server - then kill the java client - then reconnect the ethernet cable.

The server remains connected and netstat reports TCP Connection established.

I thought to resolve the problem using the TCP keepalive timer at the application level.

int keep;
m_fd = ::socket(AF_INET,SOCK_STREAM,0);
int len = sizeof(int);
getsockopt(m_fd,SOL_SOCKET,SO_KEEPALIVE,&keep,&len);
//keep is set to 0
keep= 30000; // 5mins I think
setsockopt(m_fd,SOL_SOCKET,SO_KEEPALIVE,&keep,len);
if (!::setsockopt(m_fd,SOL_SOCKET,SO_REUSEADDR,&enable,sizeof(int)) &&
!bind(m_fd, reinterpret_cast(&localAddress), sizeo
f(sockaddr_in)) &&
!::listen(m_fd,SOMAXCONN) && (::fcntl(m_fd,F_SETFL,::fcntl(m_fd,F_GETFL,0) | O_NONBLOCK) != -1))
{
.....


This code does not seem to do anything.

Has anyone any ideas how to resolve the problem?
2 REPLIES 2
vijay_37
Occasional Advisor

Re: Using SO_KEEPALIVE option in setsockopt to detect cable break


Hi,

I hope you would have implemented KEEPALIVE in c++code ie server application. I would also suggest to check the return value of setsockopt.

from
Vijay
Zigor Buruaga
Esteemed Contributor

Re: Using SO_KEEPALIVE option in setsockopt to detect cable break

Hi,

Agree with Vijay, you need KEEPALIVE on both sides. It looks like you don??t have the appropiate code in the C++ program, or maybe you need to wait more time ( KEEPALIVE time interval )?
If your PC is running Windows also need to add some keys to registry.

Hope this helps.
Kind regards,
Zigor