Solved

Java inputstream never leaves

Posted on 2006-12-01
10
318 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now