关键词不能为空

当前您在: 主页 > 英语 >

Netty 中 IOException Connection reset by peer 与 java.nio.chan

作者:高考题库网
来源:https://www.bjmy2z.cn/gaokao
2021-02-13 11:11
tags:

-

2021年2月13日发(作者:goldilocks)


Netty




IOException: Connection reset by peer




ChannelException: null


最近发现系统中出现了很多



IOException: Connection reset by peer




ClosedChannelException: null


深入看了看代码


,


做了些测试


,


发现



Connection reset


会在客户端不知道



channel


被关


闭的情况下


,


触发了



eventloop




()


操作抛出





ClosedChannelException


一般是由



Netty


主动抛出的


,




AbstractChannel


以及



SSLHandler


里都可以看到



ClosedChannel


相关的代码



AbstractChannel


static final ClosedChannelException CLOSED_CHANNEL_EXCEPTION =



new ClosedChannelException();



...







static {










CLOSED_CHANNEL_ ckTrace(_ST


ACK_TRACE);










NOT _YET_CONNECTED_ckTrace(


_STACK_TRACE);






}



...



@Override










public void


write


(Object msg, ChannelPromise promise) {














ChannelOutboundBuffer outboundBuffer


=


ndBuffer;














if


(outboundBuffer ==


null


) {


















// If the outboundBuffer is null we know the channel was


closed and so


















// need to fail the future right away. If it is not null the


handling of the rest


















// will be done in flush0()


















// See /netty/netty/issues/2362



















safeSetFailure(promise,


CLOSED_CHANNEL_EXCEPTION);


















// release message now to prevent resource- leak



















e(msg);


















return;














}














sage(msg, promise);










}



在代码的许多部分


,


都会有这个



ClosedChannelException,


大概的意思是说在



channel


close


以后


,


如果还调用了



write


方法


,


则会将



write




future


设置为



failure,


并将



cause


设置为



ClosedChannelException,


同样



SSLHandler


中也类似



-----------------


回到



Connection reset by peer,


要模拟这个情况比较简单


,


就是在



server


端设置一个在



channelActive


的时候就



close channel




handler.


而在



client


端则写一个



Connect



功后立即发送请求数据的



listener.


如下



client






public static void main(String[] args) throws IOException,


InterruptedException {










Bootstrap b


=


new Bootstrap();










(new NioEventLoopGroup())

















.channel()

















.handler(new ChannelInitializer



() {






















@Override






















protected void initChannel(NioSocketChannel ch)


throws Exception {






















}


















});










t(



,


8090


).addListener(new


ChannelFutureListener() {














@Override














public void operationComplete(ChannelFuture future) throws


Exception {


















if


(ess()) {






















l().


write


(().writeBytes(



.getBytes ()));






















l().flush();


















}














}










});



server


public class SimpleServer {







public static void main(String[] args) throws Exception {











EventLoopGroup bossGroup


= new NioEventLoopGroup(


1


);










EventLoopGroup workerGroup


=


new NioEventLoopGroup();










ServerBootstrap b


=


new ServerBootstrap();










(bossGroup, workerGroup)

















.channel()

















.option(_REUSEADDR,


true


)

















.childHandler(new ChannelInitializer



()


{






















@Override






















protected void initChannel(NioSocketChannel ch)


throws Exception {


























ne().addLast(new SimpleServerHandler());






















}


















});










(


8090


).


s ync


().channel().closeFuture().

sync


();






}


}




public class SimpleServerHandler extends ChannelInboundHandlerAdapter {






@Override






public void channelActive(ChannelHandlerContext ctx) throws Exception {










l().close().


sync


();






}







@Override






public void channelRead(ChannelHandlerContext ctx, final Object msg)


throws Exception {










n(


123


);






}







@Override






public void channelInactive(ChannelHandlerContext ctx) throws Exception


{










n(



);






}


}




这种情况之所以能触发



connection reset by peer


异常


,


是因为



connect


成功以后


, client


段先会触发



connect


成功的



listener,


这个时候



server


段虽然断开了



channel,


也触发



channel


断开的事件



(


它会触发一个客户端



read


事件


,


但是这个



read


会返回



-1, -1





channel


关闭


, client




channelInactive




channel


active


状态的改变都是在这时


发生的


),


但是这个事件是在



connect


成功的



listener


之后执行


,


所以这个时候



listener


里的



channel


并不知道自己已经断开


,


它还是会继续进行



write




flush


操作


,


在调用



flush



, eventloop


会进入



OP_READ


事件里


,


这时候



()


就会抛出



connection reset


异常


. eventloop


代码如下



NioEventLoop


private static void processSelectedKey(SelectionKey k, AbstractNioChannel


ch) {










final NioUnsafe unsafe


=


();










if


(!


d()) {














// close the channel if the key is not valid anymore















(omise());














return;










}











try {














int


readyOps =


ps();














// Also check for readOps of 0 to workaround possible JDK bug


which may otherwise lead














// to a spin loop















if


((readyOps & (_READ |


_ACCEPT)) !=


0


|| readyOps ==


0


) {


















();

-


-


-


-


-


-


-


-



本文更新与2021-02-13 11:11,由作者提供,不代表本网站立场,转载请注明出处:https://www.bjmy2z.cn/gaokao/649664.html

Netty 中 IOException Connection reset by peer 与 java.nio.chan的相关文章

  • 余华爱情经典语录,余华爱情句子

    余华的经典语录——余华《第七天》40、我不怕死,一点都不怕,只怕再也不能看见你——余华《第七天》4可是我再也没遇到一个像福贵这样令我难忘的人了,对自己的经历如此清楚,

    语文
  • 心情低落的图片压抑,心情低落的图片发朋友圈

    心情压抑的图片(心太累没人理解的说说带图片)1、有时候很想找个人倾诉一下,却又不知从何说起,最终是什么也不说,只想快点睡过去,告诉自己,明天就好了。有时候,突然会觉得

    语文
  • 经典古训100句图片大全,古训名言警句

    古代经典励志名言100句译:好的药物味苦但对治病有利;忠言劝诫的话听起来不顺耳却对人的行为有利。3良言一句三冬暖,恶语伤人六月寒。喷泉的高度不会超过它的源头;一个人的事

    语文
  • 关于青春奋斗的名人名言鲁迅,关于青年奋斗的名言鲁迅

    鲁迅名言名句大全励志1、世上本没有路,走的人多了自然便成了路。下面是我整理的鲁迅先生的名言名句大全,希望对你有所帮助!当生存时,还是将遭践踏,将遭删刈,直至于死亡而

    语文
  • 三国群英单机版手游礼包码,三国群英手机单机版攻略

    三国群英传7五神兽洞有什么用那是多一个武将技能。青龙飞升召唤出东方的守护兽,神兽之一的青龙。玄武怒流召唤出北方的守护兽,神兽之一的玄武。白虎傲啸召唤出西方的守护兽,

    语文
  • 不收费的情感挽回专家电话,情感挽回免费咨询

    免费的情感挽回机构(揭秘情感挽回机构骗局)1、牛牛(化名)向上海市公安局金山分局报案,称自己为了挽回与女友的感情,被一家名为“实花教育咨询”的情感咨询机构诈骗4万余元。

    语文