0

我正在尝试实现一个小游戏,其中多个客户端必须连接到服务器并一起玩。每个人都有不同的 GUI,它们通过 ClientConnect 类进行通信,这是一个从 GUI 类开始的 Runnable。问题是我在第一行代码中得到了一个 EOFException,当我试图对输入流进行 intanciate 时。服务器当然是一开始就启动并发送一个对象。这是我得到异常的实现的一部分。

我应该怎么办?

 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");
                    }   
4

2 回答 2

0

知道了!

我有东西阻塞了要打开的服务器套接字!

于 2013-06-27T20:38:46.853 回答
0

根据这个证据,对等点根本没有创建一个ObjectOutputStream,它只是连接然后关闭套接字。

您应该在, 两端构造ObjectOutputStream之前。ObjectInputStream

于 2013-06-28T02:01:22.680 回答