Solved

Java inputstream never leaves

Posted on 2006-12-01
10
319 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

770 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