我有三个类:Server,Client和Cap。我正在尝试将Cap从服务器发送到客户端并通过套接字返回。服务器实现了runnable,在run-method中有一个无限的while循环,如下所示:
try {
out.writeObject(new Cap());
out.flush();
Cap cap = (Cap) in.readObject();
boolean bol = false;
if(cap.getValue()==2) {
bol = true;
}
out.writeBoolean(bol);
out.flush();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Something is wrong");
break;
} catch (ClassNotFoundException ex) {JOptionPane.showMessageDialog(null, "Something is wrong");}
}
在run-method中但在while-loop之前我做了这个:
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream())
在Client-class中我也有这样的while循环:
try {
Cap cap = (Cap) in.readObject();
textBox.setText(cap.toString());
while(!JButtonHasBeendPressed) {
}
out.writeObject(cap);
out.flush();
bol = in.readBoolean();
if(bol) {
//Do something
}
else {
//Do something else
}
JButtonHasBeendPressed = false;
} catch (IOException ex) {
//Do something
} catch (ClassNotFoundException ex) {
//Do something
}
}
在while循环之前我做了这个:
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
当我调试客户端程序时,它完全按照我想要的方式工作 - 程序进入“if(bol)”。当我运行程序时,服务器可以将对象发送到客户端,但之后出现问题。正如您在代码中看到的,我在Client-class中有一个JButton,即设置JButtonHasBeendPressed = true,但是当我按下它时没有任何反应。我没有收到错误信息,确实没有任何反应。
当我调试服务器和客户端时,out.writeBoolean(bol)发生IOException;在Server-class中。
汉克