Connecting Tech Pros Worldwide Forums | Help | Site Map

InputStream .read()

hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
#1: Jun 23 '09
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 :)

JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#2: Jun 24 '09

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
hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
#3: Jun 24 '09

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?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#4: Jun 24 '09

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
hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
#5: Jun 24 '09

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?
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#6: Jun 24 '09

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
hsriat's Avatar
Expert
 
Join Date: Jan 2008
Location: Bath, UK
Posts: 1,609
#7: Jun 26 '09

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