Boost ASIO:服务器如何知道客户端是否仍然连接? [英] Boost ASIO: How can a server know if a client is still connected?

查看:496
本文介绍了Boost ASIO:服务器如何知道客户端是否仍然连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为服务器/客户端应用程序使用 boost :: asio 。服务器一次只接受一个连接。我想知道什么是服务器验证客户端是否仍然连接的最佳方式。



这样做的目的是,我想知道客户端是否崩溃,以便我可以重新尝试新的连接尝试。

解决方案

在我的应用程序中,我使用以下标志,当客户端断开连接时我的读取出来。
请在您的应用程序中尝试。 c:> boost:

在我的情况下, skt_TCP :asio :: ip :: tcp :: socket

  int32_t accept_server_socket = skt_TCP.native_handle ; 

int32_t timeout = 8;
int32_t cnt = 2;
int32_t intverval = 2;

//添加Keepalive标志
boost :: asio :: socket_base :: keep_alive opt_keep_alive(true);
skt_TCP.set_option(opt_keep_alive);
setsockopt(accept_server_socket,SOL_TCP,TCP_KEEPIDLE,& timeout,sizeof(timeout));
setsockopt(accept_server_socket,SOL_TCP,TCP_KEEPCNT,& cnt,sizeof(cnt));
setsockopt(accept_server_socket,SOL_TCP,TCP_KEEPINTVL,& intverval,sizeof(intverval));


I am using boost::asio for a server/client application. The server only accepts one connection at a time. I am wondering what is the best way for the server to verify if the client is still connected.

The purpose of this is that I would like to be able to know if the client has crashed, so that I can restart listening to new connection attempts.

解决方案

In my application I am using following flags and my read comes out when client disconnects. Please try it if it in your application. Apply this flags just after connection.

in my case skt_TCP is of type boost::asio::ip::tcp::socket

int32_t accept_server_socket = skt_TCP.native_handle();

int32_t timeout = 8;
int32_t cnt = 2;
int32_t intverval = 2;

// Added Keepalive flag
boost::asio::socket_base::keep_alive opt_keep_alive(true);
skt_TCP.set_option(opt_keep_alive);
setsockopt(accept_server_socket, SOL_TCP, TCP_KEEPIDLE, &timeout, sizeof(timeout));
setsockopt(accept_server_socket, SOL_TCP, TCP_KEEPCNT, &cnt, sizeof(cnt));
setsockopt(accept_server_socket, SOL_TCP, TCP_KEEPINTVL, &intverval, sizeof(intverval));

这篇关于Boost ASIO:服务器如何知道客户端是否仍然连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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