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

问题描述 投票:0回答:1

我正在编写一个简单的实用程序包来简化java.io.Socket api但是遇到了以下的速度问题:我到目前为止能够发送一个Message Class(只包含一个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();
    }
}

用于客户端处理的服务器端线程(我不需要包含Servers代码,因为它与输入输出无关:

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接口,因此可能不是问题。

java sockets stream
1个回答
-1
投票

我解决了在ClientMain类中(在main方法中),我将用户输入读取和服务器读取分成两个线程。我使userMessage字符串静态不稳定,现在它出于某种原因。如果有人可以回答,虽然说为什么我的方法不起作用,这将是非常有帮助的。

© www.soinside.com 2019 - 2024. All rights reserved.