Socket.Disconnect(Boolean) Socket.Disconnect(Boolean) Socket.Disconnect(Boolean) Socket.Disconnect(Boolean) Method


关闭套接字连接并允许重用套接字。Closes the socket connection and allows reuse of the socket.

 void Disconnect(bool reuseSocket);
public void Disconnect (bool reuseSocket);
member this.Disconnect : bool -> unit
Public Sub Disconnect (reuseSocket As Boolean)


Boolean Boolean Boolean Boolean

如果关闭当前连接后可以重用此套接字,则为 true;否则为 falsetrue if this socket can be reused after the current connection is closed; otherwise, false.


此方法需要 Windows 2000 或更低版本,否则将引发异常。This method requires Windows 2000 or earlier, or the exception will be thrown.

尝试访问套接字时出错。An error occurred when attempting to access the socket.


下面的代码示例创建同步通信套接字,并将一些数据发送到远程主机。The following code example creates a socket for synchronous communication and sends some data to a remote host. 然后,它调用Shutdown,以停止发送和接收活动,并Disconnect以关闭套接字连接。It then calls Shutdown, to stop the send and receive activity, and Disconnect, to close the socket connection.

IPHostEntry^ ipHost = Dns::GetHostEntry( Dns::GetHostName() );
IPAddress^ ipAddr = ipHost->AddressList[ 0 ];
IPEndPoint^ ipEndPoint = gcnew IPEndPoint( ipAddr,11000 );
Socket^ client = gcnew Socket( AddressFamily::InterNetwork,SocketType::Stream,ProtocolType::Tcp );

// Connect the socket to the remote end point.
client->Connect( ipEndPoint );

// Send some data to the remote device.
String^ data = "This is a string of data <EOF>";
array<Byte>^buffer = Encoding::ASCII->GetBytes( data );
int bytesTransferred = client->Send( buffer );

// Write to the console the number of bytes transferred.
Console::WriteLine( "{0} bytes were sent.\n", bytesTransferred );

// Release the socket.
client->Shutdown( SocketShutdown::Both );
client->Disconnect( true );
if ( client->Connected )
      Console::WriteLine( "We're still connnected" );
      Console::WriteLine( "We're disconnected" );
IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress  ipAddr = ipHost.AddressList[0];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000);

Socket client = new Socket(AddressFamily.InterNetwork,
    SocketType.Stream, ProtocolType.Tcp);

// Connect the socket to the remote end point.

// Send some data to the remote device.
string data = "This is a string of data <EOF>";
byte[] buffer = Encoding.ASCII.GetBytes(data);

int bytesTransferred =  client.Send(buffer);

// Write to the console the number of bytes transferred.
Console.WriteLine("{0} bytes were sent.\n", bytesTransferred);

// Release the socket.

if (client.Connected) 
    Console.WriteLine("We're still connnected");
    Console.WriteLine("We're disconnected");


如果使用的是面向连接的协议,可以使用此方法以关闭套接字。If you are using a connection-oriented protocol, you can use this method to close the socket. 此方法结束连接和集Connected属性设置为falseThis method ends the connection and sets the Connected property to false. 但是,如果reuseSockettrue,可以重复使用套接字。However, if reuseSocket is true, you can reuse the socket.

若要确保发送和接收套接字关闭之前的所有数据,应调用Shutdown之前调用Disconnect方法。To ensure that all data is sent and received before the socket is closed, you should call Shutdown before calling the Disconnect method.

如果您需要调用Disconnect没有首先调用Shutdown,可以设置DontLingerSocket选项设为false而指定的非零的超时间隔,以确保排队等待发送传出的传输的数据。If you need to call Disconnect without first calling Shutdown, you can set the DontLingerSocket option to false and specify a nonzero time-out interval to ensure that data queued for outgoing transmission is sent. Disconnect 然后阻止,直到发送数据或直到指定的超时时间已到。Disconnect then blocks until the 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.


如果你收到SocketException,使用SocketException.ErrorCode属性来获取特定错误代码。If you receive a SocketException, 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.