Socket.Shutdown(SocketShutdown) Socket.Shutdown(SocketShutdown) Socket.Shutdown(SocketShutdown) Socket.Shutdown(SocketShutdown) Method


禁用某 Socket 上的发送和接收。Disables sends and receives on a Socket.

 void Shutdown(System::Net::Sockets::SocketShutdown how);
public void Shutdown (System.Net.Sockets.SocketShutdown how);
member this.Shutdown : System.Net.Sockets.SocketShutdown -> unit
Public Sub Shutdown (how As SocketShutdown)


SocketShutdown SocketShutdown SocketShutdown SocketShutdown

SocketShutdown 值之一,它指定不再允许执行的操作。One of the SocketShutdown values that specifies the operation that will no longer be allowed.


尝试访问套接字时出错。An error occurred when attempting to access the socket.


下面的代码示例使用ShutdownSocket禁用。The following code example uses Shutdown to disable the Socket.

aSocket->Shutdown( SocketShutdown::Both );
if ( aSocket->Connected )
   Console::WriteLine( "Winsock error: {0}", Convert::ToString(
      System::Runtime::InteropServices::Marshal::GetLastWin32Error() ) );

End Sub 'SocketClose


使用面向Socket连接的时,请始终在关闭Shutdown Socket之前调用方法。When using a connection-oriented Socket, always call the Shutdown method before closing the Socket. 这可确保所有数据在连接的套接字关闭之前都已发送和接收。This ensures that all data is sent and received on the connected socket before it is closed.

调用方法以释放与关联的Socket所有托管资源和非托管资源。 CloseCall the Close method to free all managed and unmanaged resources associated with the Socket. 不要尝试Socket在关闭后使用。Do not attempt to reuse the Socket after closing.

下表显示SocketShutdownhow对参数有效的枚举值。The following table shows the SocketShutdown enumeration values that are valid for the how parameter.

Value 说明Description
发送Send 禁止在此Socket上发送。Disable sending on this Socket.
接收Receive 禁止在此Socket上接收。Disable receiving on this Socket.
双向Both 在此Socket上禁用发送和接收。Disable both sending and receiving on this Socket.

设置how SendSend指定不允许对的后续调用。Setting how to Send specifies that subsequent calls to Send are not allowed. 如果使用的是无连接Socket的, Send则指定将不起作用。If you are using a connectionless Socket, specifying Send will have no effect.

设置how ReceiveReceive指定不允许对的后续调用。Setting how to Receive specifies that subsequent calls to Receive are not allowed. 这在较低的协议层上不起作用。This has no effect on lower protocol layers. 如果使用面向连接的协议,则在调用Shutdown以下任一条件后,将终止连接:If you are using a connection-oriented protocol, the connection is terminated if either of the following conditions exist after a call to Shutdown :

  • 数据位于传入网络缓冲区中等待接收。Data is in the incoming network buffer waiting to be received.

  • 已到达更多数据。More data has arrived.

如果使用的是无连接协议,则接受并排队数据报。If you are using a connectionless protocol, datagrams are accepted and queued. 但是,如果没有可用于附加传入数据报的缓冲区空间,则将丢弃这些数据,并且不会将错误返回给发件人。However, if no buffer space is available for additional incoming datagrams, they will be discarded and no error will be returned to the sender. Shutdown建议在无Socket连接上使用。Using Shutdown on a connectionless Socket is not recommended.

按如上所述how禁用发送和接收的设置。BothSetting how to Both disables both sends and receives as described above.


如果在SocketException Shutdown调用方法时接收到,请使用SocketException.ErrorCode属性获取特定的错误代码。If you receive a SocketException when calling the Shutdown method, use the SocketException.ErrorCode property to obtain the specific error code. 获取此代码后,请参阅Windows 套接字版本 2 API 错误代码文档,以获取有关错误的详细说明。After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.


当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。This member outputs trace information when you enable network tracing in your application. 有关详细信息,请参阅.NET Framework 中的网络跟踪For more information, see Network Tracing in the .NET Framework.