Software caused connection abort: recv failed

写socket的出现的异常

解决方案:


  try {
   InputStream is = socket.getInputStream();
   BufferedReader br = new BufferedReader(new InputStreamReader(is));
   String line;
   while(true){
     if((line = br.readLine()) != null){
      //do ..
     }
    //设置超时 关闭socket
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
 

注意红色修改部分:

 

try {
   InputStream is = socket.getInputStream();
   BufferedReader br = new BufferedReader(new InputStreamReader(is));
   String line;
   while(true){
    if(is.available()>0){
     if((line = br.readLine()) != null){
      //do ..
     }
    }else{
     break;
    }
    //设置超时 关闭socket
   }
  } catch (IOException e) {
   e.printStackTrace();
  }

你可能感兴趣的:(socket,String,null)