Solved

socket programming: keep connection alive

Posted on 2003-10-24
3
1,822 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:cero
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
mtmike earned 250 total points
ID: 9618080
Setting the SO_KEEPALIVE socket option has a timeout of at least two hours. Some systems support the per-socket TCP_KEEPIDLE option that can be used to set this timeout. This is not very portable and only useable for TCP.

It's possible to implement keepalive by sending dummy packets at a given interval, but this doesn't sound very attractive to me. AFAIK you cannot send protocol-level NOP packets yourself.

I think the second option is the best one. You don't have to query the socket before sending though. Simply send the packet and try to reopen the connection when the send fails with EPIPE. This method has very little overhead since the reopening logic will be in the error-checking path.
0
 
LVL 2

Author Comment

by:cero
ID: 9634126
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.
0
 
LVL 5

Expert Comment

by:mtmike
ID: 9634501
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)'.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C Programming - If Statement 8 84
Super Scope, DHCP 5 101
Windows 2012 R2 -- create 3.5TB GPT from unallocated space ? 1 92
A  &&  B  || (C||D) how it works in c 7 189
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question