关键字: 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;
}