namespace Client { class ClientTcp : IClient, IDisposable { private TcpClient _tcpClient = new TcpClient(); protected Stream StreamClient { get; private set; } public bool Connect(string adresse, ushort port) { if (StreamClient != null) return false; try { _tcpClient.Connect(adresse, port); } catch (SocketException e) { if (e.SocketErrorCode == SocketError.ConnectionRefused) return false; throw; } StreamClient = _tcpClient.GetStream(); return true; } public void Disconnect() { _tcpClient.Close(); StreamClient?.Dispose(); StreamClient = null; } #region IDisposable Support private bool disposedValue = false; // To detect redundant calls protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { _tcpClient.Close(); // Dispose inaccessible. StreamClient?.Dispose(); } // Free unmanaged resources (unmanaged objects) and override a finalizer below. // Set large fields to null. disposedValue = true; } } // Override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources. // ~AClient() { // // Do not change this code. Put cleanup code in Dispose(bool disposing) above. // Dispose(false); // } // This code added to correctly implement the disposable pattern. public void Dispose() { // Do not change this code. Put cleanup code in Dispose(bool disposing) above. Dispose(true); // Uncomment the following line if the finalizer is overridden above. GC.SuppressFinalize(this); } #endregion } }