Egor
Egor

Reputation: 40193

Transferring a file from one host to another and checking its integrity

I've a university assignment to create a server-client pair, where the client can transfer a file to server. Also there should be an integrity check to see if the file is not corrupt. I've implemented the actual file transfer, using the DigestInputStream and DigestOutputStream classes, that calculate the hash code of the file at transfer time. Now my problem is to send the hash calculated by the client to server, where the server must compare it to the hash calculated by server. I need an idea of some kind of protocol to send the actual file data, its hash code and file name. Thanks in advance.

Upvotes: 1

Views: 678

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533492

You can use DataOutput/InputStream

DataOutput out = ...
String fileName = ...
byte[] bytes = ...
int hash = ...

out.writeUTF(fileName);
out.writeInt(bytes.length);
out.write(bytes);
out.writeInt(hash);
out.flush();

Upvotes: 0

Deco
Deco

Reputation: 3321

It is proabably easiest to implement using the Socket API.

Then you can take the following steps:

  1. Connect to the server from the client;
  2. Have the client send the calculated hash;
  3. Server validates the hash against its calculated one;
  4. Server sends back a "success" message to the client.

There are plenty of tutorials available on Socket and how to setup basic client-server communication.

Upvotes: 2

Related Questions