Author
Client-Server communication; client freezes
Khalil Salman
Greenhorn
Joined: Sep 05, 2010
Posts: 11
Hi,
the story:
Clients sends a request, server reads that request, and sends a response, lastly client reads the response.
There will be multiple clients so I tried to implement it using threads in the server code to be able to continue listening for new requests.
But in the last step the client freezes when I say read the response from the server. I mean the client sends a request successfully, and the server reads it and responses successfully but the client then cannot get the response; freezes.
Here is the code:
This is for the client:
And here is the server:
And the server's response sender run() function:
Thank you for any help..
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35223
posted Jul 08, 2011 10:13:47
0
Dont't println to a Socket: http://developer.apple.com/technotes/tn/tn1157.html
And don't ignore exception: } catch(Exception exc){}
That's a no-no.
Android apps – ImageJ plugins – Java web charts
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16480
If you really want the server to be multi-threaded then this is wrong:
To start a Thread running, do this:
And "handler" would need to be a Thread, not a Runnable.
Khalil Salman
Greenhorn
Joined: Sep 05, 2010
Posts: 11
Thank you guys.
Yeah I commented out println() parts and change the Runnable to Thread. And I replaced handler.run() with handler.start().
But nothing changed.
Something else should be missing. Something that I don't know but very critical.
Gosh 15-20 lines of code but I cannot get it working.
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16480
So show us the new code, the version which doesn't use PrintWriter and which writes your exceptions to somewhere you can look at them.
Khalil Salman
Greenhorn
Joined: Sep 05, 2010
Posts: 11
Alright. Now I see what you mean by saying "don't println to socket."
Don't use PrintWriter !..
Ulf, the link you gave for "don't println to socket" redirects me to somewhere else not to an article.
So please check it for future references.
Thank you guys anyway, the issue is resolved.
Here is the code, (maybe someone needs it in the future)
The server code:
server's response sender:
And the client:
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35223
posted Jul 09, 2011 11:00:40
0
Huh, kind of stupid to break long-established URLs; but here is the article .
subject: Client-Server communication; client freezes