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;
mathServer.java- 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
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:
Joined: Mar 21, 2011
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.