Socket.Disconnect(Boolean) 메서드

정의

소켓 연결을 닫고 소켓을 다시 사용할 수 있도록 합니다.

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

매개 변수

reuseSocket
Boolean

현재 연결을 닫은 다음 이 소켓을 다시 사용할 수 있으면 true이고, 그렇지 않으면 false입니다.

예외

Socket 개체가 닫힌 경우

소켓에 액세스하는 동안 오류가 발생했습니다.

예제

다음 코드 예제에서는 동기 통신을 위한 소켓을 만들고 일부 데이터를 원격 호스트로 보냅니다. 그런 다음, 를 호출하여 송신 및 수신 작업을 중지하고 Disconnect, 를 호출Shutdown하여 소켓 연결을 닫습니다.

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" );
else
      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.
client.Connect(ipEndPoint);

// 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.
client.Shutdown(SocketShutdown.Both);

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

설명

연결 지향 프로토콜을 사용하는 경우 이 메서드를 사용하여 소켓을 닫을 수 있습니다. 이 메서드는 연결을 종료하고 속성을 falseConnected 설정합니다. 그러나 가 이truereuseSocket 소켓을 다시 사용할 수 있습니다.

소켓을 닫기 전에 모든 데이터를 보내고 받도록 하려면 메서드를 호출 Disconnect 하기 전에 를 호출 Shutdown 해야 합니다.

를 먼저 호출하지 않고 를 호출 DisconnectShutdown해야 하는 경우 옵션을 false 로 설정하고 DontLingerSocket 0이 아닌 시간 제한 간격을 지정하여 송신 전송을 위해 대기 중인 데이터가 전송되도록 할 수 있습니다. Disconnect 는 데이터가 전송될 때까지 또는 지정된 제한 시간이 만료될 때까지 차단합니다. 를 로 false 설정하고 DontLinger 시간 제한 간격 Close 을 0으로 지정하면 연결이 해제되고 나가는 큐에 대기된 데이터가 자동으로 삭제됩니다.

참고

를 수신하는 SocketException경우 속성을 사용하여 SocketException.ErrorCode 특정 오류 코드를 가져옵니다. 이 코드를 가져온 후 오류에 대한 자세한 설명 은 Windows 소켓 버전 2 API 오류 코드 설명서를 참조하세요.

참고

애플리케이션에 네트워크 추적을 사용하도록 설정하면 이 멤버에서 추적 정보를 출력합니다. 자세한 내용은 .NET Framework 네트워크 추적을 참조하세요.

적용 대상