![](/img/trans.png)
[英]Reading stream over TCP on a SocketChannel with undefined number of Bytes
[英]Missing bytes while transferring file over SocketChannel
我正在使用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------");
}
发件人输出:
bytesTransfered :: 18464
传输的总字节数:6060064进度100%
传输:6060064字节:119 s-> 49.67708595651809 kbytes / s
接收器输出:
bytesTransfered :: 0
传输的总字节数:6059040进度100%
接收器的while循环继续进行,直到我手动停止该过程为止,并且字节传输在接收器端始终返回零,而文件的大小增加到6059040字节。
请谁能告诉我为什么这些字节会丢失。
我们在JDK7u60上看到了相同的东西(以前没有看到 )
我们注意到,如果缓冲区大于200KB,则SocketChannel在接收者有时间读取套接字之前关闭。
到目前为止,我们唯一的建议是暂停一下写入
int bytes = socketChannel.write(currentWriteBuffer);
if (bytes > 200000) {
try {
Thread.sleep(100 * (bytes / 200000));
}
catch (InterruptedException e) {
// ignore
}
}
我尚未调查实际的限制或实际的延迟,但这对我们有用。 请注意,我在研究更好的答案时发现了您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.