我正在尝试实现一个小游戏,其中多个客户端必须连接到服务器,并一起玩。每个人都有一个不同的GUI,他们通过ClientConnect类进行通信,ClientConnect是一个从GUI类开始的Runnable。问题是我在第一行代码中遇到EOFException,当时我正在尝试输入输入流。服务器当然是从头开始并发送一个对象。 这是我得到异常的实现的一部分。
我该怎么办?
public ClientConnect(InetAddress address, int port) throws IOException {
clientSock = new Socket(address, port);
}
@Override
public void run() {
ArrayList<Object> receivedObject = null;
try (ObjectInputStream fromServer = new ObjectInputStream(clientSock.getInputStream());
ObjectOutputStream toServer = new ObjectOutputStream(clientSock.getOutputStream()))
{
while(!move) {
receivedObject = (ArrayList<Object>) fromServer.readObject();
move = !(receivedObject.get(0).equals("you have to wait!!"));
}
actualPlayer = (String) receivedObject.get(0);
scoreCard = (List<String>) receivedObject.get(1);
highScore = (HashMap<String, Integer>) receivedObject.get(2);
numberOfPlayers = (int) receivedObject.get(3);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
以下是例外:
CShellExt::CShelljava.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 edu.hm.se2.kniffel.ClientConnect.run(ClientConnect.java:41)
at java.lang.Thread.run(Unknown Source)
Ext()
第41行是try
的行我正在GUI中启动ClientConnect线程:
public void actionPerformed(ActionEvent arg0) {
//Online-Spiel starten
if(game.listLength()>0){
try {
client = new ClientConnect(InetAddress.getByName(null), 2000);
new Thread(client).start();
while (!client.getMove()) {
lblActualUser.setText("WAITING.... " + client.getActualPlayer() + " ist an der Reihe");
}
答案 0 :(得分:0)
知道了!
我有阻止打开serversocket的东西!
答案 1 :(得分:0)
根据这个证据,对等方根本没有创建ObjectOutputStream
,它只是连接然后关闭套接字。
您应该在ObjectOutputStream
之前构建ObjectInputStream
两端。