Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Sockets and Internet Protocols and the fly likes Client-Server communication; client freezes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Client-Server communication; client freezes" Watch "Client-Server communication; client freezes" New topic
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: 41063
    
  43
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.


Ping & DNS - my free Android networking tools app
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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: 18541
    
    8

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: 41063
    
  43
Huh, kind of stupid to break long-established URLs; but here is the article.
 
 
subject: Client-Server communication; client freezes
 
Similar Threads
Client-Server Problem
I/O stream from Socket not ready
Socket constructor error
chat program
Servlet writing and reading String from Socket