Connecting Tech Pros Worldwide Help | Site Map

InputStream .read()

  #1  
Old June 23rd, 2009, 11:50 PM
hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
Provided Answers: 2
Expand|Select|Wrap|Line Numbers
  1.     public String receive() {
  2.         InputStream is;
  3.         try {
  4.             is = socket.getInputStream();
  5.         } catch (IOException e) {
  6.             e.printStackTrace();
  7.             return null;
  8.         }
  9.         String result = new String();
  10.         int c;
  11.         while (true) {
  12.             try {
  13.                 c = is.read(); //problem
  14.                 if (c == -1) {
  15.                     return result;
  16.                 }
  17.             }
  18.             catch (IOException e) {
  19.                 return result;
  20.             }
  21.             //System.out.print((char)c);
  22.             result += (char)c;
  23.         }
  24.     }
The purpose of the code is to read bytes from the TCP socket.
is.read() is supposed to give IOException when the stream ends. But its not throwing exception, instead the program keeps running and has to be stopped.

Anybody able to locate the mistake in the code?

I think mainly the problem is with reading end of the stream.

Thanks :)
  #2  
Old June 24th, 2009, 07:06 AM
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2

re: InputStream .read()


If the other side of your socket doesn't explicitly close its Stream, your side of the socket will never receive an end of file condition, i.e. it simply waits for more bytes to arrive over the wire.

kind regards,

Jos
  #3  
Old June 24th, 2009, 11:38 AM
hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
Provided Answers: 2

re: InputStream .read()


Thanks for the reply Jos. Is there any why, any timer or something that I can use to break the loop after say 10 seconds even if it does not return eof condition?
  #4  
Old June 24th, 2009, 01:58 PM
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2

re: InputStream .read()


Quote:
Originally Posted by hsriat View Post
Thanks for the reply Jos. Is there any why, any timer or something that I can use to break the loop after say 10 seconds even if it does not return eof condition?
You have to use the 'nio' package for that because an InputStream.read() call blocks until the bytes are read. Why don't you make your server side send the number of bytes before it sends them over the wire? That way your client side knows how many bytes to expect in total.

kind regards,

Jos
  #5  
Old June 24th, 2009, 05:16 PM
hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
Provided Answers: 2

re: InputStream .read()


Thanks for the reply Jos.
Actually, there's no server on the other end. Its a hardware device with ethernet port which intracts through a set of AT commands. It replys back, but does not give eof. So after info is recieved, read() function gets blocked.
I used socket.setSoTimeout, and it works, but this is not how I want it to be implimented.

Can you please through some light how to use nio package?
  #6  
Old June 24th, 2009, 05:44 PM
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2

re: InputStream .read()


Quote:
Originally Posted by hsriat View Post
Thanks for the reply Jos.
Actually, there's no server on the other end. Its a hardware device with ethernet port which intracts through a set of AT commands. It replys back, but does not give eof. So after info is recieved, read() function gets blocked.
I used socket.setSoTimeout, and it works, but this is not how I want it to be implimented.

Can you please through some light how to use nio package?
Well, there's a link to the API docs in the first article of this forum section. Some of the documentation has links to the tutorials but why don't you parse the input content and figure out when the device has sent what it had to send?

kind regards,

Jos
  #7  
Old June 26th, 2009, 03:34 PM
hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
Provided Answers: 2

re: InputStream .read()


Thanks for the reply Jos. I think I would have to use socket.setsotimout as a solution. Will keep thinking for some other alternative too.

Regards
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read a file Jromero answers 8 October 17th, 2007 01:17 AM
I can not read a small file from NTEXT field in the database =?Utf-8?B?ZGF2aWQ=?= answers 3 February 26th, 2007 04:55 PM
FIle Upload and InputStream into Excel bvasanth123@rediffmail.com answers 0 November 19th, 2005 09:35 PM
InputStream Read - how to ignore comma Rakesh Sinha answers 3 July 23rd, 2005 12:28 AM