Java inputstream never leaves

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!
LVL 17
CSecurityAsked:
Who is Participating?
 
Ajay-SinghConnect With a Mentor Commented:
it happens when
a. client closes the connection
b. you close the OutputStream of client
0
 
Ajay-SinghCommented:
read is blocking call, will wait untill it receives data from client
0
 
Ajay-SinghCommented:
You should define boundry of the request and read till that point.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
CSecurityAuthor Commented:
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
 
CSecurityAuthor Commented:
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
 
Ajay-SinghCommented:
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
 
CSecurityAuthor Commented:
how to exit while() loop? I should use return?
0
 
marchentConnect With a Mentor Commented:
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
 
Ajay-SinghCommented:
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
 
girionisConnect With a Mentor Commented:
> 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
All Courses

From novice to tech pro — start learning today.