通过ObjectInputStream从服务器接收对象时出错

时间:2019-02-22 21:35:20

标签: java sockets stream

我正在编写一个用于简化java.io.Socket api的简单实用程序包,但是遇到了以下减速情况:到目前为止,我能够发送消息类(仅包含表示任何类型的Object类型的字段类型的数据)发送到服务器(我正在使用Strings进行测试),并且能够读取它。但是,当我尝试再次读回Message时,流无法识别通过的任何数据(我正在检查in.available()> 0)。任何帮助将不胜感激!

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接口,所以这可能不是问题。

1 个答案:

答案 0 :(得分:-1)

我解决了。在ClientMain类中(在main方法中),我将用户输入读数和服务器读数分为两个线程。我使userMessage String静态可变,现在由于某种原因它可以工作。如果有人可以回答,说为什么我的方法不起作用,那将非常有帮助。