为什么读不到第二个响应包,其中的MPLogin01Response 都是相应的应用层协议包, 如何判断读成功呢?public class TestSearchSetParamBySocket { private Socket socket;
private String host = "11.113.11.38";
private int port = 27442;
DataInputStream dis;
DataOutputStream dos; public TestSearchSetParamBySocket() { try {
this.socket = new Socket(InetAddress.getByName(this.host), this.port);
System.out.println("Connect to " + this.host + " successful.");
socket.setSoTimeout(3000); dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream()); //发送登录请求
sendLogonPack();
System.out.println("接收登录响应包...");
MPLogin01Response loginRespPack = new MPLogin01Response();
loginRespPack.loadFromStream(dis); //发送SearchSetParam请求
sendSearchSetParamPack();
System.out.println("接收SearchSetParam响应包...");
MPSearchSetParam01Response pack = new MPSearchSetParam01Response();
pack.loadFromStream(dis); } catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (dis != null) {
dis.close();
}
if (dos != null) {
dos.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
} private void sendSearchSetParamPack() { System.out.println("发送SearchByParam请求包..."); byte[] pack = null; try {
pack = encodeSearchSetParamPack();
dos.write(pack);
// String fn = "e:\\test.dat";
// OutputStream os = new DataOutputStream(new FileOutputStream(fn));
// os.write(pack);
dos.flush();
} catch (IOException ex) {
ex.printStackTrace();
} catch (BaseException ex) {
ex.printStackTrace();
}
}报错输出为:
发送登录请求...
接收登录响应包...
发送SearchByParam请求包...
接收SearchSetParam响应包...
java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:250)
at protocol.MsgBase.readInteger(MsgBase.java:952)
at protocol.MPSearchSetParam01Response.loadFromStream(MPSearchSetParam01Response.java:29)
at test.TestSearchSetParamBySocket.<init>(TestSearchSetParamBySocket.java:44)
at test.TestSearchSetParamBySocket.main(TestSearchSetParamBySocket.java:158)
private String host = "11.113.11.38";
private int port = 27442;
DataInputStream dis;
DataOutputStream dos; public TestSearchSetParamBySocket() { try {
this.socket = new Socket(InetAddress.getByName(this.host), this.port);
System.out.println("Connect to " + this.host + " successful.");
socket.setSoTimeout(3000); dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream()); //发送登录请求
sendLogonPack();
System.out.println("接收登录响应包...");
MPLogin01Response loginRespPack = new MPLogin01Response();
loginRespPack.loadFromStream(dis); //发送SearchSetParam请求
sendSearchSetParamPack();
System.out.println("接收SearchSetParam响应包...");
MPSearchSetParam01Response pack = new MPSearchSetParam01Response();
pack.loadFromStream(dis); } catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (dis != null) {
dis.close();
}
if (dos != null) {
dos.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
} private void sendSearchSetParamPack() { System.out.println("发送SearchByParam请求包..."); byte[] pack = null; try {
pack = encodeSearchSetParamPack();
dos.write(pack);
// String fn = "e:\\test.dat";
// OutputStream os = new DataOutputStream(new FileOutputStream(fn));
// os.write(pack);
dos.flush();
} catch (IOException ex) {
ex.printStackTrace();
} catch (BaseException ex) {
ex.printStackTrace();
}
}报错输出为:
发送登录请求...
接收登录响应包...
发送SearchByParam请求包...
接收SearchSetParam响应包...
java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:250)
at protocol.MsgBase.readInteger(MsgBase.java:952)
at protocol.MPSearchSetParam01Response.loadFromStream(MPSearchSetParam01Response.java:29)
at test.TestSearchSetParamBySocket.<init>(TestSearchSetParamBySocket.java:44)
at test.TestSearchSetParamBySocket.main(TestSearchSetParamBySocket.java:158)
这个异常抛出,是因为,你在读取IO流的时候,InputStream已经读到最后了(或者说已经结束了)但是,你仍然读,这时,就会抛出这个异常。
一般在网络通信,或者读取文件的时候,会遇到这样的问题。
网络通信,一般也是在传输文件的时候抛出该异常。当然,传输特殊协议的时候也可能会抛出该异常。楼主可以检查两个方面的问题:
一、看看是否是发送端发送的数据要不接收端接收的数据少。
二、看看协议当中,是不是对应的字节位理解错误了。比如协议上说包头长度用2个字节表示,你给理解成4个字节表示长度;或者读取过程中重复读取了某个信息块,但没有发觉,等等。