c# - .NET 等同于 Java FileChannel?

标签 c# tcp nio .net

我想将字节直接从 TCP 套接字流式传输到磁盘上的文件。在 Java 中,可以使用 NIO channel ,特别是 SocketChannelFileChannel。引用 FileChannel#transferFrom(...):

This method is potentially much more efficient than a simple loop that reads from the source channel and writes to this channel. Many operating systems can transfer bytes directly from the source channel into the filesystem cache without actually copying them.

显然,我可以只编写标准的“复制循环”来读取和写入字节,甚至可以利用异步 I/O 来最大程度地减少等待。这是否可以与 Java 正在利用的平台 native 功能相媲美,还是有其他方法?

最佳答案

您可以使用 NetworkStream 读取数据, 然后使用 CopyTo将数据写入 FileStream 的扩展方法。

.NET4 之前的手动方法:How do I save a stream to a file in C#?

对于发送,有一个Socket.SendFile直接发送文件的方法,利用 Win32 TransmitFile方法。不幸的是,没有相应的 Socket.ReceiveFile 方法或 ReceiveFile Win32 API。

关于c# - .NET 等同于 Java FileChannel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11471536/

相关文章:

c# - 将javascript渲染的svg图像作为.png文件保存到本地磁盘

c# - SWIG 引用输入,自定义类输出

python - 以编程方式发现公共(public) IP

Jenkins 脚本化管道 : How to apply @NonCPS annotation in this specific case

java - 用java获取文件属主元数据信息

c# - 当我重新运行应用程序时,图片框不显示最后上传的图像

c# - 将项目添加到字典返回 System.IndexOutOfRangeException : Index was outside the bounds of the array

java - Java 套接字延迟 30 秒?

tcp - BitTorrent 将数据包作为 UDP 而不是 TCP 发送

Java NIO 对象到日期的转换