I am trying to read bytes from server using Socket program, ie I am using InputStream to read the bytes. If I pass the length size I am able to read the bytes, but I am not sure what may be the length. So I am not able initialize the byte array.
Also I tried while (in.read() != -1)
, I observered it loop works fine when the data is sent, but the next line after the loop is not executable, I feel its still looking for the data in the stream but there is no data. If I close the Server connection, then my client will execute the next line followed to the loop.
I am not sure where I am going wrong?
this.in = socket.getInputStream();
int dataInt = this.in.read();
while(dataInt != -1){
System.out.print(","+i+"--"+dataInt);
i++;
dataInt = this.in.read();
}
System.out.print("End Of loop");
I get the output as:-
,1--0,2--62,3--96,4--131,5--142,6--1,7--133,8--2,9--16,10--48,11--56,12--1,13--0,14--14,15--128,16--0,17--0,18--0,19--48,20--0,21--0,22--0,23--0,24--0,25--1,26--0,27--0,28--38,29--114,30--23,31--20,32--70,33--3,34--20,35--1,36--133,37--48,38--51,39--49,40--52,41--49,42--55,43--49,44--52,45--52,46--54,47--55,48--50,49--51,50--52,51--48,52--53,53--56,54--51,55--48,56--48,57--57,58--57,59--57,60--57,61--57,62--57,63--57,64--56
But no output for :- End Of loop
Please guide how shall I close the loop?
Looking forward for you response. Thanking you all in advance.
In case -1 it does not end the loop. Put 65535 in the condition and i am 99% sure it will stop the loop.
You can run this example.
If you are going to wait for the end of the stream you have to close it on the sending side for and EOF (-1) to be received.
For sending multiple binary messages I prefer to send the length before the message as it allow the code to read large blocks at once (i.e. because it knows how much it is going to get)
prints
I had also the problem that I did not come out of the loop. My solution looked similar to this:
I've had a similar problem where
in.read()
just hangs at the end of the data rather than returning a -1.If you have no control over the server code, is there a reliable marker you can detect in the returned data (e.g.,
</html>
) and use that to end the reading loop?Failing that, consider using
socket.setSoTimeout(reasonableValue)
so at least your app won't be kept hanging forever...I think you've actually answered your own question.
The reason you are not exiting the loop is that the end of input stream only happens on the client end after the server end closes its socket. (Or more precisely, after it closes its socket output stream ... or the equivalent ... and the close event has propagated to the client end.)
Until that event happens, it is possible that the server could decide to write more data to the socket. So the client-side read blocks ... until it either gets more data or it sees the protocol event that says that the server end has closed.
(Actually, other events can unblock the client read, but they will all result in an
IOException
of some kind, and probably a stream that you can't read any more data from.)Now, if you don't want to close the server end now because you want to send more stuff on the socket later on, you are going to have to change your application protocol to use some kind of framing mechanism. For instance, the server might send a frame (or record, or whatever) consisting of byte count followed by the given number of bytes. Alternatvely, it could use a distinguished byte value or byte sequence to mark the end of a frame.
You can safely close the stream after the
while
loop.dataInt
being -1 means that there is nothing more to read from it.http://download.oracle.com/javase/1.4.2/docs/api/java/io/InputStream.html#read()
If the while loop is not exiting it means that there is still data being written at the other end of the stream. Close the stream at the other end. If you can post code where you write data to stream that will be helpful.