473,395 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

InputStream .read()

hsriat
1,654 Expert 1GB
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 :)
Jun 23 '09 #1
6 7460
JosAH
11,448 Expert 8TB
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
Jun 24 '09 #2
hsriat
1,654 Expert 1GB
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?
Jun 24 '09 #3
JosAH
11,448 Expert 8TB
@hsriat
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
Jun 24 '09 #4
hsriat
1,654 Expert 1GB
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?
Jun 24 '09 #5
JosAH
11,448 Expert 8TB
@hsriat
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
Jun 24 '09 #6
hsriat
1,654 Expert 1GB
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
Jun 26 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Rakesh Sinha | last post by:
I have a very trivial question. But I searched in google / archives of this group to get the answer, checked the C++ FAQ - but did not precisely what I was looking for. The problem is with...
1
by: Charles.Deisler | last post by:
XmlTextReader requestdata = new XmlTextReader(Request.InputStream); XmlTextWriter xmltextwriter = new XmlTextWriter(somefile,someencoding); XmlTextWriter xmltextwriter2 = new...
0
by: Keith Harris | last post by:
Hi, I have an HtmlInputFile control which allows a user to upload a CSV file. I want to display the first few rows for preview and later, load the rows to a database. Displaying the preview...
0
by: bvasanth123 | last post by:
Hi, I would like to know how can I read an InputStream into Excel object. In my web app, user uploads MS-Excel file though asp.net html file control. I get UploadedFile.PostedFile.InputStream()...
0
by: bvasanth123 | last post by:
Hi, I would like to know how can I read an InputStream into Excel object. In my web app, user uploads MS-Excel file though asp.net html file control. I get UploadedFile.PostedFile.InputStream()...
1
by: O.B. | last post by:
I have the need to read a file (version.txt) that is embedded within an WAR file that is within an EAR file: foo.ear contains bar.war contains resources/version.txt I am able to get the bar.ear...
0
by: raheel javed | last post by:
i am using mobile for communication with live server when mobile send its information to server the input stream is encoded in utf-8 that is converted into string but it is still not...
4
by: macap.usenet | last post by:
Hello, I´ve a strange problem with my HttpPostedFile object. I coded a File Upload where the user can upload a zip file. My code looks like this: HttpFileCollection files =...
11
by: John Krukoff | last post by:
-----Original Message----- First off, ignore castironpi, it's a turing test failure. Second, I'm curious as to how Java manages this. I'd think their streams would have to be pretty magic to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.