System.Net.Sockets.TcpClient.Dispose C# (CSharp) Method

Dispose() protected method

protected Dispose ( bool disposing ) : void
disposing bool
return void
        protected virtual void Dispose(bool disposing)
        {
            if (NetEventSource.IsEnabled) NetEventSource.Enter(this);

            if (_cleanedUp)
            {
                if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
                return;
            }

            if (disposing)
            {
                IDisposable dataStream = _dataStream;
                if (dataStream != null)
                {
                    dataStream.Dispose();
                }
                else
                {
                    // If the NetworkStream wasn't created, the Socket might
                    // still be there and needs to be closed. In the case in which
                    // we are bound to a local IPEndPoint this will remove the
                    // binding and free up the IPEndPoint for later uses.
                    Socket chkClientSocket = _clientSocket;
                    if (chkClientSocket != null)
                    {
                        try
                        {
                            chkClientSocket.InternalShutdown(SocketShutdown.Both);
                        }
                        finally
                        {
                            chkClientSocket.Dispose();
                            _clientSocket = null;
                        }
                    }
                }

                DisposeCore(); // platform-specific disposal work

                GC.SuppressFinalize(this);
            }

            _cleanedUp = true;
            if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
        }

Same methods

TcpClient::Dispose ( ) : void

Usage Example

Ejemplo n.º 1
0
        /// <summary>
        /// Dispose of the TCP client.
        /// </summary>
        /// <param name="disposing">Dispose of resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                try
                {
                    _WriteSemaphore.Wait();
                    _ReadSemaphore.Wait();

                    if (_SslStream != null)
                    {
                        _SslStream.Close();
                    }

                    if (_NetworkStream != null)
                    {
                        _NetworkStream.Close();
                    }

                    if (_TcpClient != null)
                    {
                        _TcpClient.Close();
                        _TcpClient.Dispose();
                        _TcpClient = null;
                    }
                }
                finally
                {
                    _ReadSemaphore.Release();
                    _WriteSemaphore.Release();
                }

                _IsConnected = false;
            }
        }
All Usage Examples Of System.Net.Sockets.TcpClient::Dispose