• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't see print message after while statement  RSS feed

 
Anders Kviback
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I have a problem here. I have six classes. MathServer is a server class. It cooperates with a class MatteTal that produces mathematical numbers and calculates them. The MathServer class starts a class MathClientHandler which sends the numbers to class MathClient. In MathClientGUI you see the numbers, put in the answer and sends it back to the server. Then there is another class FileClient that you start from MathClientGUI, you choose a file from JFileChooser, this file contains rows with numbers to calculate like, 5+4=10, 7/4=1 and so forth. FileClient sends this file to MathServer that calculates and corrects the answers and sends back a file with an answer like 5+4=10(false), 7/4=1(correct) OBS! Integerdivision.

After this correction is done and the file sending procedure is completed there should be a message that says "The calculation of the sent file is completed!". And this is the problem!! I can't get this message to show up. I put it in a JOptionPane dialog box as you can see.

Here are all the classes. It is a lot of code but I don't know how to do it in another way.
MathServer:




FileClient:

I only send these two classes. If you want all six I send them as well. The problem is the while-loop in the FileClient class. Line 87. When in.readLine gets null the while loop is done and then I want the message to execute, but it doesn't. Can anyone see what I need to do? I have Swing and I have Thread and I do not know if they in some way conquer with each other in some way?

// Anders
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you talking about this?


First, why are you calling interrupt() there? That seems pointless.

As to the JOPtionPane not showing up, it's one of two things:

1. Perhaps the showMessageDialog() method doesn't do anything if the thread calling it has been interrupted. This is unlikely, and if it's the case, it will be documented.

2. More likely, your loop just isn't ending. A common beginner's mistake with sockets is to say, "Okay, I've stopped sending, so why doesn't the receiver break out of its loop?" And the answer is that the socket has no way to know that you're done sending. You need to close the socket from the sender's side (or at least its OutputStream) so that the receiver will get the EOS and allow readLine() to return null.

If you think that you are in fact properly closing things down, then there must be some other problem, but that's way too much code for me to read.
 
Anders Kviback
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I understand the loop is ending and I am closing the socket and the streams. If I send a file with six rows of numbers to calculate I do get a file with the six numbers corrected and that looks fine. So, this means that the while loop is done and it leaves the while loop but then the program gets stuck. When there is nothing more to read in the file in.readLine() gets null and the while loop is ended.
I've done System.out.println("The calculation of the sent file is completed!") on various places but nothing happens.
And I am closing down the streams and sockets on booth sides as well. This is the way it looks like on the server side:

And on the client side:


If you look on the MathServer class and when receiving the file to be corrected , line 102 - line 109 , this is the same idea as on the client side. I'm trying to do the
System.out .println("The calculation of the sent file is completed!") here as well but nothing happens. When the while loop is done it should leave the loop and end up in either the try catch statements or in the printed messages but it doesn't?
In the FileClient class I have an inner class that runs the thread since otherwise it will be a concurrent problem with the swing actionlistener thread. And then there are the other threads for the other classes as well. But the other classes works fine.
It is as the program gets stuck after the while loop. Nothing happens. ??
I'm stuck!

Do you have any more ideas?

//Anders
 
Anders Kviback
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that the problem is in.readLine() in the while loop. After googling around on the issue today I see that this seems to be a common issue. But I haven't solved it yet.

//Anders
 
Anders Kviback
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved it. In the FileClient class I had to write "quit" as a last message:
And then close down all the streams:



And in the MathServer class:



//Anders
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!