Anders Kviback

Ranch Hand
+ Follow
since Mar 21, 2011
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Anders Kviback

Thanks a lot again.

I would say that the code almost works perfect now. Now division by zero is fixed as well. great!
But still it freezes occasionaly.

And what I can see from my system.out.println is that it is always at reading the stream, both server and client side.

MathClientHandler:


MathClient:




So, it stops at some of these points. Why?


MathNumber is not a method. It is a constructor. That's why it starts with a capital letter.


Thanks a lot again for helping me out here

// Anders
Thanks alot here.
This piece of code


Should be written as:

int serverCalc = tal2.calculate();
boolean answer = (serverCalc == kSvar);
out.writeBoolean(answer);
out.flush();



really helped me. It all started to work out much better. I have System.out.println on 15 different places around the code. It does help. But now I have another thing. When I get division by zero I have to deal with that in some way.
Where should I take care of it. Should I take care of it in the beginning were the numbers are created, i e in the class MathNumber.?

The constructor in MathNumber that creates the number looks like this:
Isn't that the best way to deal with it? I mean, somewhere here

Instead of waiting with it so that the client has to take care of it. I mean I don't want to send something that is not correct. It will only give me more problem. So it is better to take care of the problem at the source.??
But how do I do that? I have to in some way create a new mathnumber. Now it is created in MathClientHandler, line 40:



I tried to make a new method out of it; public void sendNumber() and then I changed the contructor in MathNumber to MathNumber(this)

But it didn't work out. Could you just maybe give me a hint about how to deal with it?

Anders



I have done the print statements and it helps out a bit but not the whole way. (what do you mean with state of sockets?)

Here is the server class:


The client handler:


Are there to many streams in and out of the client handler, or is it not possible to build it that way, i e that I put the first stream, creating the mathnumber, in the server not the clienthandler and sending it from the server to the client instead of sending it from the clienthandler?

Anders

Since you mentioned synchronous, then you should also check whether it is the client (GUI) waiting for the server return that "tends" to freeze (maybe server side) or nothing to do with the server code.



How do I check that?


I have been changing around the code a bit now but then I do get other problems. I have a server class, clienthandler class and a client class(+gui for client).

Server:
Here I have the Serversocket and I start up the client handlers with the code:


All the streaming at the server side is taken care of in the client handler class, i e

The server crates a new MathNumber and sends this (object) to the client

Then the client recieves the object, presents it to the user and waits for the user to put in the answer.

The user puts in the answer which is stored in the object and the client sends it back to the server

The server recieves the mathnumber(that now contains the answer from the user )


The server calculates the answer and sees if it was false or true and sends that back to the cleint

The client recieves the result(a boolean) and presents it in a suitable way for the user.


My question here is; on the server side , should it all be handled in the client handler class or should part of this be done in the server, (the first sending of a new mathnumber)?

//Anders

Thanks for answering,

When the client send numbers to the server, is it asynchronous or synchronous (client waits till server return)?



It is synchronous, i e it waits till server return.

This is how I've done it. And it is supposed to be correct.
MathClient class:


The send number to server feature should be done in another thread.


MathClient class has it's own Thread. The server has another Thread. Or is this what you ment?



or use print statements on both sides to see where things get when the application hangs



I will start to look into this and do some System.out.println(); on various places and see if I can find anything.



Thanks for answering.// Anders

I have an issue with a server - client program that causes deadlocks as I believe. The server class
creates a cleintHandler class and starts a thread there. This clientHandler class
creates MathNumbers that is sent away to the client. The number shows up in the client,(that also has an inner class with a Thread running) that
has a gui, and you're supposed to answer the number, i e putting in the number in the textfield and
clicking on the SendAway Button. The number is sent to the server that calculates the number and
sends back a true or false
The client recieves this boolean and displays if the answer was correct or false. So, this cycle goes
on like that, and you can start up as many clients as you want. The problem is that the client gui
keeps on freezing all the time. The server can send away 2, 3, 4 or 5 numbers but all of a sudden the
window freezes.
I understand that there is this method invokeLater in SwingUtilities but I don't really know how to
apply it here. I don't have Thread implemented in my gui class. The code at the send answer button
looks like this:



Someone said to me that he didn't believe that this was a invokeLater issue. But I don't know, anyone
that has an idea how to implement invokeLater here or if I at all should implement it?

The gui has the EDT and the main thread.
Hello, again!

Thanks for answering. I have now been working on this issue and I solved it by doing
Hereby the code stops and waits at the last line.

So now that works and now everything works except one thing;

the system freezes. When starting up a client gui and after entering the port numbers a new thread starts. A new thread does also mean that a new gui starts. So,

