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
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...
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?
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>
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
Joined: Jun 08, 2002
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 ]
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.
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.