我正在开发一个执行Java中的对象IO的项目。问题在于客户端和服务器之间的关系,位于服务器中的ObjectOutputStream发送的对象不等于客户端接收的类。
此处编写的ClientLoop类在客户端上不存在。(我应该更改它以避免混淆。)为每个客户端创建ClientLoop,从Socket中检测到并且是从服务器建立连接。它会侦听客户端发送的下一个对象,然后将其发送到确定操作的Object管道。
A question possibly anticipating the underlying technology from the object streams
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类的其他功能,其中类很难遵循。它只存在于运行命令的方法。它可能不是最好的代码结构,稍后会修复它。
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);
}
}
}
这是来自最新运行会话的日志。每当它说“服务器接收”时,就意味着服务器从客户端找到了一个发送的对象,当它说“服务器发送”时,它将是服务器发送的对象,通过ObjectInputStream和ObjectOutputStream。否则,它是通用控制台语句。在日志的末尾,它显示服务器发送一个名为的对象 &#34;服务器发送:资产{file = Nexus \ sample.txt,content = Hello Server!asdxc}&#34;。 (请注意,短语&#34; asdxc&#34;是在测试期间使用按钮混合创建的。它不包含代码重要性。)
然而,在前面的陈述中,这就是问题所在 &#34;接收:资产{file = Nexus \ sample.txt,content = Hello Server!}&#34;。
我从这个日志输出中得出结论,客户端中的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
答案 0 :(得分:1)
如果您使用不同的内容两次发送相同的对象,则需要使用writeUnshared()
或reset()
以确保它实际上再次发送。