Home arrow static arrow Java Programming [Archive] - inputstream read takes a long time to return.
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - inputstream read takes a long time to return.
This topic has 11 replies on 1 page.

Posts:41
Registered: 6/13/04
inputstream read takes a long time to return.  
Jun 13, 2004 1:09 AM



 
Hello,

I have a java application which sends a request to a server, and gets a response back.
on some requests, which return some 8 lines of data, the stream is slow.. and on some
requests which receive say one line of response, the stream is ok. (not so fast still).

I understand it could be the time to connect and read the information.
But, the same application i have a C++ version.. which also uses sockets and a stream reading mechanism, and this application works faster. and gets responces much quicker. so the java
implementation has a pb (below is my implementation). and the pb escapes me.

Any help is appreciated..

-------src code

Socket MyClient;
MyClient = new Socket(<ip address>,8080);
printin = new DataInputStream (MyClient.getInputStream());
printout = new DataOutputStream(MyClient.getOutputStream());

//send request
printout.writeBytes (cmdString);
printout.flush ();

// Get response data.

String str="";
String str1="";

while (null != ((str = printin.readLine())))
{
str1 += newline + str;
}

jTextArea2.setText(str1 + "\n\ndone");

----src ends

Thanks
'Harish palaniappan
 

Posts:11,200
Registered: 7/22/99
Re: inputstream read takes a long time to return.  
Jun 13, 2004 1:40 AM (reply 1 of 11)



 
Some observations:
o Pretty much all I/O operations are faster if you use streams that buffer data. See if this makes it any faster:
printin = new DataInputStream (                   new BufferedInputStream(MyClient.getInputStream()));printout = new DataOutputStream(                   new BufferedOutputStream(MyClient.getOutputStream()));
o Concatenating strings with += in a loop is relatively slow. You should use StringBuffer and the append method instead. However if there are only 8 lines of text I doubt that has a big overall effect.
o You use the deprecated DataInputStream.readLine method. If what you read is text you should be using BufferedReader.readLine instead. If what you read is binary data you should be reading into an array of bytes instead of a string.
 

Posts:41
Registered: 6/13/04
Re: inputstream read takes a long time to return.  
Jun 14, 2004 12:27 AM (reply 2 of 11)



 
hi jsalonen,

i tried using the bufferedreader, it didnt fasten the read process.

STRANGE: the code takes atleast one minute on the first readLine() operation alone.. not in the subsequent readLine() operations in the while loop

.. the new code with the buffered reader that i am using is as below.

printin = new BufferedReader(new InputStreamReader(MyClient.getInputStream()));
while (null != ((str = printin.readLine())))
{
str1 += newline + str;
}
 

Posts:41
Registered: 6/13/04
Re: inputstream read takes a long time to return.  
Jun 14, 2004 12:35 AM (reply 3 of 11)



 

also. this code produces blocking while it reads the socket..
i dont find a non-blocking read on a datainputstream..
are there some classes or packages to do a non-blocking read.
 

Posts:37,103
Registered: 3/30/99
Re: inputstream read takes a long time to return.  
Jun 14, 2004 12:46 AM (reply 4 of 11)



 

also. this code produces blocking while it reads the
socket..
i dont find a non-blocking read on a datainputstream..
are there some classes or packages to do a
non-blocking read.

Look at the java.nio.channels package.
 

Posts:11,200
Registered: 7/22/99
Re: inputstream read takes a long time to return.  
Jun 14, 2004 1:34 AM (reply 5 of 11)



 
takes atleast one minute on the first readLine() operation alone..

And the native code doesm't? Are you sure you are doing the exact same thing in Java and C++?

This sounds like a network configuration problem or a problem with the server software, it can't take over a minute to connect to a server and read the first byte...
 

Posts:41
Registered: 6/13/04
Re: inputstream read takes a long time to return.  
Jun 14, 2004 10:16 AM (reply 6 of 11)



 
yes. could be.. i will check on that..

meanwhile.. i found a lot of resources saying bufferedreader is "the choice".. for the purpose..
i also found a strange reasoning on a forum for a similar pb that.. the guy had netbeans installed.. he uninstalled it.. and it was ok..
i also have netbeans.. i find this crazy.. has anyone come across a similar pb like mine..

could it be a network pb, like a firewall, a proxy.. etc., i find the pb existing.. to whichever server i connect to.. the delay is directly proportional to the no. of bytes in the response..

any help is appreciated.

Thanks
'Harish
 

Posts:41
Registered: 6/13/04
Re: inputstream read takes a long time to return.  
Jun 17, 2004 6:12 AM (reply 7 of 11)



 

Hi people.,

I tried many means.. for this problem. It hasnt helped.
I want to confirm this as a server response problem, but the c++ code which implements winsockets connects and reads data in 2 or 3 seconds.

Try connecting to this server,
MyClient = new Socket("213.193.201.38",2080);

send a "GET\r\n" request.

My place it takes 61 seconds for a response. no matter what class i use for teh reader, and how many ever bytes i read. The first read() or read(bytes) or readline() takes 61 secs and the rest of the socket is read like it reads a local file.

i tried telnet 213.193.201.38 2080 on the command prompt and sent a GET .. and it also took 60 secs..
how is that c++ code alone could do it better.
 

Posts:11,200
Registered: 7/22/99
Re: inputstream read takes a long time to return.  
Jun 17, 2004 7:45 AM (reply 8 of 11)



 
There's something strange about that server, how is it implemented?

If you change your code to connect to for instance microsoft.com port 80 the reply is almost instantaneous.
 

Posts:41
Registered: 6/13/04
Re: inputstream read takes a long time to return.  
Jun 17, 2004 9:07 AM (reply 9 of 11)



 

hi people..

PROBLEM FOUND
finally.

it was an implementation on the server that all requests should follow by two <CRLF>
i had only one.. and in my tests with "GET" also i had only "GET\r\n" .. it should have been \r\n\r\n

finally i got a reply from my server admin who said.. it should end with 2 <CRLf>... and that the server was waiting for that timeout (60 secs) expecting u to complete the request.
:o)

anyone thanks for your comments.
 

Posts:11,200
Registered: 7/22/99
Re: inputstream read takes a long time to return.  
Jun 17, 2004 10:19 AM (reply 10 of 11)



 
Ah, so in the end it wasn't Java's fault, only an incomplete specification :)
 

Posts:41
Registered: 6/13/04
Re: inputstream read takes a long time to return.  
Jun 19, 2004 12:02 AM (reply 11 of 11)



 
yep..

excuse me for that :o)
Thanks for your help.
 
This topic has 11 replies on 1 page.