The read() method of java.io.BufferedReader class returns int value. The value is -1 when there is End of File. Rite?
Now, i was trying to make a client/server application in which a server application sends some data and the client application reads the data using the following contruct:
Now, the condition in while fails only when server closes the socket for this client. Please tell me what is the charater or how i can tell the client to stop wating or in.read() returns -1 without closing the socket/stream.
If you want to keep your socket open, you can't use EOF to indicate the end of a message. What you want to do is create a protocol, or a set of rules for exchanging messages. There's an example of this technique in the Java Tutorial: Custom Networking trail.
If you want to keep your socket open, you can't use EOF to indicate the end of a message.
I want to keep the socket open, thats rite. But I want to use EOF to indicate the normal bufferReader.read() method that I am finish sending data to you, not the value u should return is -1.
I mean I simply want to know how read method comes to know that the server application is finished sending data and returns -1. Is it; - Socket.close() sends some speacial EOF charater to tell reader. - Socket.close() sends some special packet to tell reader.
In both the cases, what is that characater/packet?
The Socket objects knows the connection is closed when the underlying, platform-specific implementation tells it so. As a Java programmer, you have no access to this lower layer. This is covered, at least obliquely, in the tutorial Joe pointed you to.
The rest of Joe's answer is right on, too: you need to invent a protocol. This can be as simple as the server sending a 4-byte integer telling the client how many bytes of data to expect, then sending the data, and the client looping only long enough to read the correct number of bytes. Again, this is covered in the networking tutorial.