My chat room program. I have 2 packages, 1 for Client & other for Server. Run Server program, fill the PortField and click button Start Server, window program is stuck, i cant do anything on it, but Server still work, Clients still connect and chat.
private void btnOpenActionPerformed(java.awt.event.ActionEvent evt) {
int port = Integer.parseInt(txtPort.getText());
go(port);
}
private void go(int port){
try {
listUser = new Hashtable<String, ClientConnect>();
server = new ServerSocket(port);
txaStatus.append("Server is started\n");
txaStatus.append("IP Server : "+InetAddress.getLocalHost().getHostAddress()+"\n");
txaStatus.append("Port : " + port + "\n");
while(true){
client = server.accept();
new ClientConnect(this,client);//class ClientConnect for DataOutPut & DataInput Client <=> Server
}
} catch (IOException e) {
txaStatus.append("Server cannot start\n");
JOptionPane.showMessageDialog(this,"Port busy","warning",JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
}
You can’t run your server in the GUI Thread, this will prevent the program from accepting any other input. You need to move the
go()method into a separate thread.