Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Java inputstream never leaves

Posted on 2006-12-01
10
Medium Priority
?
344 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 1000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 800 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 200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

824 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