Link to home
Start Free TrialLog in
Avatar of cero
cero

asked on

socket programming: keep connection alive

Hi,

I have an application that maintains an open connection to a server.
I have no control over the server, and sometimes when no action occurs in the socket in a long time the server closes the connection, that's the problem

Well I have the following options to maintain the socket open everytime:

1. I could set the KEEP ALIVE option by means of setsockopt. But AFAIK it sends a NO OPERATION packet when two hours have passed without activity and if I want to manipulate this, I have to change that OPTION in the whole system not in one  connection.

2. Before send the message, I could test if a socket is open, using getpeername or recv with MSG_PEEK. If the connection is closed, reopen it.
This option doesn't convince me enough. I will test this option after all.

3. I could have a way to test if no action has ocurred -in a specified time period- in the socket (it's a sender socket) by means of select for example and if this is the case, then send a NOP packet or doing some activity to avoid the server close mi connection. Its possible? do you have some sample code on this last idea???

4. The last: Use a thread to send packets each ten minutes for example on the open connection. I don't want this option.

Some comments???

thanks,

cero

P.D. sorry but english is not my first language
ASKER CERTIFIED SOLUTION
Avatar of mtmike
mtmike

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cero
cero

ASKER

Thanks,

Well the problem was that I was receiving SIGPIPE instead of EPIPE, and the signal was terminating my program, and when I read your comment I will look at "send" another time and in the flags I found MSG_NOSIGNAL for avoiding the signal SIGPIPE.

Points for you.

cero.
MSG_NOSIGNAL will work, but is not entirely portable. Another, more portable way is to ignore the pipe signal for all file descriptors using 'signal(SIGPIPE, SIG_IGN)'.