TransmitFileOptions 枚举

定义

TransmitFileOptions 枚举定义文件传输请求中使用的值。

此枚举支持其成员值的按位组合。

C#
[System.Flags]
public enum TransmitFileOptions
继承
TransmitFileOptions
属性

字段

名称 说明
Disconnect 1

所有要传输的数据文件完成排队后,启动传输层断开连接。 如果与 ReuseSocket 一起使用,这些标志将在完成文件传输后使套接字返回到断开连接的可重用状态。

ReuseSocket 2

完成请求后,可以重用套接字句柄。 此标志仅在同时指定了 Disconnect 时才有效。 如果与 Disconnect 一起使用,这些标志将在完成文件传输后使套接字返回到断开连接的可重用状态。

UseDefaultWorkerThread 0

使用默认线程处理长文件传输请求。

UseKernelApc 32

使用内核异步过程调用 (APC) 而不是辅助线程来处理长文件传输请求。 长请求定义为要求从文件或缓存中进行多次读取的请求,因此请求取决于文件的大小和发送数据包的指定长度。

UseSystemThread 16

使用系统线程处理长文件传输请求。

WriteBehind 4

立即完成文件传输请求,而不挂起。 如果指定了此标志并且文件传输成功,则表明系统已接受数据,但远端并不一定识别该数据。 请勿将此标志与 DisconnectReuseSocket 标志一起使用。

示例

以下示例演示如何 TransmitFileOptions 在调用 Socket.SendFile中使用 。 文件“test.txt”位于本地计算机的根目录中。 在此示例中,将创建数据的 prebuffer 和 postbuffer,并使用 文件发送到远程主机。 若要使用系统的默认线程, UseDefaultWorkerThread 请指定 。

C#
// Establish the local endpoint for the socket.
IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress  ipAddr = ipHost.AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000);

// Create a TCP socket.
Socket client = new Socket(AddressFamily.InterNetwork,
        SocketType.Stream, ProtocolType.Tcp);

// Connect the socket to the remote endpoint.
client.Connect(ipEndPoint);

// Send file fileName to the remote host with preBuffer and postBuffer data.
// There is a text file test.txt located in the root directory.
string fileName = "C:\\test.txt";

// Create the preBuffer data.
string string1 = String.Format("This is text data that precedes the file.{0}", Environment.NewLine);
byte[] preBuf = Encoding.ASCII.GetBytes(string1);

// Create the postBuffer data.
string string2 = String.Format("This is text data that will follow the file.{0}", Environment.NewLine);
byte[] postBuf = Encoding.ASCII.GetBytes(string2);

//Send file fileName with buffers and default flags to the remote device.
Console.WriteLine("Sending {0} with buffers to the host.{1}", fileName, Environment.NewLine);
client.SendFile(fileName, preBuf, postBuf, TransmitFileOptions.UseDefaultWorkerThread);

// Release the socket.
client.Shutdown(SocketShutdown.Both);
client.Close();

注解

备注

在传输文件后,标志 DisconnectReuseSocket 将套接字返回到断开连接的可重用状态。 不应在请求服务质量 (QOS) 的套接字上使用这些标志,因为服务提供商可能会在文件传输完成之前立即删除与套接字关联的任何服务质量。 启用 QOS 的套接字的最佳方法是在文件传输完成后调用 Socket.Close ,而不是依赖于这些标志。

适用于

产品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1