• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

socket, gui, thread - problem

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

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.









[/code]
[/code]

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


Why?

Can anyone please help here?

Anders
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 45
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 will suppress my every urge. But not this shameless plug:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic