This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

client hangs after getting data from server

 
mihir maniar
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

clientbfw.write(username+"\n");
clientbfw.flush();

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

while((temp==bfr.readLine())!=null)
{
System.out.println(temp);
}

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
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
mihir maniar
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 socket.so how should i do that?
 
Elliotte Rusty Harold
author
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Santa Maria
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,
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
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic