【问题标题】:Error recieving Object via ObjectInputStream from server从服务器通过 ObjectInputStream 接收对象时出错
【发布时间】:2019-02-22 21:35:20
【问题描述】:

我正在编写一个用于简化 java.io.Socket api 的简单实用程序包,但遇到了以下减速带:到目前为止,我能够发送一个消息类(只包含一个 Object 类型的字段,它代表任何类型的数据)到服务器(我正在使用字符串进行测试)并且它能够读取它。但是,当我尝试再次读取消息时,流无法识别任何通过的数据(我正在检查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 接口,所以这可能不是问题。

【问题讨论】:

  • 删除 available() 检查。它没有任何价值。
  • 它创建一个畅通的读取。这就是我想要的

标签: java sockets stream


【解决方案1】:

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

【讨论】:

  • 我可能有点急于在 StackOverflow 上发布这个:D
猜你喜欢
  • 2013-07-13
  • 2016-04-30
  • 2018-02-04
  • 1970-01-01
  • 2014-06-23
  • 2018-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多