Sending and receiving files on socket
Solution 1:
You need to:
- Send the length of the file ahead of the file. You can use
DataOutputStream.writeLong()
for that, andDataInputStream.readLong()
at the receiver. -
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.