java—objectinputstream接收的对象不等于objectoutputstream发送的对象

tkqqtvp1  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(530)

我正在从事一个项目,在这个项目中,java中的objectio是执行的。问题存在于客户机和服务器之间的关系中,在该关系中,位于服务器中的objectoutputstream发送的对象与客户机接收的类不相等。
客户端上不存在在此编写的clientloop类(为了避免混淆,我可能应该对此进行更改。)从一个套接字为每个客户机创建一个clientloop,该套接字被检测到并与服务器建立连接。它侦听客户机发送的下一个对象,并将其发送到对象管道,在该管道中确定操作。
一个可能从对象流中预测底层技术的问题

public class ClientLoop extends Loop implements Sendable {
    private final Commander commander;

    private final Socket socket;
    private final ObjectInputStream input;
    private final ObjectOutputStream output;
    private final Server server;

    public ClientLoop(Server server, Socket socket) throws IOException {
        this.server = server;
        this.socket = socket;

        //we need to construct the output stream first
        //to discard the stream header
        this.output = new ObjectOutputStream(socket.getOutputStream());
        this.input = new ObjectInputStream(socket.getInputStream());

        this.commander = new Commander(server, socket, this);
    }

    @Override
    public void loop() {
        try {
            try {
                Object next = input.readObject();
                String className = next.getClass().getName();

                System.out.println("Server Receive: " + next);

                if (className.equals("com.meti.server.util.Command")) {
                    commander.runCommand(castIfOfInstance(next, Command.class));
                //this section of code here says that whenever an AssetChange is being sent by the client. 
                } else if (next instanceof AssetChange) {
                    AssetChange assetChange = castIfOfInstance(next, AssetChange.class);
                    assetChange.update(server.getAssetManager().getAsset(assetChange.getAssetPath()));

                    //has been change, update all clientloops
                } else {
                    getInstance().log(Level.WARNING, "Found no type handling " +
                            "for class type " + className);
                }
            } catch (EOFException e) {
                socket.close();
                setRunning(false);
            }
        } catch (Exception e) {
            getInstance().log(Level.WARNING, e);
        }
    }

    @Override
    public void send(Serializable serializable, boolean flush) throws IOException {
        System.out.println("Server Send: " + serializable);

        output.writeObject(serializable);
        if (flush) {
            output.flush();
        }
    }
}

这里的commander类提供了clientloop类的附加功能,而clientloop类很难遵循。它的存在仅仅是为了容纳运行命令的方法。它可能不是最好的代码结构,稍后将修复它。

public class Commander {
    private Server server;
    private Socket socket;
    private Sendable sendable;

    public Commander() {
    }

    public Commander(Server server, Socket socket, Sendable sendable) {
        this.server = server;
        this.socket = socket;
        this.sendable = sendable;
    }

    public void runCommand(Command next) throws IOException {
        if ("login".equals(next.getName())) {
            String password = (String) next.getArgs()[0];
            if (password.equals(server.getPassword())) {
                getInstance().log(Level.INFO, "Client " + socket.getInetAddress() + " has connected with valid password");
            } else {
                getInstance().log(Level.INFO, "Client has invalid password, kicking out!");
                socket.close();
            }
        } else if ("disconnect".equals(next.getName())) {
            socket.close();
        } else if ("list".equals(next.getName())) {
            Cargo<String> cargo = new Cargo<>();
            HashMap<String, Asset<?>> assets = server.getAssetManager().getAssets();
            ArrayList<String> paths = new ArrayList<>();
            paths.addAll(assets.keySet());
            cargo.getContents().addAll(paths);

            sendable.send(cargo, true);

        } else if ("get".equals(next.getName())) {//should be declared other side...
            String path = Utility.castIfOfInstance(next.getArgs()[0], String.class);
            Asset<?> asset = server.getAssetManager().getAsset(path);

            sendable.send(asset, true);
        }
    }
}

这是最新运行会话的日志。每当它说“server receive”时,意味着服务器从客户机找到一个已发送的对象,当它说“server send”时,它将是服务器通过objectinputstream和objectoutputstream发送的对象。否则,它是一个通用的控制台语句。在日志的末尾,它显示服务器发送了一个名为“server”的对象send:asset{file=nexus\sample.txt,content=hello server!asdxc}(请注意,短语“asdxc”是在测试期间通过按钮mashing创建的。它不包含代码意义。)
然而,问题就出在前面的语句“receive:asset{file=nexus\sample.txt,content=hello server!}”中。
我从这个日志输出得出结论,客户机中的objectinputstream和服务器中的objectoutputstream确实正常工作,但是服务器上objectoutputstream发送的对象并不等于客户机上objectinputstream立即接收的对象。根据日志,服务器只发送了一个对象,客户端没有正确注册。

Oct 31, 2017 8:11:11 AM com.meti.Main log
INFO: Starting application
Oct 31, 2017 8:11:50 AM com.meti.Main log
INFO: Reading directory
Oct 31, 2017 8:11:51 AM com.meti.Main log
INFO: Listening for clients
Oct 31, 2017 8:11:53 AM com.meti.Main log
INFO: Located client at /127.0.0.1
Server Receive: Command{name='login', args=[5875580034436271440]}
Oct 31, 2017 8:11:53 AM com.meti.Main log
INFO: Client /127.0.0.1 has connected with valid password
Server Receive: Command{name='list', args=[files]}
Server Send:com.meti.server.util.Cargo@68917144
Server Receive: Command{name='get', args=[Nexus\sample.txt]}
Server Send:Asset{file=Nexus\sample.txt, content=Hello Server!}
Receive: Asset{file=Nexus\sample.txt, content=Hello Server!}
Server Receive: com.meti.server.asset.text.TextChange@546c91cb
Server Receive: com.meti.server.asset.text.TextChange@1a2f571d
Server Receive: com.meti.server.asset.text.TextChange@7a308ae0
Server Receive: com.meti.server.asset.text.TextChange@5897f82d
Server Receive: com.meti.server.asset.text.TextChange@68c5d83d
Server Receive: com.meti.server.asset.text.TextChange@832ed87
Server Receive: com.meti.server.asset.text.TextChange@76ab11eb
Server Receive: Command{name='get', args=[Nexus\sample.txt]}
Server Send:Asset{file=Nexus\sample.txt, content=Hello Server!asdxc}
Receive: Asset{file=Nexus\sample.txt, content=Hello Server!}

如果需要更多代码来诊断问题,我在github链接中有完整的代码库:https://github.com/mathhman/nexus

gg58donl

gg58donl1#

如果您发送同一个对象两次,需要使用不同的内容 writeUnshared() 或者 reset() 以确保它再次被发送。

相关问题