File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes client hangs after getting data from server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "client hangs after getting data from server" Watch "client hangs after getting data from server" New topic

client hangs after getting data from server

mihir maniar
Ranch Hand

Joined: Sep 09, 2003
Posts: 88
i have designed a client server architecture where in the client asks the server to authenticate and allow hime to chat. the client hangs when the login button is pressed. the server authenticates him properly and it informs the client that he is valid .
the code of client to server for authentication is


on the server side the server authenticates him and notifies him that he is authenticated or not and the client does get the info in the following way


the client prints the information from the server but then it just hangs. what is wrong that i am doing .please guide me...
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
Is there any exception that the client throws? If not, it is alright with your code. I think you just need to close the application peacefully, after the client got the response from the server.

Or your code must be inside while(true) loop so that you can chat with the server back and forth, sending the requests and responses... This is called blocking approach in client-server chat application...

There also is non-blocking approach, which is discussed in the "Java Network Programming, Third Edition" from Manning Publishing... You might want to have a look at that too...

Hope it helps...

Co-author of SCMAD Exam Guide, Author of JMADPlus
mihir maniar
Ranch Hand

Joined: Sep 09, 2003
Posts: 88
i want a back and forth talk between the client and the server and when they are done talkin then the client can inform the server that he wants to log out and that is when i want to close the how should i do that?
Elliotte Rusty Harold
Ranch Hand

Joined: Feb 25, 2004
Posts: 91
There are several things that could go wrong here. It's hard to tell without seeing the complete code. However, one thing does jump out at me: readLine(). Never, never, never use this method. It is dangerous and evil; and causes exactly the problem you're seeing. I can't be sure that's the problem here, but I wouldn't even bother trying anything else until you removed that from your code.

If you do need readLine-like funcitonality then you should reinvent it on top of the standard read() methods rather than relying on the buggy versions built into Java. Example 4-1 of Java Network Programming shows you how to do this. See Example 4-1 on Cafe au Lait

Elliotte Rusty Harold<br />Author of <a href="" target="_blank" rel="nofollow">Refactoring HTML</a>
Paul Santa Maria
Ranch Hand

Joined: Feb 24, 2004
Posts: 236
Mihir -

Please be sure to post back to the group and let us know if changing "readLine()" (which is very definitely susceptible to hangs under far too many scenarios) to "read()" (inside of a loop) fixed the problem.

Thanx in advance .. PSM
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
mihir sent me his source codes for server and client as private. Here it is.

Well, u guys might want to compile them and run them. He asked my help in the private message. I am going to find the errors out soon. If u guys see anything wrong with the codes, please don't hesititate to point them here... Thanks...
[ December 08, 2004: Message edited by: Ko Ko Naing ]
alex balka

Joined: Jan 14, 2005
Posts: 1
I would like to comment the usage of readLine() in the while loop:
When using readLine() in a loop for reading from a file, readLine stopps when the file ends. The socket ends when it is closed, i.e.readLine() in a while loop for reading from a socket will wait forever until the socket is closed.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
If you can use JDK 1.4+, you might want to check out the new I/O packages (java.nio.*). This ONJava article covers using non-blocking sockets.

Certainly there is a solution to your current issue using the standard I/O packages (I admit I haven't read this whole thread), but if this project is for learning, here's another area for exploration.
I agree. Here's the link:
subject: client hangs after getting data from server
It's not a secret anymore!