boost::asio::ip::tcp::socket is connected?(如何知道socket的链接是链接或断开?)

翻译:华亮      From:http://stackoverflow.com/questions/1511129/boostasioiptcpsocket-is-connected


问题:

I want to verify the connection status before realize my operations (read/write).

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

I saw this, but it seems "ugly".

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

Thanks

大概内容为:在进行网络通讯前,我如何知道当前的连接状态?


回答:

TCP is meant to be robust in the face of a harsh network

TCP注定是在混乱的网络环境中能够保持健壮性,这个是因为TCP就是那么设计的。


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.

尽管TCP在表面看来是提供了一个持续的点对点的连接,但那仅仅是一个假象。每个数据包都是一个独立不可靠的数据报。


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.

所谓连接,只是一个由连接的每个端点保存的一些状态构成的虚拟的管道。网络栈通过这些状态,知道把传入的数据包传给那个进程,也知道把什么状态放到发出的数据包的包头。


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


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 returnerror::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.

由于asio的异步的天性,最容易发现断开连接的方法是在 async_read 里面收到 error::eof。


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.


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

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.

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: boost::asio::ip::tcp::socket是一个基于Boost库的C++网络编程库中的一个类,用于实现TCP/IP协议的套接字通信。它提供了一种异步的、事件驱动的网络编程模型,可以方便地实现高性能、高并发的网络应用程序。 ### 回答2: boost::asio::ip::tcp::socket是基于Boost库中的ASIO(Asynchronous I/O)库中的IP协议栈中的TCP套接字类。ASIO是一个C++的网络编程库,它提供了一套基于事件处理的异步I/O(Asynchronous I/O)框架,使得开发者可以使用相对简单的代码实现高效、可靠的网络应用程序。 boost::asio::ip::tcp::socket类是在tcp协议上实现异步通讯的最基本工具之一。它提供了一组同步和异步的网络I/O操作,可以方便地对TCP连接进行读写操作。同时,该类还提供了对套接字选项设置、获取的操作,例如设置套接字发送缓冲区大小、获取对端IP地址和端口号等等。 基本用法是通过boost::asio::ip::tcp::socket类的构造函数创建一个新的套接字,然后使用成员函数进行IO操作。在使用过程中,可以通过异步模式来进行读写请求,提高了程序的效率。 使用boost::asio::ip::tcp::socket类的一般步骤如下: 1.创建socket对象,使用boost::asio::io_service对象的成员函数socket()创建一个tcp socket对象。 2.与服务器建立连接,使用socket类的connect()函数连接服务器。 3.写入数据到服务器,使用socket类的write_some()函数向服务器写入数据。 4.从服务器读取数据,使用socket类的read_some()函数从服务器读取数据。 5.关闭连接,使用socket类的close()函数关闭连接。 总之,boost::asio::ip::tcp::socket提供了方便、高效的异步网络I/O编程接口,可以帮助开发者实现高效的网络应用程序。 ### 回答3: boost::asio::ip::tcp::socketboost库中的一个TCP套接字类。它提供了一种高级的方式来使用基于流的套接字来进行网络通信。通过该类,可以方便快捷地进行TCP通信,并且可以使用异步操作来实现非阻塞式通信。 在使用boost::asio::ip::tcp::socket时,首先需要创建一个socket对象,并且将其与服务器或客户端关联起来。对于服务器来说,可以将socket对象与一个本地的IP地址和端口号进行绑定,从而使得该socket对象可以监听来自客户端的连接请求。对于客户端来说,则需要将socket对象与服务器的IP地址和端口号进行连接,从而建立一个TCP连接。 一旦socket对象与服务器或客户端建立了连接,就可以使用它进行数据的读取和写入。boost::asio::ip::tcp::socket提供了若干个异步读写操作,例如async_read()和async_write(),这些操作可以让用户使用回调函数来处理读写完成的事件。此外,还提供了许多其他的方法,如close()、shutdown()、bind()等,来控制套接字的状态和行为。 总之,boost::asio::ip::tcp::socket是一个非常方便和强大的类,可以帮助用户快速搭建基于TCP的网络通信程序,尤其是那些需要进行异步操作或者非阻塞式通信的场合。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值