java如何将列表<String>作为对象发送并使用ObjectInputStream读取
我有服务器和客户端,我想从sendMsg(objectobj)向服务器发送对象:第一个是自定义类Message(Message类是可序列化的),第二个是String,最后一个是List。 当我尝试读取列表时,服务器什么也不做(没有错误)。调试器说(应用程序正在运行),然后我将消息对象发送给客户端,使其连接松动,因为服务器没有响应。 以下是客户端:
public static synchronized boolean sendMsg(Object obj) {
try {
if (socket == null || socket.isClosed()) {
launch();
return false;
} else {
objOutStream.writeObject(obj);
objOutStream.flush();
return true;
}
} catch (IOException e) {
System.out.println("Outputstream not found.");
return false;
}
}
以下是服务器端:
public synchronized void readMessages() throws IOException {
while (true) {
try {
Object obj = objInputStream.readObject();
if (obj != null) {
if (obj.getClass().getSimpleName().equals("Message")) {
server.chooseClientForMessage((Message) obj, this);
break;
}
if (obj.getClass().getSimpleName().equals("List<String>")) {
sendMessage(server.getOnlineClients((List<String>) obj));
break;
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
共 (0) 个答案