Socket.Close Socket.Close Socket.Close Socket.Close Method

定义

关闭 Socket 连接并释放所有关联的资源。Closes the Socket connection and releases all associated resources.

重载

Close() Close() Close() Close()

关闭 Socket 连接并释放所有关联的资源。Closes the Socket connection and releases all associated resources.

Close(Int32) Close(Int32) Close(Int32) Close(Int32)

关闭 Socket 连接并释放所有与指定超时关联的资源,以允许要发送的数据排队。Closes the Socket connection and releases all associated resources with a specified timeout to allow queued data to be sent.

Close() Close() Close() Close()

关闭 Socket 连接并释放所有关联的资源。Closes the Socket connection and releases all associated resources.

public:
 void Close();
public void Close ();
member this.Close : unit -> unit
Public Sub Close ()

示例

下面的代码示例关闭SocketThe following code example closes a Socket.

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

End Sub 'SocketClose

注解

方法会关闭远程主机连接, 并释放与关联的Socket所有托管资源和非托管资源。 CloseThe Close method closes the remote host connection and releases all managed and unmanaged resources associated with the Socket. 关闭时, Connected属性将设置为falseUpon closing, the Connected property is set to false.

对于面向连接的协议, 建议你在Shutdown Close调用方法之前调用。For connection-oriented protocols, it is recommended that you call Shutdown before calling the Close method. 这可确保所有数据在连接的套接字关闭之前都已发送和接收。This ensures that all data is sent and received on the connected socket before it is closed.

Close如果需要DontLinger Socket false先调用而不调用,则可以通过将选项设置为并指定非零超时间隔来确保将排队等待传出传输的数据发送。ShutdownIf you need to call Close without first calling Shutdown, you can ensure that data queued for outgoing transmission will be sent by setting the DontLingerSocket option to false and specifying a non-zero time-out interval. Close然后将被阻止, 直到发送此数据或指定的超时过期。Close will then block until this data is sent or until the specified time-out expires. 如果将设置DontLingerfalse并指定零超时间隔, Close则会释放连接并自动丢弃传出的排队数据。If you set DontLinger to false and specify a zero time-out interval, Close releases the connection and automatically discards outgoing queued data.

备注

若要将DontLinger套接字选项false设置为, LingerOption请创建, 将 enabled 属性true设置为, 并LingerTime将属性设置为所需的超时时间。To set the DontLinger socket option to false, create a LingerOption, set the enabled property to true, and set the LingerTime property to the desired time out period. 将此LingerOption DontLinger与套接字选项一起使用SetSocketOption来调用方法。Use this LingerOption along with the DontLinger socket option to call the SetSocketOption method.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。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.

另请参阅

Close(Int32) Close(Int32) Close(Int32) Close(Int32)

关闭 Socket 连接并释放所有与指定超时关联的资源,以允许要发送的数据排队。Closes the Socket connection and releases all associated resources with a specified timeout to allow queued data to be sent.

public:
 void Close(int timeout);
public void Close (int timeout);
member this.Close : int -> unit
Public Sub Close (timeout As Integer)

参数

timeout
Int32 Int32 Int32 Int32

等待最多 timeout 秒钟后,即可发送任何剩余数据,然后关闭套接字。Wait up to timeout seconds to send any remaining data, then close the socket.

示例

下面的代码示例演示如何关闭SocketThe following code example demonstrates how to close a Socket.

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

End Sub 'SocketClose

注解

方法会关闭远程主机连接, 并释放与关联的Socket所有托管资源和非托管资源。 CloseThe Close method closes the remote host connection and releases all managed and unmanaged resources associated with the Socket. 关闭时, Connected属性将设置为falseUpon closing, the Connected property is set to false.

对于面向连接的协议, 建议你在调用Shutdown Close之前调用。For connection-oriented protocols, it is recommended that you call Shutdown before calling Close. 这可确保所有数据在连接的套接字关闭之前都已发送和接收。This ensures that all data is sent and received on the connected socket before it is closed.

如果需要Close DontLinger先调用而不调用false ,则可以通过将选项设置为并指定非零超时间隔来确保将排队等待传出传输的数据发送。ShutdownIf you need to call Close without first calling Shutdown, you can ensure that data queued for outgoing transmission will be sent by setting the DontLinger option to false and specifying a non-zero time-out interval. Close然后将被阻止, 直到发送此数据或指定的超时过期。Close will then block until this data is sent or until the specified time-out expires. 如果将设置DontLingerfalse并指定零超时间隔, Close则会释放连接并自动丢弃传出的排队数据。If you set DontLinger to false and specify a zero time-out interval, Close releases the connection and automatically discards outgoing queued data.

备注

若要将DontLinger套接字选项false设置为, LingerOption请创建, 将 enabled 属性true设置为, 并LingerTime将属性设置为所需的超时期限。To set the DontLinger socket option to false, create a LingerOption, set the enabled property to true, and set the LingerTime property to the desired time-out period. 将此LingerOption DontLinger与套接字选项一起使用SetSocketOption来调用方法。Use this LingerOption along with the DontLinger socket option to call the SetSocketOption method.

备注

当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。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.

另请参阅

适用于