开发者

durable duplex with TCP without having any KeepAlive() method

开发者 https://www.devze.com 2023-03-06 23:42 出处:网络
I need to implement a long lasting TCP duplex connection for event notification. Client register for events and server informs about this events to all subscribers.

I need to implement a long lasting TCP duplex connection for event notification. Client register for events and server informs about this events to all subscribers.

The problem is, the TCP connection closes after 10 minutes. I know about reliable se开发者_运维技巧ssion. close and inactivity timeouts.

What i need is to implement some kind of "always opened connection" between the client and the server. The client reconnects as soon as it notices the connection is lost. but it can be difficult to notice some connection loses while connected with TCP.

I was able to invent some kind of solution with long receive and inactivity timeouts. Client "pulse checks" the server in some short interval, if it's still online and connected using some kind of KeepAlive() method.

I would like to have the solution without having KeepAlive() method on my service contract.

Any ideas?

//Miro


TCP connections do not close on their own; they're either explicitly closed or one of the end-points has vanished. (Note: An end-point can be a transparent proxy or stateful router in the middle.) I frequently run TCP connections that stay up for days, if not months, even with no keep-alive.

In the case of the former, an explicit close will get your side notified but it sounds like that isn't happening.

In the case of a vanished end-point, sending data is the only way to find out if the other side has gone away, and keep-alive is the transparent way to do that.

int delay = X; setsockopt(sockfd,SOL_TCP,TCP_KEEPIDLE,&delay,sizeof(delay));
int count = X; setsockopt(sockfd,SOL_TCP,TCP_KEEPCNT,&count,sizeof(count));
int interval = X; setsockopt(sockfd,SOL_TCP,TCP_KEEPINTVL,&interval,sizeof(interval));
int enable = 1; setsockopt(sockfd,SOL_SOCKET,SO_KEEPALIVE,&enable,sizeof(enable));

If you don't want to use TCP's keep-alive, then you need to send real data to the server on a periodic basis or find out what along your route from client to server is causing your connection to drop.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号