most of the time the system can generate a math number or two maybe three but then it freezes. There are a lot of different threads here, gui, thread and then the main.
Do you have any idea were to start looking for the problem. Or do you need more info?

This is the way it works:
I start the Numbergenerator thread in MathServer class and the MathClientHandler thread. In Numbergenerator I then set the numbers to work with
through nbr.setNumber(nbr1, op, nbr1);
In sendNumber(), MathClientHandler I have to wait to send new numbers to the client before having answered the number quiz.
So, I have three synchronized methods(or, at least I believe they are synchronized). They are supposed to interact through the classes and threads
MathClientHandler(MCH) class and Numbergenerator class.

public synchronized void setNumber(int op1, String op, int op2) - MathNumber class

public synchronized MatteTal getNumber() - MathNumber class

public synchronized void sendNumber() - MCH


So;

sendNumber has to wait until it can send away its number. If I set the setNumber method, operand gets a value
and therefore the setNumber method waits. When I get the number from getNumber method the operand gets a null value.
In sendNumber I can't send the number away if operand is null(i e we don't have a number to send). So we wait in sendNumber.
The thing is that the number is set so operand shouldn't be null. But how do I write the code so that nbr gets a value
(because it has a value and I do not understand how to get it) and not null?


The server client communication works fine, it is all running in the same JVM. I have to have this constructor -
MathNumber() that creates the numbers.
I can send the whole code if you want to?
Sorry, I forgot some peace of code that you need to see: Here is more of the MathNumber class:

Thank you for answering,

In MathClientHandler line 11 it should be


(instead of tal, forget talSvar),
and then on line 55 it should be


And as you say it is not assigned anything to it(nbr).

As you can see MathNumber is created in class Numbergenerator. So, then it must be possible in some way to get it to the method
sendNumber() in MathClientHandler class. How do I do that?

On my other question I just tried to show what happens when I hide the statement.

//Anders


So, I've been working on this now. I believe that I managed to solve that problem.
I changed the way to solve the issue but now I've got stuck on another thing.

I have a server and a MathClientHandler class that is supposed to send out mathematical
numbers, for example 2/4.
On the other side I have a client MathClient that is supposed to put an answer to
these numbers.

The MathClientHandler can only send out numbers when the client has answered on them.
I e it has to wait until it is OK to send.
I have a class Numbergenerator that deliveres the numbers:

The MathNumber class looks partly like this:

And then part of the server class:



So, the problem now is that I get a NPE at line 55 in the MathClientHandler class
and I can't understand why.
And if I hide the statement, ie //operand = talSvar.getOperand();
I the get this result from the console:

Waiting for a new number to send

, i e operand = null, but how can it be null?

If I do a System.out.println("")
System.out.println("op1: " + nbr.getNumber1());
System.out.println("op2: " + nbr.getNumber2());
System.out.println("op: " + nbr.getOperand());

The result is for example
op1: 3
op2: 13
op: /

i e operand is not null.


What is wrong here?

As you can see I have three synchronized methods and two threads here. Is there something wrong with the synchronization?
Please, anyone?

Yes, you are right. Forget my previous message.

Thank you-I have to look into the code again.

Thanks, Anders

Thank you for answering, but why is mcn declared null?
Could you please explain that?

Anders
I can't figure out why I keep on getting the message Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException.

I have a server client program where the server sends out a mathnumber to the client. The number is shown up on a gui textfield. I put in my answer and hit the sendbutton.

Here is part of the so called MathClientHandler class.


And here is the Client class which has an inner class:



And here is the gui class:


So, when I execute this I get the error message:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Lektion5.MathClientGUINy.actionPerformed(MathClientGUINy.java:147) <--- So, this is the problem
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)


Why do I get a NPE here?
There is something wrong with the line

mcn.sendAnswer(tal);

Why doesn't the code understand it, or mcn?

Could anyone please help me here?


Anders
Hello!

I have an issue here regarding streams and server - client. This is an university course and it is my absolutely last exercise. And I just can't figure out the last part . Hopefully I will be able to explain the problem without showing the whole code. The server class sends a number, for example 14 + 27. In the gUI I have a JTextField where I put in the answer, i e 41. I then click on the send button in order to send the answer to the server. (I am the client). But this sending can't be done before I click the send button. And this is my problem. In the ClientgUI it looks like this:

And in the MathClient class it looks like this:

I mean; when the program in Mathclient has reached sendAnswer(number) it must stay there and not execute further. It has to start execute further when I click on the send button. I need the number to be "handed over" from the gui class to the client class. But how shall the sendAnswer method look like?

Hopefully you can follow me here.

Anyone? Or do I have to send more code?

Anders