0
private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {                                        
    // TODO add your handling code here:
    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
    String fromServer;
    String fromUser;

    try{
    while ((fromServer = in.readLine()) != null) {
        txtField.setText("Server: " + fromServer +"\n");
        if (fromServer.equals("hehe bye"))
            break;

        fromUser = stdIn.readLine();
    if (fromUser != null) {
            txtField.setText("Client: " + fromUser +"\n");
            out.println(fromUser);
    }
    }
    out.close();
    in.close();
    stdIn.close();
    nisSocket.close();
  } catch(Exception e){
      //
  }


}    


public static void main(String args[]) {
    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Gui_java().setVisible(true);
        }
    });

    try {
        nisSocket = new Socket("localhost", 8888);
        out = new PrintWriter(nisSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(nisSocket.getInputStream()));
    } catch (UnknownHostException e) {
        System.err.println("Don't know about host: localhost.");
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to: localhost.");
        System.exit(1);
    }
}

// Variables declaration - do not modify                     
private javax.swing.JButton btnSend;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea txtArea;
private javax.swing.JTextField txtField;
// End of variables declaration                   

}

When I run the GUI code, it hangs. I tried changing alot of things, but it doesn't work out. The plan was to make the text field to send message and shows in text area, and then the text area will show what does the server replies. The server has its own reply, so I've made a gui only for the Client.

Been spending hours trying to find the fix but I don't quite understand, due to low level of knowledge, sorry.

edited:

/**
 * Creates new form Gui_java
 */

static Socket nisSocket = null;
static PrintWriter out = null;
static BufferedReader in = null;

they are declared at the start, I have 3 classes: client(gui), protocol, server. I don't want to post all because it might be too long.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
HeatKai C
  • 65
  • 1
  • 9
  • Is this the whole code? I don't se the declaration of 'in', 'out', nisSocket'. – Embid123 Nov 15 '18 at 07:18
  • @Embid123 this is only the gui code. it's not full my bad, nissocket is a Socket. the code is not full – HeatKai C Nov 15 '18 at 07:29
  • It hangs because you need a separate thread to listen to the socket and not block the GUI thread – OneCricketeer Nov 15 '18 at 07:34
  • @cricket_007 I don't quite get it, can you explain it to me with more details? Thank you for your time! :) – HeatKai C Nov 15 '18 at 07:40
  • 2
    The GUI runs on its own thread. You have an infinite loop as part of that thread, pausing the GUI from redrawing and taking any other user input, so it "freezes"... – OneCricketeer Nov 15 '18 at 07:41
  • Basically same question here from 6 years before https://stackoverflow.com/questions/11185485/java-swing-gui-freezes – OneCricketeer Nov 15 '18 at 07:45
  • @cricket_007 oh thank you! After I read the post, I noticed they mentioned about while() and socket, so how can I change/fix the while problem? The client was actually from its own class, I just try to fit it in the gui because I thought it might work and here I am. Thank you for your time. – HeatKai C Nov 15 '18 at 08:01
  • Well, it's a complicated answer, for beginners I think, but the code involves `new Thread` – OneCricketeer Nov 15 '18 at 08:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/183688/discussion-between-heatkai-c-and-cricket-007). – HeatKai C Nov 15 '18 at 08:04
  • @HeatKaiC, read the section from the Swing tutorial on [Concurrency](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) to understand the problem. Your Socket code needs to execute in a separate Thread. Try using a SwingWorker as described in the tutorial . – camickr Nov 15 '18 at 16:08

0 Answers0