• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cannot loop without freeze on socket networking  RSS feed

 
Amit Shef
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I'm writing to server from client it freeze and stuck for a second when the server receive it.
and its just annoying.

Server:


Client:


And another thing i need to loop to send multiple of messages to the server , is there another way to send multiple messages without loop?
And without freezing the application.
Thanks in advance.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What exactly does "it freezes" mean? What is freezing (server or client) and how do you know?
 
Amit Shef
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the client that it freezing...

And by "It freezes" i mean that:

When I'm clicking a button to write to the server the client freeze for just a second.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A temporary freeze or a permanent freeze? The problem I mentioned in http://www.coderanch.com/t/636003/java/java/Send-message-server-client-time#2915417 does not apply? It might manifest itself in a something that looks like a freeze.
 
Amit Shef
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Temporary freeze.
The all application goes clean.

But when i click the button to write to server it just freeze for a moment and come back after a second
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit Shef wrote:It's the client that it freezing...

And by "It freezes" i mean that:

When I'm clicking a button to write to the server the client freeze for just a second.


I bet you are using the client event handling Thread to write to the socket - of course it can't respond to new events.

Bill
 
Amit Shef
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes
The client is handled with a thread.

So you are suggesting to remove it?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We would need more code. Like William said, it looks like the socket communication is happening in the same thread that is used to paint displays and handle button inputs (the event dispatch thread). You would need to move it to a different thread since you shouldn't put long running tasks in the EDT (see here). But we can't be sure without more code.
 
Amit Shef
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok i listened to what you said and i searched on the internet about EDT and some people say you need inovkeLater from EventQueue so i tried it and now I'm getting another error says

java.lang.NoClassDefFoundError : java.awt.EventQueue

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that is the opposite of what you want. You don't want to make the task eventually to run on the EDT, you want to make sure the task is NOT run on the EDT. Read the tutorial link I posted.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit Shef wrote:java.lang.NoClassDefFoundError : java.awt.EventQueue
What environment are you running your client in? Servlets? Android? JME?...
 
Amit Shef
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Android
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is an important fact. Android does not use Swing or AWT, so the tutorial I posted won't be much help, nor would any Swing or AWT APIs like the EventQueue (the principal is the same, but the classes it uses aren't available). So you need to use Android-specific API. The Android API is here: http://developer.android.com/reference/packages.html. The key class you will be looking for is the AsyncTask. You can learn about making responsive UIs here
 
Amit Shef
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still dosent work i used AsyncTask.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly how are you connecting the "clicking a button" event to the socket sending code?

Bill
 
Amit Shef
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just put all the client code in the setOnClickListener
Like this:

button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {

//////CODE

}
});
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, the part you put /////CODE looks like it might be important to see. Just saying.

Why are you making it so hard to help you? Why not post all the code so we can see what is happening rather than guessing?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:Why are you making it so hard to help you? Why not post all the code so we can see what is happening rather than guessing?

That should be all the relevant code - the part from where the click happens to whatever makes the connection to the server and this loop you keep talking about.
 
Amit Shef
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please notice the arrows where it points and the note there.


try {
loop = true;

final Handler clientLoop = new Handler();
clientLoop.post(new Runnable() {

@Override
public void run() {
try {
client = new Socket(IP, PORT); <----------- I THINK HERE IS THE PART WHERE IT FREEZE
} catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace();
}
try {

in = new BufferedReader( new InputStreamReader( client.getInputStream())); printlng = new PrintWriter( client.getOutputStream()); printlng.println(MESSAGE); <---------- OR HERE THE PART WHERE IT FREEZE printlng.flush(); } catch (IOException e1) { e1.printStackTrace();
}
try {
if ((serverResponse = in .readLine()) != null) {
System.out .println("Server Response : "
+ serverResponse);
}
} catch (IOException e1) {
e1.printStackTrace();
}
try {
in.close();
client.close();

} catch (IOException e) {
System.out .println("EXCEPTION : " + e); e.printStackTrace();
}

if (loop) {
clientLoop.postDelayed(this,
4000);
}
}
});
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amit,

Can you please UseCodeTags (<- that is a link). I was about to edit your post to do it, but the code is so poorly formatted that it is hardly readable without a lot of effort. So can you please edit your post, add code tags and make it readable? That would be appreciated, thanks.

It appears you are using a Handler. A Handler uses a MessageQueue to execute tasks and pass messages to the Thread in which the Handler was created. Now, I asked you to post all the code, from button click onward, but you didn't do that. So I have to make an assumption, and the assumption I make is that you run that code in the event dispatch thread. Which means the Runnable you submit to the Handler would be run in the EDT.

As I said before, you should be using AsyncTask, not Handler. You should read the link I posted previously.
 
Amit Shef
Ranch Hand
Posts: 81
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much.

I wrapped the code and put it on service.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome, glad you solved the problem.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!