This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

BufferReader.read() int  RSS feed

 
Sandeep Jindal
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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.

Thanks in advance
Sandeep Jindal
IBM
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sandeep Jindal
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Joe,

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?

Hope this clears my question.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!