Question

I'm trying to create a live messenger application. But the code hangs when trying to readObject from an ObjectInputStream with no reason. No exception is thrown.

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();
}

The last message that is printed is "Reading object".

 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();
    }

And the class Chat:

    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();
    }
}
}

The reason every chat is a different thread is that I'm planning to implement multiple one on one chats someday.

Was it helpful?

Solution

This is because the reading method is a blocking method. This means it is trying to read until it gets a -1 to show that the read data are finished. Ensure that the OutputStream which writes to this socket is sending this. So you invoke at sending socket the ObjectOutputStream.flush(); method after you wrote the data or just close the output stream after writing. Closing this one would'nt be reasonable, because probably you may want to send further data throug this stream later.


Just a general thing: You don't need to flush the outputstream immediatley after you have created it

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top