java.net.SocketException: Software caused connection abort: recv failed 异常分析

关键字: recv failed

java.net.SocketException: Software caused connection abort: recv failed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at java.net.SocketInputStream.read(SocketInputStream.java:182)

客户机<-->服务器,之间是由一个socket 长连接来通信,客户端有一个接收线程在while(true){..}循环里不停地从InputStream流中读数据,客户机每隔几秒钟发一次心跳包,如果连续未收到心跳包的次数已达到规定的次数,客户机认为此链路异常,将socket关闭,那么服务器会抛出java.net.SocketException: Connection reset by peer异常,然后分配给此socket连接的线程退出,那么客户端在while(true){..}循环,读取流时便会发java.net.SocketException: Software caused connection abort: recv failed异常。

 

try   {
                   String   host   =   getCodeBase().getHost();
                   byte[]   response   =   new   byte[50500];
                
                      host= "192.168.2.148 ";
                      clientSocket   =   new   Socket(host,16555);
                            //   需要输入和输出流
                    outbound   =   new   DataOutputStream(clientSocket.getOutputStream());
                      inbound      =   new   DataInputStream(clientSocket.getInputStream());
                            //   向服务器发出请求
                       ps   =   new   PrintStream(outbound);

                   byte[]   cmd   =   new   byte[1024];

                   cmd[0]   =   0x7e;
                   cmd[1]   =   0x00;
          
                   ps.write(cmd,0,2);//第一次发送
                 ps.flush();

                   inbound.read(response,   0,   50500);//这里不报异常

                 cmd[0]   =   0x7e;
                   cmd[1]   =   0x01;
    
                   ps.write(cmd,0,2);                              //第二次发送
                 ps.flush();

                   if(inbound.read(response,   0,   50500)==-1))//这里报异常
                          return   false;
                   return   true;
             }
             catch   (IOException   ioe)   {
             System.out.println( "IOException:    "   +   ioe);
             return   false;
       }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值