简体   繁体   English

Java,在ObjectOutputStream上调用readObject()时,它仅返回一个值

[英]Java, when calling readObject() on an ObjectOutputStream it is only returning one value

I'm writing a server / client game. 我正在编写服务器/客户端游戏。 My client sends a message to the server and the server is supposed to distribute that message to all other clients connected to the server. 我的客户端向服务器发送一条消息,服务器应该将该消息分发给连接到服务器的所有其他客户端。 The problem I'm having is that when I call ois.readObject() (where ois is the ObjectInputStream created from the ServerSocket accept() call) it only ever returns the very first value that I write to the corresponding ObjectOutputStream on the client side. 我遇到的问题是,当我调用ois.readObject()(其中ois是从ServerSocket accept()调用创建的ObjectInputStream)时,它永远只会返回我写入客户端相应的ObjectOutputStream的第一个值。 。

This is the code for the Thread my server creates for each client that connects. 这是服务器为连接的每个客户端创建的线程的代码。

@Override
public void run() {
    Iterator<ClientThread> itr = null;
    ClientThread ct = null;
    Object recObj = null;
    try {
        oos.writeObject(id);
        oos.flush();

        while(oos != null && ois != null) {
            recObj = ois.readObject();
            System.out.println(recObj);
            if(recObj instanceof Player) {
                System.out.println("[" + id + "] Recieved player from client.");

                player = (Player) recObj;
                Server.playerMap.put(player.getId(), player);

                if(player == null) {
                    System.out.println("Player " + player.getId() + " has joined the server.");
                }

                itr = Server.threadList.iterator();
                while(itr.hasNext()) {
                    ct = itr.next();
                    if(!(ct.id == this.id)) {
                        ct.oos.writeObject(recObj);
                        ct.oos.flush();
                        System.out.println("Sending " + (Player) recObj + " to client " + ct.id + "!");
                    } else {
                        System.out.println("Don't send stuff to " + ct.id + " or we get an infinite loop!");
                    }
                }
            } else {
                System.err.println("Recieved something other than a Player object.");
            }
        }
    } catch (IOException e) {
        System.err.println("[INFO] " + e.getLocalizedMessage());

    } catch (ClassNotFoundException e) {
        System.err.println(e.getLocalizedMessage());
        e.printStackTrace();
    }
    cleanup();
}

This code is for the Thread that each client creates. 此代码适用于每个客户端创建的线程。

@Override
public void run() {
    Object recObj = null;
    Player newPlayer = null;
    try {
        recObj = client.ois.readObject();
        if(recObj instanceof Integer) {
            client.player = new Player((Integer) recObj);
            client.playerMap.put(client.player.getId(), client.player);
        } else {
            System.err.println("First object recieved from server was not of type Integer.");
            System.err.println("No valid id has been set.");
        }

        while(client.ois != null) {
            recObj = client.ois.readObject();
            if(recObj instanceof Player) {
                newPlayer = (Player) recObj;
                System.out.println("Recieved " + newPlayer + "!");
                client.playerMap.put(newPlayer.getId(), newPlayer); 
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

PS I realize I don't need to flush the ObjectOutputStream but it was just an idea to test. PS我意识到我不需要刷新ObjectOutputStream,但这只是测试的一个主意。

每次写入后在ObjectOutputStream上调用.reset。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ObjectOutputStream, readObject 只从序列化文件中读取第一个对象 - ObjectOutputStream, readObject only reads first object from serialized file 在ObjectInputStream上调用readObject()时获取java.io.EOFException - Getting java.io.EOFException when calling readObject() on ObjectInputStream 在Java中使用ObjectOutputStream()时,对象必须相同吗? - Must the object be the same one when using ObjectOutputStream() in java? Java:为什么在调用.readObject()时具有引用的泛型类型的ArrayList不算作强制转换? - Java: Why does an ArrayList with a referred generic type not count as the cast when calling .readObject()? 为什么ObjectOutputStream.readObject()访问MyClass.readObject() - Why does ObjectOutputStream.readObject() access MyClass.readObject() 仅通过一个客户端通过ObjectOutputStream发送对象 - send an object through ObjectOutputStream only one client ObjectOutputStream仅读取赋予它的第一个实例(java)? - ObjectOutputStream reads only the first instance given to it (java)? Java中的ObjectOutputStream - ObjectOutputStream in Java objectOutputStream 的 readObject() 方法没有接收到写入到 Client 的 inputStream 上的 object? - The readObject() Method of objectOutputStream is not receiving the object written on the inputStream to the Client? 什么时候使用 ObjectInputStream.readUnshared() 与 readObject()? - When would one use ObjectInputStream.readUnshared() vs .readObject()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM