Sending and receiving files on socket

You need to:

  1. Send the length of the file ahead of the file. You can use DataOutputStream.writeLong() for that, and DataInputStream.readLong() at the receiver.
  2. Read exactly that many bytes from the stream at the receiver:

    while (total < length && (count = in.read(buffer, 0, length-total > buffer.length ? buffer.length : (int)(length-total))) > 0)
    {
        out.write(buffer, 0, count);
        total += count;
    }
    

E&OE

Actually I want to reset socket connection

Actually you don’t want to do any such thing.

Leave a Comment