Solved

Java inputstream never leaves

Posted on 2006-12-01
10
320 Views
Last Modified: 2008-02-01
I'm coding a server app in Java.

I wrote data receive part in server like this:
String s;
in = new DataInputStream(client.getInputStream());


And below this code after some other processings...
  while ((i = in.read()) != -1) {
                    s = s + (char) i;
                }
System.out.println("OK! We received data part");

This code never prints out  "OK! We received data part" till I close connection from client!
Why?

So how can I read data from inputStream?

Thanks from now!
0
Comment
Question by:CSecurity
10 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18058921
read is blocking call, will wait untill it receives data from client
0
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 250 total points
ID: 18058923
it happens when
a. client closes the connection
b. you close the OutputStream of client
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18058926
You should define boundry of the request and read till that point.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 17

Author Comment

by:CSecurity
ID: 18058929
b. you close the OutputStream of client
How? My client is coded in VB6.

And is there another call or reading method which can leave in inputstream?
0
 
LVL 17

Author Comment

by:CSecurity
ID: 18058931
P.S please tell me exit while code.... (in while how can I exit) in VB I write-> exit for. exit do or...

Thanks
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18058949
Not sure how you can close output fd in VB. If you are expecting data from the client in terms of line, you can use readLine() function
0
 
LVL 17

Author Comment

by:CSecurity
ID: 18058951
how to exit while() loop? I should use return?
0
 
LVL 13

Assisted Solution

by:marchent
marchent earned 200 total points
ID: 18059098
suppose u wanna break when i = -1, then use this code

while( ...condition..)
{
   if(i == -1) break;
}

it will get u out from while loop

~marchent~
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18059872
Assuming that from VB program, you are trying to send the commands line by line,
i would suggest to use the following:

String command = in.readLine();
// Work on the command

Do NOT read char by char
0
 
LVL 35

Assisted Solution

by:girionis
girionis earned 50 total points
ID: 18060072
> how to exit while() loop? I should use return?

You can use return if you want ot return a value, you can even use it to exit the loop (and also the whole method). If you only want to exit the loop follow marchent's suggestion a use break;
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question