boost::asio::ip::tcp::socket is connected?(如何知道socket已经连接?)

【学习总结】boost::Asio网络编程 一、同步or异步 1. 连接过程 客户端client 服务器端server 2. 同步编程实例 3. 异步编程实例 4. 异常处理&错误码 二、Tcp连接&长连接 1. 短连接 2. 长连接 三、Websocket和Soket boost::asio库支持TCP、UDP和ICMP通信协议,在名字空间boost::asio::ip中提供大量网络通信方面的函数和... 阅读详情

翻译:华亮      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 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.

由于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.




link | improve this answer

boost::asio tcp socket 的使用示例 // ThreadTest.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "boost/thread.hpp" #include "boost/shared_ptr.hpp" #include "boost/asio.hpp" #include "boost/bind.hpp" #include #include typedef 阅读详情

相关推荐

Boost.Asio 网络编程([译]Boost.Asio基本原理)

Boost.Asio基本原理 这一章涵盖了使用Boost.Asio时必须知道的一些事情。我们也将深入研究比同步编程更复杂、更有乐趣的异步编程。 网络API 这一部分包含了当使用Boost.Asio编写网络应用程序时必须知道的事情。 Boost.Asio命名空间 Boost.Asio的所有内容都包含在boost::asio命名空间或者其子命名空间内。 boost::asio:这是核心类和...

hyl999的专栏 5712

boost::asio::ip::tcp实现网络通信的小例子

原文地址:http://www.cppblog.com/janvy/archive/2010/03/24/110478.html 服务端: Boost.Asio是一个跨平台的网络及底层IO的C++编程库,它使用现代C++手法实现了统一的异步调用模型。 头文件 #include <boost/asio.hpp> 名空间 using ...

weixin_33979363的博客 981

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 t

记事本 8041

【C++boost::asio网络编程】有关socket的创建和连接的笔记

本文主要简单介绍boost库中对于socket创建及绑定监听等操作

#include>的博客 1546

Boost.Asio学习笔记2——Socket基础操作

我们知道Linux底层有一套Socket的API用以实现网络连接功能,但由于它被设计为⼀个⾮常通⽤的 API,⽀持许多不同的协议,因此它相当复杂且使⽤起来比较麻烦。Boost.Asio封装了原始的套接字 API,并为开发者提供了⾯向对象的接⼝。

qq_41007891的博客 1765

1. boost::asiosocket的创建和连接

网络编程的基本流程对于服务端是这样的服务端1)socket——创建socket对象。2)bind——绑定本机ip+port。3)listen——监听来电,若在监听到来电,则建立起连接。4)accept——再创建一个socket对象给其收发消息。原因是现实中服务端都是面对多个客户端,那么为了区分各个客户端,则每个客户端都需再分配一个socket对象进行收发消息。5)read、write——就是收发消息了。对于客户端是这样的客户端1)socket——创建socket对象。

yang_pi_pi的博客 825

boostasioTCP通信

asio支持TCP、UDP、ICMP等通信协议,它在名字空间boost::asio::ip里提供了大量的网络通信方面的函数和类,很好的封装了Socket API。

OceanStar的博客 2419

boost::asiosocket的创建和连接

服务器和客户端的通信流程大致如下:是一种用于网络通信的编程接口,它提供了一种机制,使不同主机上的进程能够通过网络进行数据交换。

m0_73523314的博客 1392

自研操作系统中基于boost asio库的同步数据收发

自研车载操作系统中,移植boost库,需要在自研系统上基于boost asio库,基于socket通信调通同步数据收发功能。

笑得没心没肺的专栏 1082

asiosocket创建与连接的基础实现和与C风格的socket网络通信的对比

asio socket创建与连接的基础实现和与C风格的socket网络通信的对比

CodeDingHai的博客 1597

[Boost基础]并发编程——asio网络库——同步socket处理

网络通信简述 asio库支持TCP,UDP和ICMP通信协议,它在名字空间boost::asio::ip里提供了大量的网络通信方面的函数和类,很好的封装了原始的Berkeley Socket API,展现给asio用户一个方便易用且健壮的网络通信库。 ip::tcp类是asio网络通信(TCP)部分主要的类,但它本身并没有太多的功能,而且定义了数个用于TCP通信的typedef类型,用来协

超越 4115

boost::asio库的TCP连接以及类详细摘要

asio库的简明笔记 boost库作为C++准标准库,特别是类摘要写的极其规范和简明,是一个榜样。 1 头文件 #include <iostream> #include <boost/array.hpp> #include <boost/asio.hpp> using boost::asio::ip::tcp; 2 boost::asio::io_service类 负责事件的处理 class io_servie :private nocopyable // 不可拷贝,将

m0_47178337的博客 3576

asiotcp

asio库支持TCP、UDP和工CMP等通信协议,它在名字空间boost::asio :: ip里提供了大量的网络通信方面的函数和类,很好地封装了原始的Socket API,展现给asio用户一个方便易用且健壮的网络通信库,下面的论述主要针对使用最广泛的TCP协议。类ip ::tcpasio网络通信(TCP)部分主要的类,表示TCP协议。但它本身并没有太多的功能,而是定义了数个用于TCP通信的typedef类型,用来协作完成网络通信。

qq_62309585的博客 1587

boost/asio的同步网络核心组件(完善中)

boost::asio::ip::tcp::endpoint(upd类似)类用于表示一个 TCP 网络通信的端点,它封装了 IP 地址和端口号的信息。在网络编程中,端点是通信双方进行数据交换的基础,通过指定端点,程序可以确定与哪个 IP 地址和端口进行连接或监听。io_context 本质上是一个事件循环(Event Loop)管理器,它负责跟踪和调度所有注册到其中的异步操作,当这些操作完成时,会触发相应的回调函数。

m0_73359068的博客 1073

boost::asio::ip::tcp::socket set_option

请注意,这个例子假设服务器在本地机器(127.0.0.1)的8080端口上运行,并且已经设置好了监听该端口的服务。实际使用时,需要根据实际情况调整服务器的IP地址和端口。是一个用于异步I/O操作的类,它是Boost.Asio库的一部分,专门用于处理TCP套接字。方法读取数据,并将其打印到控制台。最后,我们关闭了与服务器的连接。对象,指定服务器的IP地址和端口。为了接收服务器的响应,我们使用。连接成功后,我们使用。在这个例子中,我们首先创建了一个。然后,我们创建了一个。以下是一个简单的使用。

sun007700的专栏 1071

asio::ip::tcp学习

摘要 Boost.AsioTCP网络编程核心组件包括: tcp::socket:表示已建立的TCP连接端点,管理连接生命周期和I/O操作。需注意异步操作的生命周期管理,推荐使用shared_ptr保活会话对象。 tcp::acceptor:监听器组件,负责绑定端口、监听和接受新连接。必须实现accept循环才能持续接收多个连接连接管理:提供同步/异步读写接口,支持半关闭(shutdown)操作。客户端使用resolver进行DNS解析获取endpoint。 常见问题:包括忘记accept循环、并发写冲

ht_vIC的博客 595
上一篇: 关于asio中的io_service::run函数在没有任务时退出的问题
下一篇: Thread Interruption in Boost Thread Library
cedricporter
博客等级 码龄19年 47粉丝 37原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值