Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting out of input stream loop (reading byte by byte)

Status
Not open for further replies.

jakeisstoked

Technical User
May 9, 2003
28
AU
Ok, I have this really simple function in some client / server programs, I am trying to read an input stream, it sends ok, says I send something like

"ostream.write("message".getBytes());" on a client

and it sends. And this little procedure reads the stream;


private void processRequest(){
String request = new String();
int Byte;
while(true){
try{
inStream = socket.getInputStream();

while((Byte = inStream.read()) >= 0){
request = request + (char)Byte;
}

System.out.println(request);
}
catch(Exception e){
e.printStackTrace();
}
}
}

Ok the first while is supposed to be infinite,
but I want to get one "message" at a time, but still read byte by byte. I can recieve the stream byte by byte, but I can't figure out how to terminate the second while loop, it just stays in it reading bytes forever (and when it gets to the end of the input stream it just sits there, it never gets to "System.out.println(request);").
Any clues? I know this is really simple, any tips would be much appreciated.
-Jake
 
You need to send an EOF at the end of your write.
I suppose something like
Code:
ostream.write(-1);
would do the trick.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
... or maybe you should close your ostream after sending the message. Not sure if this would automatically send an EOF. You should be closing the output stream when you're done with it in any case.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Hey, Merry Christmas sedj.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top