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
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
posted
0
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 SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
mihir maniar
Ranch Hand
Joined: Sep 09, 2003
Posts: 88
posted
0
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
Joined: Feb 25, 2004
Posts: 91
posted
0
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="http://cafe.elharo.com/web/refactoring-html/" target="_blank" rel="nofollow">Refactoring HTML</a>
Paul Santa Maria
Ranch Hand
Joined: Feb 24, 2004
Posts: 236
posted
0
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
posted
0
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
Joined: Jan 14, 2005
Posts: 1
posted
0
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
posted
0
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: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: client hangs after getting data from server