我目前正在构建一些网络应用程序。其中一种方法ANDgate()
使用套接字来传输一些字节。客户端
public static ArrayList ANDgate(ArrayList bits, ArrayList macs, ArrayList tags, int gateNumber) throws IOException, GeneralSecurityException, ClassNotFoundException
{
Socket s= new Socket("192.168.1.109", 8888);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream ios = new ObjectInputStream(s.getInputStream());
//Some computation
oos.writeByte(p);
oos.flush();
oos.writeByte(p1);
oos.flush();
//Some computation
byte P = ios.readByte();
byte P1 = ios.readByte();
//Some computation
oos.writeByte(p_shared1);
oos.flush();
//Some computation
byte p_shared2 = ios.readByte();
//Some computation
oos.writeByte(p1_shared2);
oos.flush();
//Some computation
oos.close();
ios.close();
s.close();
}
在服务器端,我有几乎相同的代码。运行程序时,该方法可以调用并成功执行1000次(或更多),但随机出现如下错误
Exception in thread "main" java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at Gate.ANDgate(Gate.java:119)
119线是这个ObjectInputStream ios = new ObjectInputStream(s.getInputStream());
预先感谢您的帮助