- public String receive() {
-
InputStream is;
-
try {
-
is = socket.getInputStream();
-
} catch (IOException e) {
-
e.printStackTrace();
-
return null;
-
}
-
String result = new String();
-
int c;
-
while (true) {
-
try {
-
c = is.read(); //problem
-
if (c == -1) {
-
return result;
-
}
-
}
-
catch (IOException e) {
-
return result;
-
}
-
//System.out.print((char)c);
-
result += (char)c;
-
}
-
}
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 :)
6 7460
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
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?
@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
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?
@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
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
by: Charles.Deisler |
last post by:
XmlTextReader requestdata = new XmlTextReader(Request.InputStream);
XmlTextWriter xmltextwriter = new XmlTextWriter(somefile,someencoding);
XmlTextWriter xmltextwriter2 = new...
|
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...
|
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()...
|
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()...
|
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...
|
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...
|
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 =...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
|
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...
| |