Java 通过ObjectInputStream从服务器接收对象时出错
我正在编写一个简单的实用程序包来简化java.io.Socket api,但遇到了以下问题:到目前为止,我能够向服务器发送一个消息类(只包含一个表示任何类型数据的Object类型的字段)(我正在使用字符串进行测试),并且它能够读入。但是,当我再次尝试读回消息时,流无法识别任何通过的数据(我正在使用in.available()>0进行检查)。任何帮助都将不胜感激Java 通过ObjectInputStream从服务器接收对象时出错,java,sockets,stream,Java,Sockets,Stream,我正在编写一个简单的实用程序包来简化java.io.Socket api,但遇到了以下问题:到目前为止,我能够向服务器发送一个消息类(只包含一个表示任何类型数据的Object类型的字段)(我正在使用字符串进行测试),并且它能够读入。但是,当我再次尝试读回消息时,流无法识别任何通过的数据(我正在使用in.available()>0进行检查)。任何帮助都将不胜感激 public class Client { private Socket client; private Object
public class Client {
private Socket client;
private ObjectInputStream in;
private ObjectOutputStream out;
public Client (String hostName, int port) throws UnknownHostException, IOException {
System.out.println("Establishing connection to server at " + hostName + ":" + port + "...");
client = new Socket(hostName, port);
System.out.println("Client connected. Client: " + client);
this.out = new ObjectOutputStream(client.getOutputStream());
this.out.flush();
this.in = new ObjectInputStream(client.getInputStream());
}
public Object getFromServer () throws ClassNotFoundException, IOException {
return in.readObject();
}
public void sendToServer (Object o) throws IOException {
out.writeObject(o);
out.flush();
}
public boolean dataAvailable () throws IOException {
return in.available() > 0;
}
public void close () throws IOException {
client.close();
}
}
用于客户端处理的服务器端线程(我不需要包含服务器代码,因为它与输入输出无关:
public class ClientHandler extends Thread {
private Socket client;
private ObjectInputStream in;
private ObjectOutputStream out;
public ClientHandler (Socket socket) throws IOException {
super();
this.client = socket;
this.out = new ObjectOutputStream(socket.getOutputStream());
this.out.flush();
this.in = new ObjectInputStream(socket.getInputStream());
}
@Override
public void run () {
String currentMessage = "";
while (currentMessage != "/exit") {
try {
currentMessage = (String)((Message)in.readObject()).getData();
out.writeObject(new Message(currentMessage.toUpperCase()));
out.flush();
} catch (ClassNotFoundException e) {
System.err.println("ClassNotFoundException occured while processing client input");
e.printStackTrace();
} catch (IOException e) {
System.err.println("IOException occured while processing client input");
e.printStackTrace();
}
}
try {
client.close();
} catch (IOException e) {
System.err.println("IOException occured while trying to close client socket");
e.printStackTrace();
}
}
}
客户端的测试类:
public class ClientMain {
public static void main (String[] args) throws UnknownHostException, IOException, ClassNotFoundException {
Client client = new Client("localhost", 9090);
BufferedReader sysIn = new BufferedReader(new InputStreamReader(System.in));
String userMessage = "";
while (userMessage != "/exit") {
if (client.dataAvailable()) /*this if-statement never returns true*/ {
String serverMessage = (String)((Message)client.getFromServer()).getData();
System.out.println(serverMessage);
}
if (sysIn.ready()) {
userMessage = sysIn.readLine();
client.sendToServer(new Message(userMessage));
}
}
client.close();
}
}
在任何人提问之前,我的Message类确实实现了Serializable接口,所以这可能不是问题所在。我解决了它。在ClientMain类中(在main方法中),我将用户输入读取和服务器读取分为两个线程。我使userMessage字符串静态易失性,现在由于某种原因它可以工作。如果有人能回答为什么我的方法不起作用,那将非常有用。我可能有点急于在StackOverflow:DRemove the
available()上发布此消息
check。它不会增加任何值。它会创建一个未阻止的读取。这就是我想要的