Solved

Java inputstream never leaves

Posted on 2006-12-01
10
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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 This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

617 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