• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

BufferReader.read() int

 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 9625
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic