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的情况下调用DontLinger Socket ,可以将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.