client socket hangs when using JOptionPane

E

Eqbal Z

I am using a simple client and server using sockets and displaying
some dialogs using JOptionPane on the client side. The problem is that
the application hangs after doing everything correctly (meaning it
does not exit).
Below is the code I use. I have not used swing before so maybe I am
missing something?

import java.net.*;
import java.io.*;
import javax.swing.*;

public class NIMSwingClient extends JFrame {
public static void main(String [] args) {
if (args.length != 2){
System.out.println("Usage: java NIMClient host port");
System.exit(1);
}
try {
Socket server = new Socket(args[0],Integer.parseInt(args[1]));
JFrame frame = new JFrame("NIM Game Client");
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setVisible(true);
JOptionPane.showMessageDialog(frame, "Welcome to NIM
game!","NIM Game", JOptionPane.INFORMATION_MESSAGE);
BufferedReader fromServer = new BufferedReader(new
InputStreamReader(server.getInputStream()));
PrintWriter toServer = new
PrintWriter(server.getOutputStream(),true);
String serverMsg;
String playerMsg;
String playerName;
// get player name
serverMsg = fromServer.readLine();
playerName = JOptionPane.showInputDialog(frame, serverMsg,
"NIM Game",JOptionPane.PLAIN_MESSAGE);
toServer.println(playerName);
// start playing
while ((serverMsg = fromServer.readLine()) != null) {
if ("Incorrect Choice.".equals(serverMsg)) {
JOptionPane.showMessageDialog(frame, serverMsg, serverMsg,
JOptionPane.WARNING_MESSAGE);
continue;
}
if ("Game Over!".equals(serverMsg)) {
String title = serverMsg;
serverMsg += "\n" + fromServer.readLine();
JOptionPane.showMessageDialog(frame, serverMsg, title,
JOptionPane.INFORMATION_MESSAGE);
break;
}
playerMsg = JOptionPane.showInputDialog(frame, serverMsg,
"NIM Game - Pick token",JOptionPane.PLAIN_MESSAGE);//input.readLine();
toServer.println(playerMsg);
}
fromServer.close();
toServer.close();
server.close();

}catch(Exception e) {
e.printStackTrace();
}
}
}
 
C

Cid

I am using a simple client and server using sockets and displaying
some dialogs using JOptionPane on the client side. The problem is that
the application hangs after doing everything correctly (meaning it
does not exit).
Below is the code I use. I have not used swing before so maybe I am
missing something?
JFrame frame = new JFrame("NIM Game Client");
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

I think this is your problem. You are correctly tieing the option pane
to your frame which is good. But you've commented out your default
close operation here. If not specified, the swing default for JFrame
is HIDE_ON_CLOSE which means the swing thread just sits there thinkin
about biscuits after your main thread exists. You wanna shut that
hippy down.

I recommend DISPOSE_ON_CLOSE rather than EXIT_ON_CLOSE however.
EXIT... just calls System.exit() which is blah blah evil wizard blah
blah Al Queda. DISPOSE will release your JFrame resources that are
keeping the Swing thread(s) running so you're app will shut down.
Because you specified your frame as the parent of JOptionPane, it gets
cleaned up by your DISPOSE as well. Perfect.

I ran into problems using JOptionPane w/o specifying the parent -
where it uses some Swing internal frame as the parent instead but
doesn't clean up after itself and hangs the Swing threads. So in my
experience, you always want to attach the JOptionPane to a container
you control so you know it'll get cleaned up.
 
E

Eqbal Z

I tried what you suggested, but still no luck. The client just won't exit.
I have to ^C.
 
G

Gordon Beaton

I am using a simple client and server using sockets and displaying
some dialogs using JOptionPane on the client side. The problem is
that the application hangs after doing everything correctly (meaning
it does not exit). Below is the code I use. I have not used swing
before so maybe I am missing something?

[...]

The following line says to continue until the server closes his end of
the connection. Has that happened?
while ((serverMsg = fromServer.readLine()) != null) {

Further down in the loop, after determining that the server sent "Game
Over!", you again call readLine(). Does the server send an additional
message after "Game Over!"?
if ("Game Over!".equals(serverMsg)) {
String title = serverMsg;
serverMsg += "\n" + fromServer.readLine();
JOptionPane.showMessageDialog(frame, serverMsg, title,
JOptionPane.INFORMATION_MESSAGE);
break;

If neither of the above clues helps, you need to determine exactly
where the application hangs (we can't guess). Use System.err.println()
to display some log messages.

/gordon
 
C

Cid

I tried what you suggested, but still no luck. The client just won't exit.
I have to ^C.

Since frame isn't made visible it can never explicitly be 'closed' so
it doesn't get a close event which would in turn trigger the
DISPOSE_ON_CLOSE action. So, you just need to manually dispose of it.

To do so, add
frame.dispose() ;
at the end with your assorted close() calls.


While you're at it, you may want to move those close() and dispose()
to a finally block to ensure they go off. Beware of errors they
themselves may throw that would interrupt the cleanup!

Happy coding.
 
E

Eqbal Z

Gordon,
my client-server protocol are ok. I also have a text version of the
client which does not have this problem.
I tried frame.dispose() after all the close statements and then I did
a System.out.println after that. I set the frame.setVisible(true) as
well. My frames seem to have been closed and it reached the very last
statement in the try block (System.out.println), but the client thread
is still hanging.


Gordon Beaton said:
I am using a simple client and server using sockets and displaying
some dialogs using JOptionPane on the client side. The problem is
that the application hangs after doing everything correctly (meaning
it does not exit). Below is the code I use. I have not used swing
before so maybe I am missing something?

[...]

The following line says to continue until the server closes his end of
the connection. Has that happened?
while ((serverMsg = fromServer.readLine()) != null) {

Further down in the loop, after determining that the server sent "Game
Over!", you again call readLine(). Does the server send an additional
message after "Game Over!"?
if ("Game Over!".equals(serverMsg)) {
String title = serverMsg;
serverMsg += "\n" + fromServer.readLine();
JOptionPane.showMessageDialog(frame, serverMsg, title,
JOptionPane.INFORMATION_MESSAGE);
break;

If neither of the above clues helps, you need to determine exactly
where the application hangs (we can't guess). Use System.err.println()
to display some log messages.

/gordon
 
C

Cid

Gordon,
my client-server protocol are ok. I also have a text version of the
client which does not have this problem.
I tried frame.dispose() after all the close statements and then I did
a System.out.println after that. I set the frame.setVisible(true) as
well. My frames seem to have been closed and it reached the very last
statement in the try block (System.out.println), but the client thread
is still hanging.

Are you sure which thread is hanging? Might be helpful to verify
whether it's yours or still Swing.

You can use jdb for this if your compiler doesn't support showing the
list of current threads.

Eclipse provides this - failing that, using jdb, just walk through the
code till you hit a snag or leave your main thread and then use
'threads' to see the list of threads. It'll show you what's still out
on the town - more specifically which one is currently active. That
will at least nail down the culprit.
 
E

Eqbal Z

Ok, another discovery. If I run my program using jdk 1.4 it runs fine.
But if I use jdk 1.3 it gives the problem.
I also fiddled with debugging in eclipse and the threads that continue
to run when they should have terminated are:

Thread [Thread-2] (Running)
System Thread [Finalizer] (Running)
System Thread [Signal Dispatcher] (Running)
System Thread [Reference Handler] (Running)
System Thread [CompileThread0] (Running)
Thread [AWT-EventQueue-0] (Running)
Thread [SunToolkit.PostEventQueue-0] (Running)
Thread [AWT-Windows] (Running)
Thread [EventQueueMonitor-ComponentEvtDispatch] (Running)
Thread [Thread-1] (Running)
Thread [Screen Updater] (Running)
Thread [TimerQueue] (Running)

Thanks for your help.
 
C

Cid

Ok, another discovery. If I run my program using jdk 1.4 it runs fine.
But if I use jdk 1.3 it gives the problem.
I also fiddled with debugging in eclipse and the threads that continue
to run when they should have terminated are:

Thread [Thread-2] (Running)
System Thread [Finalizer] (Running)
System Thread [Signal Dispatcher] (Running)
System Thread [Reference Handler] (Running)
System Thread [CompileThread0] (Running)
Thread [AWT-EventQueue-0] (Running)
Thread [SunToolkit.PostEventQueue-0] (Running)
Thread [AWT-Windows] (Running)
Thread [EventQueueMonitor-ComponentEvtDispatch] (Running)
Thread [Thread-1] (Running)
Thread [Screen Updater] (Running)
Thread [TimerQueue] (Running)

Ok, that looks like the assorted swing/awt library maintenance threads
to me. If 1.4 fixes it and 1.3 breaks it, I vote to call that a 1.3
bug and move on. Do you have to use 1.3 for some reason? Steam powered
server or something? :)
 
C

Carl Howells

Eqbal said:
Ok, another discovery. If I run my program using jdk 1.4 it runs fine.
But if I use jdk 1.3 it gives the problem.

Yep. This was a bug in 1.3 that was fixed in 1.4. A lot of people were
quite annoyed that it took that long to fix, and wasn't fixed until a
major release.

This means you have a couple options:

a) Only use 1.4
b) Add in code to call System.exit when all windows are closed if the
system isn't cleaning itself up.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top