插座,检测连接丢失 [英] socket, detect connection is lost
问题描述
我连接一个服务器进程和一个TCP连接的客户端的过程,我必须检测结果
两台机器之间的物理连接已断开。我试图做到这一点使用的存活时间,结果
降低默认的系统范围内的值:
I'm connecting a server process and a client process with a TCP connection, and I have to detect
that physical connection between the two machines is down. I'm trying to do this using the keepalive,
decreasing the default system wide values to:
TCP_KEEPIDLE = 5结果
TCP_KEEPCNT = 5结果
TCP_KEEPINTVL = 1
TCP_KEEPIDLE=5
TCP_KEEPCNT = 5
TCP_KEEPINTVL = 1
当连接出现故障(我断开连接线)仅在10秒内服务器检测到连接已丢失,客户端只是挂起的发送。
When the connection goes down ( I disconnect the cable ) only the server in 10 seconds detect that the connection has been lost, the client just hangs on the send.
这是客户端code:
#include <iostream>
#include <string.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/tcp.h>
int main(int argc, char** argv) {
char myVector[1600];
int mySocket = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
if (mySocket < 0 ) {
std::cout << "error creating the socket" << strerror(errno) << std::endl;
::exit(-1);
}
struct sockaddr_in sin;
memset( (char *)&sin, 0, sizeof( sin ) );
sin.sin_addr.s_addr = inet_addr("192.168.21.27");
sin.sin_port = htons(7788);
sin.sin_family = AF_INET;
if ( connect( mySocket, (struct sockaddr *)&sin, sizeof( sin )) < 0 ) {
std::cout << "Error on connection: " << strerror(errno) << std::endl;
::exit(-1);
}
int optval = 1;
socklen_t optlen = sizeof(optval);
/*Enabling keep alive*/
if(setsockopt(mySocket, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
std::cout << "Error setting SO_KEEPALIVE: " << strerror(errno) << std::endl;
}
optval = 5;
optlen = sizeof(optval);
if(setsockopt(mySocket, SOL_TCP, TCP_KEEPIDLE, &optval, optlen) < 0) {
std::cout << "Error setting TCP_KEEPIDLE: " << strerror(errno) << std::endl;
}
optval = 5;
optlen = sizeof(optval);
if(setsockopt(mySocket, SOL_TCP, TCP_KEEPCNT, &optval, optlen) < 0) {
std::cout << "Error setting TCP_KEEPCNT: " << strerror(errno) << std::endl;
}
optval = 1;
optlen = sizeof(optval);
if(setsockopt(mySocket, SOL_TCP, TCP_KEEPINTVL, &optval, optlen) < 0) {
std::cout << "Error setting TCP_KEEPINTVL: " << strerror(errno) << std::endl;
}
for (;;) {
ssize_t myRet= ::send(mySocket,
myVector,
sizeof(myVector),
0);
if (myRet < 0) {
std::cout << "Error: " << strerror(errno) << std::endl;
break;
}
std::cout << myRet << "."; std::cout.flush();
sleep(1);
}
}
我敢肯定,我失去了一些东西,但什么?
I'm sure I'm missing something, but what ?
推荐答案
TCP保持不用于这种用途。
TCP Keepalive isn't intended for this use.
如果要检测应用层中断,做什么样的SSH,IMAP和IRC协议做 - 实现在应用层回声/平类型的消息。给他们定期,如果没有得到及时回复,连接可以被认为是了。
If you want to detect outages at the application layer, do what protocols like SSH, IMAP and IRC do - implement an echo/ping type message at the application layer. Send them on a regular basis, and if you don't get a timely reply, the connection can be assumed to be down.
这篇关于插座,检测连接丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!