System.Net.Sockets.Socket.Disconnect C# (CSharp) Method

Disconnect() public method

public Disconnect ( bool reuseSocket ) : void
reuseSocket bool
return void
        public void Disconnect(bool reuseSocket)
        {
            if (NetEventSource.IsEnabled) NetEventSource.Enter(this);
            if (CleanedUp)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            SocketError errorCode = SocketError.Success;

            // This can throw ObjectDisposedException (handle, and retrieving the delegate).
            errorCode = SocketPal.Disconnect(this, _handle, reuseSocket);

            if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"UnsafeNclNativeMethods.OSSOCK.DisConnectEx returns:{errorCode}");

            if (errorCode != SocketError.Success)
            {
                // update our internal state after this socket error and throw
                SocketException socketException = new SocketException((int)errorCode);
                UpdateStatusAfterSocketError(socketException);
                if (NetEventSource.IsEnabled) NetEventSource.Error(this, socketException);
                throw socketException;
            }

            SetToDisconnected();
            _remoteEndPoint = null;

            if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
        }

Usage Example

示例#1
1
 static void Main(string[] args)
 {
     Socket s=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
     IPEndPoint ie=new IPEndPoint(IPAddress.Parse("127.0.0.1"),9000);
     s.Connect(ie);
     Console.WriteLine("Connected to Server.....");
     byte[] data=new byte[1024];
     int k=s.Receive(data);
     Console.WriteLine("Loi chao tu Server:{0}",Encoding.ASCII.GetString(data,0,k));
     while(true)
     {
         Console.WriteLine("Moi nhap du lieu can tinh");
         string st=Console.ReadLine();
         byte[] dl=new byte[1024];
         dl=Encoding.ASCII.GetBytes(st);
         s.Send(dl,dl.Length,SocketFlags.None);
         if(st.ToUpper().Equals("QUIT"))
             break;
         dl=new byte[1024];
         int k1=s.Receive(dl);
         Console.WriteLine("Ket qua tinh tong tu server tra ve:{0}", Encoding.ASCII.GetString(dl, 0, k1));
     }
     s.Disconnect(true);
     s.Close();
 }
All Usage Examples Of System.Net.Sockets.Socket::Disconnect