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.


下面的代码示例使用Shutdown若要禁用SocketThe 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方法关闭之前SocketWhen 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.

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

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

Value 描述Description
发送Send 禁止对此发送SocketDisable sending on this Socket.
接收Receive 禁用对此接收SocketDisable receiving on this Socket.
双向Both 禁用发送和接收对此SocketDisable both sending and receiving on this Socket.

设置howSend指定后续调用Send不允许。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.

设置howReceive指定后续调用Receive不允许。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.

设置howBoth同时禁用发送和接收上文所述。Setting 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.