Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ObjectInputStream接收的对象不等于ObjectOutputStream发送的对象_Java_Sockets_Objectinputstream_Objectoutputstream - Fatal编程技术网

Java ObjectInputStream接收的对象不等于ObjectOutputStream发送的对象

Java ObjectInputStream接收的对象不等于ObjectOutputStream发送的对象,java,sockets,objectinputstream,objectoutputstream,Java,Sockets,Objectinputstream,Objectoutputstream,我正在从事一个项目,在这个项目中,Java中的对象IO是被执行的。问题在于客户机和服务器之间的关系中,位于服务器中的ObjectOutputStream发送的对象不等于客户机接收的类 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

我正在从事一个项目,在这个项目中,Java中的对象IO是被执行的。问题在于客户机和服务器之间的关系中,位于服务器中的ObjectOutputStream发送的对象不等于客户机接收的类

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!}
客户端上不存在在此编写的ClientLoop类。(我可能应该更改该类以避免混淆。)从套接字为每个客户端创建ClientLoop,该套接字被检测到并与服务器建立连接。它侦听客户端发送的下一个对象,并将其发送到对象管道中,在该管道中确定操作

这里的Commander类是从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);
        }
    }
}
如果需要更多代码来诊断问题,我在GitHub链接上有完整的代码库:

如果要两次发送同一个对象,且内容不同,则需要使用
writeUnshared()
reset()
来确保该对象实际再次发送。

是否可以创建较小的代码块来重新创建错误?
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!}