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

注解

Close方法关闭远程主机连接和版本的所有托管和非托管资源与关联SocketThe 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没有首先调用Shutdown,可以确保数据的情况下设置发送传出传输排入队列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. 如果您设置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,将启用的属性设置为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

注解

Close方法关闭远程主机连接和版本的所有托管和非托管资源与关联SocketThe 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之前调用CloseFor 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没有首先调用Shutdown,可以确保数据的情况下设置发送传出传输排入队列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. 如果您设置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,将启用的属性设置为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.

另请参阅

适用于