boost :: asio :: ip :: tcp :: socket已连接? [英] boost::asio::ip::tcp::socket is connected?

查看:426
本文介绍了boost :: asio :: ip :: tcp :: socket已连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在执行读/写操作之前验证连接状态.

I want to verify the connection status before performing read/write operations.

有没有办法制作isConnect()方法?

Is there a way to make an isConnect() method?

我看到了,但看起来难看.

I saw this, but it seems "ugly".

我已经测试过 is_open( )也可以,但没有预期的行为.

I have tested is_open() function as well, but it doesn't have the expected behavior.

推荐答案

TCP旨在在恶劣的网络环境下保持强大的性能.尽管TCP提供了看起来像一个持久的端到端连接,但这只是一个谎言,每个数据包实际上只是一个唯一的,不可靠的数据报.

TCP is meant to be robust in the face of a harsh network; even though TCP provides what looks like a persistent end-to-end connection, it's all just a lie, each packet is really just a unique, unreliable datagram.

连接实际上只是创建的虚拟管道,在连接的两端(源端口和目标端口和地址以及本地套接字)都跟踪了一个小状态.网络堆栈使用此状态来知道将每个传入数据包分配给哪个进程以及将哪个状态放入每个传出数据包的报头中.

The connections are really just virtual conduits created with a little state tracked at each end of the connection (Source and destination ports and addresses, and local socket). The network stack uses this state to know which process to give each incoming packet to and what state to put in the header of each outgoing packet.

由于底层—本质上是无连接且不可靠的—根据网络的性质,只有在远端发送FIN数据包以关闭连接时,或者如果它未收到对已发送数据包的ACK响应(在超时和几次重试之后),堆栈才会报告断开的连接.

Because of the underlying — inherently connectionless and unreliable — nature of the network, the stack will only report a severed connection when the remote end sends a FIN packet to close the connection, or if it doesn't receive an ACK response to a sent packet (after a timeout and a couple retries).

由于asio具有异步特性,因此,通知正常断开连接的最简单方法是拥有一个出色的async_read,当关闭连接时,该async_read将立即返回error::eof.但是,仅此一项,仍可能导致其他问题(如半开连接和网络问题)未被发现.

Because of the asynchronous nature of asio, the easiest way to be notified of a graceful disconnection is to have an outstanding async_read which will return error::eof immediately when the connection is closed. But this alone still leaves the possibility of other issues like half-open connections and network issues going undetected.

解决意外连接中断的最有效方法是使用某种保持活动或ping操作.偶尔通过连接传输数据的尝试将允许方便地检测到意外断开的连接.

The most effectively way to work around unexpected connection interruption is to use some sort of keep-alive or ping. This occasional attempt to transfer data over the connection will allow expedient detection of an unintentionally severed connection.

TCP协议实际上具有内置的保持活跃机制可以使用asio::tcp::socket::keep_alive在asio中配置. TCP keep-alive的好处是它对用户模式应用程序是透明的,只有对keep-alive感兴趣的对等方才需要配置它.缺点是您需要操作系统级别的访问/知识来配置超时参数,不幸的是,它们没有通过简单的套接字选项公开,并且通常具有很大的默认超时值(在Linux上为7200秒).

The TCP protocol actually has a built-in keep-alive mechanism which can be configured in asio using asio::tcp::socket::keep_alive. The nice thing about TCP keep-alive is that it's transparent to the user-mode application, and only the peers interested in keep-alive need configure it. The downside is that you need OS level access/knowledge to configure the timeout parameters, they're unfortunately not exposed via a simple socket option and usually have default timeout values that are quite large (7200 seconds on Linux).

最可能的保持活动状态的方法是在应用程序层实现该功能,在该层应用程序具有特殊的noop或ping消息,并且在发痒时不做任何响应.这种方法为您提供了实施保持活动策略的最大灵活性.

Probably the most common method of keep-alive is to implement it at the application layer, where the application has a special noop or ping message and does nothing but respond when tickled. This method gives you the most flexibility in implementing a keep-alive strategy.

这篇关于boost :: asio :: ip :: tcp :: socket已连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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