Socket.SendPacketsAsync(SocketAsyncEventArgs) Socket.SendPacketsAsync(SocketAsyncEventArgs) Socket.SendPacketsAsync(SocketAsyncEventArgs) Socket.SendPacketsAsync(SocketAsyncEventArgs) Method


将文件集合或者内存中的数据缓冲区以异步方法发送给连接的 Socket 对象。Sends a collection of files or in memory data buffers asynchronously to a connected Socket object.

 bool SendPacketsAsync(System::Net::Sockets::SocketAsyncEventArgs ^ e);
public bool SendPacketsAsync (System.Net.Sockets.SocketAsyncEventArgs e);
member this.SendPacketsAsync : System.Net.Sockets.SocketAsyncEventArgs -> bool
Public Function SendPacketsAsync (e As SocketAsyncEventArgs) As Boolean


SocketAsyncEventArgs SocketAsyncEventArgs SocketAsyncEventArgs SocketAsyncEventArgs

要用于此异步套接字操作的 SocketAsyncEventArgs 对象。The SocketAsyncEventArgs object to use for this asynchronous socket operation.


如果 I/O 操作挂起,则为 truetrue if the I/O operation is pending. 操作完成时,将引发 e 参数的 Completed 事件。The Completed event on the e parameter will be raised upon completion of the operation.

如果 I/O 操作同步完成,则为 falsefalse if the I/O operation completed synchronously. 在这种情况下,将不会引发 e 参数的 Completed 事件,并且可能在方法调用返回后立即检查作为参数传递的 e 对象以检索操作的结果。In this case, The Completed event on the e parameter will not be raised and the e object passed as a parameter may be examined immediately after the method call returns to retrieve the result of the operation.


未找到在 FilePath 属性中指定的文件。The file specified in the FilePath property was not found.

已经在使用 e 参数中指定的 SocketAsyncEventArgs 对象执行套接字操作。A socket operation was already in progress using the SocketAsyncEventArgs object specified in the e parameter.

此方法需要 Windows XP 或更高版本。Windows XP or later is required for this method. 如果 Socket 未连接到远程主机,也会发生此异常。This exception also occurs if the Socket is not connected to a remote host.

使用的是无连接 Socket,并且所发送的文件超过了基础传输的最大数据包大小。A connectionless Socket is being used and the file being sent exceeds the maximum packet size of the underlying transport.


SendPacketsAsync方法用于将集合中的文件或在内存中数据缓冲区发送到远程主机。The SendPacketsAsync method is used to send a collection of files or in memory data buffers to remote host. Socket必须已连接到远程主机。The Socket must already be connected to the remote host.

如果System.Net.Sockets.SendPacketsElement的工作目录中的文件的引用,则可能会将其标识只是该文件的名称; 否则,必须指定完整路径和文件的名称。If a System.Net.Sockets.SendPacketsElement references a file in the working directory, it may be identified with just the name of the file; otherwise, the full path and name of the file must be specified. 支持通配符和 UNC 共享名称。Wildcards and UNC share names are supported. 如果找不到该文件,FileNotFoundException引发。If the file is not found, FileNotFoundException is thrown.

若要在完成时得到通知,必须创建实现事件处理程序的回调方法<SocketAsyncEventArgs > 委托并将附加到回调SocketAsyncEventArgs.Completed事件。To be notified of completion, you must create a callback method that implements the EventHandler<SocketAsyncEventArgs> delegate and attach the callback to the SocketAsyncEventArgs.Completed event.

SocketAsyncEventArgs.SendPacketsFlags属性上的e参数提供了关于文件传输的其他信息的窗口套接字服务提供程序。The SocketAsyncEventArgs.SendPacketsFlags property on the e parameter provides the Window Sockets service provider with additional information about the file transfer. 有关如何使用此参数的详细信息,请参阅TransmitFileOptionsFor more information about how to use this parameter, see TransmitFileOptions.

下列属性和事件上的System.Net.Sockets.SocketAsyncEventArgs成功调用此方法所需的对象:The following properties and events on the System.Net.Sockets.SocketAsyncEventArgs object are required to successfully call this method:

调用方可能会设置SocketAsyncEventArgs.UserToken属性设置为在调用之前所需的任何用户状态对象SendPacketsAsync方法,以便可在回调方法中检索信息。The caller may set the SocketAsyncEventArgs.UserToken property to any user state object desired before calling the SendPacketsAsync method, so that the information will be retrievable in the callback method. 如果回调需要比单个对象的详细信息,可以创建一个小类来保存作为成员的其他所需的状态信息。If the callback needs more information than a single object, a small class can be created to hold the other required state information as members.

此方法使用 Windows Sockets 2 API 中找到的 TransmitPackets 函数。This method uses the TransmitPackets function found in the Windows Sockets 2 API. 有关 TransmitPackets 函数和它的标志的详细信息,请参阅Windows 套接字文档。For more information about the TransmitPackets function and its flags, see the Windows Sockets documentation.

适用于面向连接的协议,尽管SendPacketsAsync方法也适用于无连接协议,前提是先调用BeginConnectConnect,或ConnectAsync方法以建立默认远程主机。Although intended for connection-oriented protocols, the SendPacketsAsync method also works for connectionless protocols, provided that you first call the BeginConnect, Connect, or ConnectAsync method to establish a default remote host. 使用无连接协议时,您还必须确保文件的大小不超过基础服务提供程序的最大数据包大小。With connectionless protocols, you must also be sure that the size of your file does not exceed the maximum packet size of the underlying service provider. 如果是这样,不发送数据报并SendPacketsAsync引发SocketException异常。If it does, the datagram is not sent and SendPacketsAsync throws a SocketException exception.

SendPacketsAsync方法优化根据操作系统使用它。The SendPacketsAsync method is optimized according to the operating system on which it is used. 在 Windows server 版本,SendPacketsAsync针对高性能优化方法。On Windows server editions, the SendPacketsAsync method is optimized for high performance.

Windows 客户端版本上SendPacketsAsync方法适用于最小内存和资源利用率。On Windows client editions, the SendPacketsAsync method is optimized for minimum memory and resource utilization.

利用TransmitFileOptions.UseKernelApc中的标志SocketAsyncEventArgs.SendPacketsFlags属性上的e参数可以提供显著的性能优势。Use of the TransmitFileOptions.UseKernelApc flag in the SocketAsyncEventArgs.SendPacketsFlags property on the e parameter can deliver significant performance benefits. 如果线程启动SendPacketsAsync方法调用的使用进行大量计算,则可以,尽管可能性不大,,Apc 可能无法启动。If the thread initiating the SendPacketsAsync method call is being used for heavy computations, it is possible, though unlikely, that APCs could be prevented from launching. 请注意,内核和用户模式下的 Apc 之间的差异。Note that there is a difference between kernel and user-mode APCs. 内核 Apc 在一个线程处于等待状态时启动。Kernel APCs launch when a thread is in a wait state. 用户模式下 Apc 在一个线程处于可报警等待状态时启动User-mode APCs launch when a thread is in an alertable wait state