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.