我正在使用 nio ServerSocketChannel 和 SocketChannel 在客户端和服务器之间传输大文件。问题是当我将大小为 6060064 字节的文件从发送方传输到接收方时,接收方仅收到 6059040 字节丢失了一些字节。随着文件大小变大丢失字节的差异差距增加。我找不到这些字节丢失的原因。
发件人代码:
public boolean send(SelectionKey selectionKey) {
File file = new File("/media/data1/sample.mp4");
try {
SocketChannel socketChannel = (SocketChannel) selectionKey
.channel();
FileInputStream fileInputStream = new FileInputStream(file);
System.out.println("File Length :: " + file.length());
System.out.println("File lastModified :: " + file.lastModified());
FileChannel fileChannel = fileInputStream.getChannel();
transfer(fileChannel, socketChannel, file.length(), 1024 * 50);
fileInputStream.close();
System.out.println("File Send Completely Done 100%....");
System.out.println("Closing Connection 100%....");
return true;
} catch (Exception e) {
System.out.println(e);
System.out.println("Connection Failed. Connectiontimeout.");
return false;
}
}
public static void transfer(FileChannel fileChannel,
SocketChannel socketChannel, long lengthInBytes,
long chunckSizeInBytes)
throws IOException {
long overallBytesTransfered = 0L;
long time = -System.currentTimeMillis();
while (overallBytesTransfered < lengthInBytes) {
long bytesTransfered = 0L;
bytesTransfered = fileChannel.transferTo(
overallBytesTransfered,
Math.min(chunckSizeInBytes, lengthInBytes
- overallBytesTransfered), socketChannel);
System.out.println("bytesTransfered :: " + bytesTransfered);
overallBytesTransfered += bytesTransfered;
System.out.printf(
"overall bytes transfered: %s progress %s%%\n",
overallBytesTransfered, Math.round(overallBytesTransfered / ((double) lengthInBytes) * 100.0));
}
time += System.currentTimeMillis();
System.out.printf("Transfered: %s bytes in: %s s -> %s kbytes/s\n",
overallBytesTransfered, time / 1000,
(overallBytesTransfered / 1024.0) / (time / 1000.0));
System.out.println("------- File transfer completed ------");
}
收货人代码:
public boolean recieve(SelectionKey key) {
SocketChannel socketChannel = null;
Socket socket = null;
try {
socketChannel = (SocketChannel) key.channel();
socket = socketChannel.socket();
System.out.println(socketChannel.getRemoteAddress());
// Save file destination
String FileKey = "/media/data1/Test/sample1.mp4";
// File size
long sizeInBytes = 6060064;
long timeStap = 1402301850000l;
File file = new File(FileKey);
FileOutputStream fileOutputStream = new FileOutputStream(file);
FileChannel fileChannel = fileOutputStream.getChannel();
transfer(fileChannel, socketChannel, sizeInBytes, 1024 * 100);
fileOutputStream.close();
file.setLastModified(timeStap);
socket.close();
return true;
} catch (Exception e) {
System.out.println("Connection Failed. Connectiontimeout.");
e.printStackTrace();
return false;
}
}
public static void transfer(FileChannel fileChannel,
SocketChannel socketChannel, long lengthInBytes, long chunckSizeInBytes)
throws IOException {
long overallBytesTransfered = 0L;
long time = -System.currentTimeMillis();
while (overallBytesTransfered < lengthInBytes) {
long bytesTransfered = 0L;
bytesTransfered = fileChannel.transferFrom(
socketChannel,
overallBytesTransfered,
Math.min(chunckSizeInBytes, lengthInBytes
- overallBytesTransfered));
System.out.println("bytesTransfered :: " + bytesTransfered);
overallBytesTransfered += bytesTransfered;
System.out.printf(
"overall bytes transfered: %s progress %s%%\n", overallBytesTransfered,
Math.round(overallBytesTransfered / ((double) lengthInBytes) * 100.0));
}
time += System.currentTimeMillis();
System.out.printf("Transfered: %s bytes in: %s s -> %s kbytes/s\n",
overallBytesTransfered, time / 1000,
(overallBytesTransfered / 1024.0) / (time / 1000.0));
System.out.println("-------Dateiübertragung fertig------");
}
发件人的输出:
字节转移 :: 18464
传输的总字节数:6060064 进度 100%
传输:6060064 字节在:119 s -> 49.67708595651809 kbytes/s
接收器输出:
字节传输 :: 0
传输的总字节数:6059040 进度 100%
接收器的 while 循环一直持续到我手动停止进程,并且bytesTransfered总是在接收器端返回零,而文件的大小增加到 6059040 字节。
请任何人都可以告诉为什么这些字节丢失。