Reputation: 83
I have a gui server that call joptionpanel first to input port number.. If I click OK button , it looks run well. But if in the first run i click cancel, and then click ok my GUI freeze. I think class start()
make it freeze.
I am sorry this is a duplicate question, but maybe the problems is differents with the others.
this is my server code. Anyone can help me. What actually causes the gui freeze ?
public class server {
// Main meathod
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Throwable e) {
e.printStackTrace();
}
server server2 = new server(PORT);
server2.start();
}
public server(int PORT)
{
sdf = new SimpleDateFormat("[HH:mm:ss]");
go();
}
public void go() {
//The GUI hidden`enter code here`...
connect.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
// When Exit button is pressed
if (ae.getSource() == connect) {
start();
}
}
});
}
// Add new threads when new user enters
class reader implements Runnable {
BufferedReader br;
public reader(Socket clientsocket) {
try {
sock = clientsocket;
// Getting input stream
InputStreamReader ir = new InputStreamReader(
sock.getInputStream());
br = new BufferedReader(ir);
} catch (IOException ex) {
info.append(ex + "\n");
}
}
@Override
public void run() {
String msg = null;
String n;
try{
InputStreamReader ir = new InputStreamReader(sock.getInputStream());
br = new BufferedReader(ir);
pw = new PrintWriter(sock.getOutputStream());
// If message is not empty then broadcast message
while((msg=br.readLine())!=null)
{
incoming.append(msg +"\n");
pw.println(); // send the same line back to the client.
pw.flush(); // flush the stream to ensure that the data reaches the other end.
}
}
catch(SocketException ex)
{
info.append("Client Disconnected.\n");
}
catch(IOException ex)
{
info.append(ex+"\n");
}
}
}
private static int setPortNumber()
{
String portNumber = (String) JOptionPane.showInputDialog(frame,
"Enter the Port number for server creation","Server Connection\n",
JOptionPane.OK_CANCEL_OPTION, null, null, PORT);
int PORT = Integer.parseInt(portNumber);
return PORT;
}
// Meathod to handle networking Activities
public void start(){
connected = new ArrayList<PrintWriter>();
try {
PORT = setPortNumber();
ss = new ServerSocket(PORT);
String time = sdf.format(new Date());
info.append(time + "Server Started at port : " + PORT + "\n");
keep = true;
while (keep) {
try {
// Get socket connection
Socket clientsocket = ss.accept();
InetAddress a = clientsocket.getInetAddress();
info.append(a.getHostName() + " at IP Address "
+ a.getHostAddress() + " Connected to server.\n");
// Get output steam
PrintWriter pw = new PrintWriter(
clientsocket.getOutputStream());
connected.add(pw);
// Create and start new thread
Thread t1 = new Thread(new reader(clientsocket));
t1.start();
}
catch (NullPointerException ex) {
info.append("Null pointer Exception ");
} catch (SocketException e) {
info.append("Connection Reset\n");
}
}
} catch (Exception ex) {
info.append("Server has not been connected! Please try again, click connect button.\n");
}
}
Upvotes: 1
Views: 184
Reputation: 43
As HovercraftFullOfEels said, your while() should be in a separate thread, so as not to stop Swings events. Also just a suggestion, but in your code block for the creation of an ActionListener in the method go(), why do you recheck if connect is the source?
Upvotes: 1
Reputation: 285430
Your start method eventually calls a while (true)
loop that blocks Swing's event thread. The solution is to do this long running code in a background thread, and a decent way to do this is to use a SwingWorker
. Please see Concurrency in Swing for more details on this useful construct.
As an important aside: you will want to improve your code formatting, in particular your code indentation. Your current indentation is misleading in that you start some internal classes flush left, which makes them appear to be external classes. If you can use an IDE, do so, and allow it to help improve your indentation. Otherwise do it by hand with care since good indentation helps you debug some concepts of your code at a glance, and bad indentation can mislead you and others about your code.
Upvotes: 5