File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes socket, gui, thread - problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "socket, gui, thread - problem" Watch "socket, gui, thread - problem" New topic

socket, gui, thread - problem

Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45

I have a problem here when trying to make a simple server-client program. The server side is supposed to bring up a math number, for example 3+5 = , sending it to the client who answers and sends it back to the server that says if its correct or not.
I want both the server and client sides to have a GUI so that everything comes up in a GUI instead of the console. But I can't get it to work out propably with the GUI. I do not know if it has something to do with that the server is a multithread server ....?
Anyway, I have six classes; that starts up a socket and listens after clients.
MathClientHandler -that creates the streams and communicates with the client.
mathServerGUI- that makes the GUI for the serverside.
MatteTal- that brings up the number to calculate and calulates if the answer is correct.
mathClient - creates the client and communicates with the server.
mathClientGUI - GUI for the client.


So, when I start up the server, writing 10001 as port number. Clicks the button- and the button gets kind of stuck. I start up the clientGUI gives localhost as address and port number 10001. But now I get a NPE at the class MathClientHandler in method


Can anyone please help here?

Emanuel Borsoi

Joined: Mar 27, 2012
Posts: 15
IMHO you messed up a little bit the code, you should completely separate GUI from LOGIC code.
However, your problem is that the GUI and the server runs in the same thread, so when the server is in the while() loop, the GUI is stack. To simply solve this problem you should write an inner class in MathServer extending thread or implementing Runnable, and start it in a new thread:

starting it:
Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
Thanks a lot, that solved my problem. I saw in my javabook that the solution was somewhat around here the other day and now you pointed out the direction in a better way. Thanks again, Anders.
I agree. Here's the link:
subject: socket, gui, thread - problem
jQuery in Action, 3rd edition