boost asio 中文教程:asio很烂吗

温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
boost::asio::ip::tcp::acceptor acceptor(io_service);& boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), port);& acceptor.open(endpoint.protocol());& acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));& acceptor.bind(endpoint);& acceptor.listen();& & 2.还有一种方法 就是在acceptor的构造函数,下面是acceptor的构造函数,我们看到,第三个参数名reuse_addr 。默认是可以端口复用的,要设置是否复用,也可以通过该处传参。& basic_socket_acceptor(boost::asio::io_service& io_service,const endpoint_type& endpoint, bool reuse_addr = true)
阅读(4998)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'boost asio socket 端口复用',
blogAbstract:'& &1. 在boost的acceptor类说明中有这样的一个例子。用set_option设置端口是否可复用。 boost::asio::ip::tcp::acceptor acceptor(io_service);& boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), port);& acceptor.open(endpoint.protocol());&',
blogTag:'boost,端口复用',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:4,
publishTime:8,
permalink:'blog/static/',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'0',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}本帖子已过去太久远了,不再提供回复功能。下次自动登录
现在的位置:
& 综合 & 正文
boost::asio 无法接受新连接的处理方法
写了一个压力测试程序,创建2000个连接,TCP server基于boost::asio实现。用kill杀掉2000个连接,再重新创建,如此反复多次后,TCP server不再接受新的连接,已经建立的连接仍然能够正常工作。用wc -l /proc/net/tcp 命令检查tcp 连接数,不存在大量未被系统关闭的连接。
在stackoverflow上问了别人,帖子:
有人提醒用netstat -ap命令检查,发现有很多CLOSE_WAIT状态的连接,这个状态分析参考
然后添加了一行代码:
acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
同时编码时要注意在调用close方法之前调用shutdown。
void CloseSocket() {
socket.shutdown(tcp::socket::shutdown_both);
socket.close();
BOOST_LOG_TRIVIAL(warning) && "close the connection";
} catch (std::exception& e) {
BOOST_LOG_TRIVIAL(warning) && "thread id: " && this_thread::get_id() && " " && e.what();
搞定,现在试了几十次,TCP server都能正常接受新的连接了。
&&&&推荐文章:
【上篇】【下篇】

我要回帖

更多关于 boost asio write 的文章

 

随机推荐