简体   繁体   中英

Sending large files over sockets on separate computers

I have created a basic client-server program that transfers file from server to the client. Since the file is about 500MB, the server transfers the file as chunks of bytes to the client over the DataOutputStream object. While this logic works okay when both client and server are running on the same computer, it doesn't work when the two programs are running on separate computers(Both computers are on the same network and I have disabled the firewall for both)

When running on separate computers the problem is that a few bytes get transferred

Server Logic:

byte byteArr[] = new byte[1024];
while((c=fileInputStream.read(byteArr, 0, 1024) != -1))
{
    dataOutputStream.writeBoolean(true);
    dataOutputStream.flush();
    dataOutputStream.write(byteArr, 0, 1024);
    dataOutputStream.flush();
}
 /*When running on different computers, after a few hundred iterations 
   it just stops looping the following lines are never executed*/
dataOutputStream.writeBoolean(false);
System.out.println("Transfer complete");

Client Logic

byte byteArr[] = new byte[1024];
while(dataInputStream.readBoolean())
{
     dataInputStream.read(byteArr, 0, 1024);
     fileOutputStream.write(byteArr, 0, 1024);
}

A read(buf, 0, 1024) call is not guaranteed to read exactly 1024 bytes. This causes bugs in both pieces of code:

  • the server incorrectly assumes that each chunk read from the file is always exactly 1024 bytes long and sends the whole buffer to the client.
  • the client might not read the whole chunk in a single iteration. It will then treat the first byte of the remainder as a boolean and get out of sync with the server.

To resolve this, you could:

  • send the file size (if known) before sending the file, then just keep reading until you've read that many bytes.

  • or send c (the chunk size) instead of a single boolean, then use dataInputStream.readFully() to make sure that many bytes will be read.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM