Home arrow static arrow Java Programming [Archive] - strangely problem with a InputStream
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - strangely problem with a InputStream
This topic has 7 replies on 1 page.

Posts:47
Registered: 5/14/03
strangely problem with a InputStream  
Jul 22, 2004 1:56 AM



 
Hello,
i have a problem and hope that you can help me.
I have a server client connection using 'ServerSocket' and 'Socket'.
The client send's the data of some files with the following structure:
1 Byte -> length of a filename
some bytes containing the filename
8 Byte -> length of the data
data
and so on
<<

The problem is that the server don't read it correctly. (I'm using a BufferdInputStream)
With the following code there is no problem:

// here starts a loop
int namelen = in.read();

byte[] name = new byte[namelen];
in.read(name);

long len = 0;

// reading the length of the data (it's not important yet)

System.out.println(new String(name));
System.out.println(new Long(len));

FileOutputStream fw = new FileOutputStream(new File(dir, new String(name)));

int r;
for(int ii = 0; ii < len && (r = in.read()) > -1; ii++) fw.write(r);

fw.close();
// here ends the loop

(But you know it is realy slow)

Using this code ...

// here starts a loop
int namelen = in.read();

byte[] name = new byte[namelen];
in.read(name);

long len = 0;

// reading the length of the data (not important)

System.out.println(new String(name));
System.out.println(new Long(len));

FileOutputStream fw = new FileOutputStream(new File(dir, new String(name)));

for(long k = 0; k < len; k+= buf.length){
int buf_length = buf.length;
long k_buf_length = k + buf_length;
if(k_buf_length > len){
buf_length = buf_length = (buf.length - (int)(k_buf_length - len));
}

in.read(buf, 0, buf_length);
Thread.sleep(1); // !!!!!!!!!!!!!!! that's important
fw.write(buf, 0, buf_length);
}
fw.close();
// here ends the loop

... it runs too. But if i remove the Thread.sleep(1) only the first file will be read corretly,
but the second one contains some MB of the first one. The second file is named from some bytes of the contents of the first file.
Hope it is understandable.

What can i do?

Stev Glasow
 

Posts:964
Registered: 7/23/03
Re: strangely problem with a InputStream  
Jul 22, 2004 2:18 AM (reply 1 of 7)



 
dun really understand what you trying to explain here...
however, i have a suggestion for you...
why dun you use DataInputStream and DataOutputStream so that you can send & receive different data type...
it will be much more simple then convert all you byte recieved to a certain data type...

and...
reading the data 1(byte) by 1 is extreamly slow...
try to read using a bigger buffer...
 

Posts:47
Registered: 5/14/03
Re: strangely problem with a InputStream  
Jul 22, 2004 2:33 AM (reply 2 of 7)



 
hm, thx. The second code runs fast (my buffer has length of 1024 bytes) and i don't have a problem with the conversion.
I try to explan it in a nother way:
The problem in the second code is that without the "Thread.sleep(1);" it runs but when I take it out it doesnt.
 

Posts:964
Registered: 7/23/03
Re: strangely problem with a InputStream  
Jul 22, 2004 2:43 AM (reply 3 of 7)



 
but the second one contains some MB of the first one. The second file is named
from some bytes of the contents of the first file.
are you saying that the second file having some data from the 1st file ?
if yes, then it may due to these codes
in.read(buf, 0, buf_length);fw.write(buf, 0, buf_length);


try
int byteReaded = in.read(buf, 0, buf_length);fw.write(buf, 0, byteReaded);
 

Posts:47
Registered: 5/14/03
Re: strangely problem with a InputStream  
Jul 22, 2004 3:45 AM (reply 4 of 7)



 
thx for your answer,
but the second one contains some mega bytes form the data of the first file, although the buffer is 1KB ! I think the InputStream reads the same data two times, so that this data occurs in both, in the first and in the second file.
Isn't it strange that without the "Thread.sleep(1);" it runs as it should, but when I take it out it read the data incorrect ?
 

Posts:47
Registered: 5/14/03
Re: strangely problem with a InputStream  
Jul 22, 2004 10:45 AM (reply 5 of 7)



 
Can nobody help me? I don't know what I can do anything else.

The only problem' is that the second code runs if there is the 'Thread.sleep(1)' and if i take it out it doesn't run.
And using this 'Thread.sleep(1)' is not acceptable, because i don't know how much the dependence of
'computer speed' and 'Data transmission rate' is.

or is that a bug in Suns implementation, so that the InputStream reads the Data not correctly if the read(byte[], int, int) was called in to short time intervals :-/ ?

Stev

an sorry for my bad english
 

Posts:27,518
Registered: 11/3/97
Re: strangely problem with a InputStream  
Jul 22, 2004 2:39 PM (reply 6 of 7)



 
Can nobody help me? I don't know what I can do
anything else.

Say you are transmitting 10,000 bytes for the first file and 20,000 bytes in the second

And your buf_length is 1000.

That would be 10 blocks for the first file (10 x 1000 = 10,000)

Label those blocks 1 through 10.

Now what happens, if the socket (nothing to do with your file) only sends 800 bytes for the 8th block?

Now given that you are completely ignoring how many bytes are really being read, what is going to happen?

As Joey@_@ stated you need to use the length that is returned.

 

Posts:47
Registered: 5/14/03
Re: strangely problem with a InputStream  
Jul 22, 2004 11:07 PM (reply 7 of 7)



 
Thank you so much.
that was the problem. he only read 500 (for example) although the buffer has a size of 1024.
I've changed the code a litle bit ...

long k = 0;
while(k < len){
long len_k = len - k;
int kk = in.read(buf, 0, len_k < (long)buf.length?(int)len_k: buf.length);
fw.write(buf, 0, kk);
k += kk;
}
... and now it reads all bytes correct :-)

 
This topic has 7 replies on 1 page.