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

來源:互聯網
上載者:User

關鍵字: 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;
       }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.