0

Any time I try to use .ready() or .readLine() on my BufferedReader it just makes my program hang.

I've tried using .ready() instead of .readLine() Commenting out any socket closes Surrounding with try and catch

private void connectButtonActionPerformed(ActionEvent e){
    try {

        Socket mySocket = new Socket(jServerAddressField.getText(),
                Integer.parseInt(jPortField.getText()));

        if (jConnectButton.getText() == "Connect") {
            jChatBox.append("Connected to Server\n");

            jConnectButton.setText("Disconnect");
        } else if (jConnectButton.getText() == "Disconnect") {
            mySocket.close();

            jChatBox.append("Disconnected from server: " + jServerAddressField.getText() + ": " + jPortField.getText() + "\n");

            jConnectButton.setText("Connect");
        } else
            jChatBox.append("Unknown error");
    } catch (IOException evt) {
        jChatBox.append("Could not connect to Server\n");
    }
}

private void sendButtonActionPerformed(ActionEvent e){

    Socket mySocket = null;
    PrintWriter out = null;
    BufferedReader in = null;
    String fromServer;
    try {

        mySocket = new Socket(jServerAddressField.getText(),
                Integer.parseInt(jPortField.getText()));

        out = new PrintWriter(mySocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(
                mySocket.getInputStream()));


    } catch (UnknownHostException evt) {
        System.err.println("Don't know about host: " + jServerAddressField);
        System.exit(1);

    } catch (IOException evt) {
        System.err.println("Couldn't get I/O for the connection to: " + jServerAddressField);
        System.exit(1);
    }
    BufferedReader stdIn = new BufferedReader(
            new InputStreamReader(System.in));

    try {
        System.out.println(mySocket);
        System.out.println(mySocket.getOutputStream());
        System.out.println(in);
        System.out.println(stdIn);
        while ((fromServer = in.readLine()) != null) {
            System.out.println("Server: " + fromServer);
            if (fromServer.equals("Bye."))
                break;

            fromUser = stdIn.readLine();
            if (fromUser != null) {
                System.out.println("Client: " + fromUser);
                out.println(fromUser);
            }
        }
        out.close();
        in.close();
        stdIn.close();
        mySocket.close();
    } catch (IOException ev) {
        System.out.println("bad");
    }
}

in another method...

jSendButton.addActionListener(this::sendButtonActionPerformed); 

I put print statements in to see what was printing.

System.out.println(mySocket);

System.out.println(mySocket.getOutputStream());

System.out.println(in);

System.out.println(stdIn);

Respectively printed out.

Socket[addr=constance.cs.rutgers.edu/128.6.25.89,port=5520,localport=64759]

java.net.SocketOutputStream@38c0ccc7

java.io.BufferedReader@cabb724

java.io.BufferedReader@25204e41
VGR
  • 40,506
  • 4
  • 48
  • 63
  • 1
    Exceptions are extremely important information. Don’t hide them. Always print or log the full stack trace of an exception you catch, unless you plan to make it the cause of another exception. Meaning, add `evt.printStackTrace();` to each of your `catch` blocks. Without seeing that stack trace, you are forced to guess at what’s going wrong. – VGR Sep 11 '19 at 23:46
  • @vgr thank you for the tip, I am but a novice at programmer. Although when I run this program it does not enter any of the catches. – Pablo Hernandez Sep 11 '19 at 23:53
  • 1
    Comparing strings with `==` will eventually fail. See https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java. – VGR Sep 11 '19 at 23:58
  • Does `in.readLine()` hang, or does `stdIn.readLine()` hang? – VGR Sep 11 '19 at 23:59
  • @vgr it hangs on in.readLine() for the while loop condition. It never even goes in the loop. If I just type out 'in.readLine()" it will hang. Everything operates normally until it reaches .readLine() – Pablo Hernandez Sep 12 '19 at 00:06
  • That loop is waiting for the server to send back text. Will the server send back text if you have not yet sent any lines of text to it? – VGR Sep 12 '19 at 00:08
  • @VGR ahh I thought it was doing that in the first try in sendButtonActionPerformed. Evidently it's not. Although it should go in that area, yes? I have a field for my GUI so I would use message.getText() and write that to the server somehow right? – Pablo Hernandez Sep 12 '19 at 00:29
  • If `message` is meant for user input, then yes. But you probably won’t be able to use the same loop; Swing is event-based, and a loop will hang the entire user interface. You will need to add a listener to `message` (or a send button), while a separate thread reads from the socket. – VGR Sep 12 '19 at 02:04
  • Possible duplicate of [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – GBlodgett Sep 29 '19 at 20:36

0 Answers0