Socket.Shutdown(SocketShutdown) 方法

定义

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

public:
 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)

参数

how
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.

Socket 已关闭。The Socket has been closed.

示例

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

try
{
   aSocket->Shutdown(SocketShutdown::Both);
   aSocket->Close();
}
catch (...)
{
   aSocket->Close();
   throw;
}

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

End Sub

注解

使用面向连接的 Socket时,请在关闭 Socket之前始终调用 Shutdown 方法。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.

调用 Close 方法,释放与 Socket关联的所有托管资源和非托管资源。Call the Close method to free all managed and unmanaged resources associated with the Socket. 请勿尝试在关闭后重用 SocketDo not attempt to reuse the Socket after closing.

下表显示了对 how 参数有效的 SocketShutdown 枚举值。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 设置为 Send 指定不允许对 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.

how 设置为 Receive 指定不允许对 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. 不建议对无连接 Socket 使用 ShutdownUsing Shutdown on a connectionless Socket is not recommended.

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

备注

如果在调用 Shutdown 方法时接收到 SocketException,请使用 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.

适用于

另请参阅