Socket.Close 方法

定义

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

重载

Close()

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

Close(Int32)

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

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.

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

注解

Close 方法会关闭远程主机连接,并释放与 Socket关联的所有托管资源和非托管资源。The 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.

对于面向连接的协议,建议你在调用 Close 方法之前调用 ShutdownFor 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.

如果需要在不首先调用 Shutdown的情况下调用 Close,则可以通过将 DontLingerSocket 选项设置为 false 并指定非零超时间隔来确保将排队等待传出传输的数据发送。If 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. 如果将 DontLinger 设置为 false 并指定零超时间隔,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. 使用此 LingerOptionDontLinger 套接字选项来调用 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)

关闭 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

等待最多 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.

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

注解

Close 方法会关闭远程主机连接,并释放与 Socket关联的所有托管资源和非托管资源。The 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.

对于面向连接的协议,建议你在调用 Close之前调用 ShutdownFor 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.

如果需要在不首先调用 Shutdown的情况下调用 Close,则可以通过将 DontLinger 选项设置为 false 并指定一个非零超时间隔来确保将排队等待传出传输的数据发送。If 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. 如果将 DontLinger 设置为 false 并指定零超时间隔,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. 使用此 LingerOptionDontLinger 套接字选项来调用 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.

另请参阅

适用于