readObject挂起应用程序

时间:2014-04-09 12:43:52

标签: java hang objectinputstream

我正在尝试创建一个实时信使应用程序。但是,当没有任何理由从ObjectInputStream尝试readObject时,代码会挂起。没有例外。

try {
    System.out.println("Trying to connect to server");
    socket = new Socket(InetAddress.getByName("localhost"),6789);
    System.out.println("Connected to server");

    inputStream = new ObjectInputStream(socket.getInputStream());

    outputStream = new ObjectOutputStream(socket.getOutputStream());
    outputStream.flush();

    System.out.println("Streams are set up.");

    window.toggleTyping(true);
    System.out.println("Typing is now enabled");

    String inputMsg = null;

    do {
        try {
            System.out.println("Reading object");
            inputMsg = (String)inputStream.readObject();
            System.out.println("Object read");

        } catch(ClassNotFoundException ee) {
            System.out.println("Clas not found exception");
            ee.printStackTrace();
        }

    } while(!inputMsg.equalsIgnoreCase("/exit"));

    closeConnection();

}catch(IOException ex) {
    ex.printStackTrace();
}

打印的最后一条消息是"读取对象"。

 try {
        serverSocket = new ServerSocket(6789);
            System.out.println("Socket created. About to accept connections");
            Socket s = serverSocket.accept();
            new Thread(new Chat(s)).start();

    } catch(IOException e) {
        e.printStackTrace();
    }

课堂聊天:

    public class Chat implements Runnable {

private Socket socket;
private ObjectOutputStream outputStream;
private ObjectInputStream inputStream;

public Chat(Socket s) {
    System.out.println("Chat class constructor called");
    this.socket = s;

    try {
        outputStream = new ObjectOutputStream(socket.getOutputStream());
        outputStream.flush();

        inputStream = new ObjectInputStream(socket.getInputStream());
        System.out.println("Chat streams are now set up");

    }catch(IOException ex) {
        ex.printStackTrace();
    }


}
private void closeChat() {
    try {
        outputStream.close();
        inputStream.close();
        socket.close();
        System.out.println("Chat is now closed");
    }catch(IOException ex) {
        ex.printStackTrace();
    }

}

@Override
public void run() {
    System.out.println("Chat class method run called");
    try {
        outputStream.writeObject("Connection is cool");
        outputStream.flush();
        System.out.println("Text sent");
        String inputMsg = "";
        do {

            try {
                inputMsg = (String)inputStream.readObject();
                System.out.println("Message read:"+inputMsg);
            }catch(ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        while(!inputMsg.equalsIgnoreCase("/exit"));
        closeChat();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}
}

每次聊天都是另一个主题的原因是我计划有一天在一个聊天中实现多个聊天。

1 个答案:

答案 0 :(得分:1)

这是因为读取方法是阻塞方法。这意味着它试图读取,直到它得到-1表示读取数据已完成。确保写入此套接字的OutputStream正在发送此信息。因此,您在写入数据后调用ObjectOutputStream.flush();方法发送套接字,或者在写入后关闭输出流。关闭这个将是合理的,因为您可能希望稍后通过此流发送更多数据。


一般情况:创建后你不需要刷新输出流