如何检测TCP保持活动数据包并保持连接打开 [英] how to detect TCP keep alive packets and keep connection open

查看:225
本文介绍了如何检测TCP保持活动数据包并保持连接打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用epoll编写了自己的服务器.当我从客户端向服务器发送TCP保持活动包时,epoll事件将不会触发.问:我希望我的服务器在tcp保持活动状态时使连接保持打开状态.

I wrote my own server with epoll. When I sent TCP keep alive packages from client to server, epoll event will not get triggered. Question: I want my server to keep the connection open when server gets tcp keep alive packages.

我也尝试查看tcp信息,但是当服务器保持活动包时,其属性没有更新.

I also tried to look at tcp info but there are no update for its attributes when server got keep alive packages.

我了解tcp keep alive软件包不是数据而是标头.我在tcpdump中看到,内核在收到keep alive程序包后发回tcp keep alive ACK.我的目标是在保持活动状态时保持连接打开(有点像服务器中的重置计时器,我的服务器可以在没有tcp实际数据的情况下关闭连接)

I understand tcp keep alive packages is no data but header. I saw in my tcpdump, kernel sent back tcp keep alive ACK after received keep alive package. My goal is to keep connection open when keep alive arrives (kind like reset timer in my server, my server can close connection in no tcp real data)

我设置了客户端以建立连接并发送数据(例如"hello world"),然后将保持活动的程序包发送到服务器.

I set up my client to make connection and send data (e.g "hello world") then send keep alive packages to server.

我的服务器被epoll触发.我已经尝试将服务器设置为非阻塞状态.

My server is epoll triggered. I already also tried to set my server to be non-blocking.

// epoll setting I tested
EPOLLIN | EPOLLOUT | EPOLLRDHUP | EPOLLET

(我在互联网上发现的大多数问题都与客户端有关,我的问题更多地是关于tcp的服务器或接收方保持活动状态,如何使其保持打开状态)

(most questions I found on internet are related to client side, my question is more towards server or receiver side of tcp keep alive, how to keep it open)

推荐答案

不,据我所知这是不可能的.

No, this is not possible as far as I know.

您将必须在应用程序协议中实现检测信号,然后才能停止使用TCP Keepalive.

You will have to implement a heartbeat in the application protocol, and then you can stop using TCP keepalive.

这篇关于如何检测TCP保持活动数据包并保持连接打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