[英]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.