Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

c - how to detect TCP keep alive packets and keep connection open

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.

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

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)

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

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

(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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...