transferring file through socket

  • Thread starter polaris venus via JavaKB.com
  • Start date
P

polaris venus via JavaKB.com

when i use this code to transfer a file from the server
to the client, it is appended by some text. why?
another question how can i make resumable transfer?

Client:
Code:
out = new DataOutputStream(s.getOutputStream());
in = new DataInputStream(s.getInputStream());

fos = new FileOutputStream(file);
bos = new DataOutputStream(fos);

byte [] b = new byte [1024];
int len = 1024;
long c=0;
while((c=in.read(b,0,len))!=-1)
{
bos.write(b,0,len);
bos.flush();
}

Server:

Code:
in = new DataInputStream(connection.getInputStream());
out = new DataOutputStream(connection.getOutputStream());

fis = new FileInputStream(file.getAbsolutePath());
dis = new DataInputStream(fis);

byte [] b = new byte [1024];
int len = 1024;
int i;

while((i=dis.read(b,0,len))!=-1)
{
out.write(b,0,len);
out.flush();
}
 
M

Matt Humphrey

polaris venus via JavaKB.com said:
when i use this code to transfer a file from the server
to the client, it is appended by some text. why?
another question how can i make resumable transfer?

Client:
Code:
out = new DataOutputStream(s.getOutputStream());
in = new DataInputStream(s.getInputStream());

fos = new FileOutputStream(file);
bos = new DataOutputStream(fos);

byte [] b = new byte [1024];
int len = 1024;
long c=0;
while((c=in.read(b,0,len))!=-1)[/QUOTE]

Read is not guaranteed to fill the buffer, especially on a socket call.  The
corresponding write should only examine the first c bytes.
[QUOTE]
{
bos.write(b,0,len);
bos.flush();
}

Server:

Code:
in = new DataInputStream(connection.getInputStream());
out = new DataOutputStream(connection.getOutputStream());

fis = new FileInputStream(file.getAbsolutePath());
dis = new DataInputStream(fis);

byte [] b = new byte [1024];
int len = 1024;
int i;

while((i=dis.read(b,0,len))!=-1)[/QUOTE]

Same problem here--read isn't guaranteed to fill the buffer, although when
reading a file it usually does. However, if the file length isn't an exact
multiple of 1024 (your block size) you will get trash appended to the file.
Only write as many bytes as you read.
[QUOTE]
{
out.write(b,0,len);
out.flush();
}

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
T

Thomas Weidenfeller

Matt said:
when i use this code to transfer a file from the server
to the client, it is appended by some text. why?
another question how can i make resumable transfer?

Client:
Code:
out = new DataOutputStream(s.getOutputStream());
in = new DataInputStream(s.getInputStream());

fos = new FileOutputStream(file);
bos = new DataOutputStream(fos);

byte [] b = new byte [1024];
int len = 1024;
long c=0;
while((c=in.read(b,0,len))!=-1)[/QUOTE]


Read is not guaranteed to fill the buffer, especially on a socket call.  The
corresponding write should only examine the first c bytes.[/QUOTE]

In addition, the DataInputStream and DataOutputStream are not needed.
Data[Input|Output]Stream is for writing Java primitives, not for writing
a file.

/Thomas
 
P

polaris venus via JavaKB.com

Thanks Humphrey you open my eyes to a problem
upon your reply i modified the code simply by
making the number of bytes read equal to
the number of bytes written in the client and
the server.
thank you Weidenfeller it works properly now.
I use Data[Input][Outpu]Stream because i think
that File[Input][Outpu]Stream write and read
methods are deprecated.

but about the another part of question how can
i resume the download after disconnecting the
connection
 
G

Gordon Beaton

but about the another part of question how can
i resume the download after disconnecting the
connection.

The client can determine the size of the existing file before
reconnecting, then ask the server to start sending data from that
offset in the file.

The server should open a RandomAccessFile and seek() to the requested
starting position, then send bytes until EOF is reached.

The client should append the received data to the existing file by
opening the file with the appropriate FileOutputStream constructor.

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top