public void Connect(string hostname, int port, bool ssl) { try { Host = hostname; Port = port; Ssl = ssl; var protocol = ssl ? SecureProtocol.Tls1 | SecureProtocol.Ssl3 : SecureProtocol.None; SecurityOptions options = new SecurityOptions(protocol); options.Certificate = null; options.Entity = ConnectionEnd.Client; options.CommonName = hostname; options.VerificationType = CredentialVerification.Auto; options.Flags = SecurityFlags.Default; options.AllowedAlgorithms = SslAlgorithms.SECURE_CIPHERS; //_Connection = new TcpClient(hostname, port); _Connection = new SecureTcpClient(hostname, port, options); _Stream = _Connection.GetStream(); _Reader = new StreamReader(_Stream, System.Text.Encoding.Default); string info = _Reader.ReadLine(); OnConnected(info); IsConnected = true; Host = hostname; } catch (Exception) { IsConnected = false; throw; } }
private void ListenForMessages() { while (m_listenerActive) { // receive messages SecureTcpClient client = null; try { client = m_listener.AcceptTcpClient(); if (client != null) // everything ok // disable Nagle algorithm, to reduce delay { client.NoDelay = true; // now process the message of this client SslServerTransport transport = new SslServerTransport(client); m_clientAcceptCallback(transport); } else { Trace.WriteLine("acceptTcpClient hasn't worked"); } } catch (Exception e) { Debug.WriteLine("Exception in server listener thread: " + e); if (client != null) { client.Close(); } } } }
private void ConnectClient(SecureProtocol protocol) { lock (this) { if (connected) { throw new Exception("Connection with IRC server already opened."); } Debug.WriteLineIf(Rfc2812Util.IrcTrace.TraceInfo, "[" + Thread.CurrentThread.Name + "] Connection::Connect()"); SecurityOptions options = new SecurityOptions(protocol); options.Certificate = null; options.Entity = ConnectionEnd.Client; options.VerificationType = CredentialVerification.None; options.Flags = SecurityFlags.Default; options.AllowedAlgorithms = SslAlgorithms.SECURE_CIPHERS; client = new SecureTcpClient(options); client.Connect(connectionArgs.Hostname, connectionArgs.Port); connected = true; writer = new StreamWriter(client.GetStream(), TextEncoding); writer.AutoFlush = true; reader = new StreamReader(client.GetStream(), TextEncoding); socketListenThread = new Thread(new ThreadStart(ReceiveIRCMessages)); socketListenThread.Name = Name; socketListenThread.Start(); sender.RegisterConnection(connectionArgs); } }
public void Dispose() { try { OnDispose(); } catch (Exception) { } Disconnect(); IsDisposed = true; _Stream = null; _Reader = null; _Connection = null; }
/// <summary> /// Connect to the IRC server and start listening for messages /// on a new thread. /// </summary> /// <exception cref="SocketException">If a connection cannot be established with the IRC server</exception> public void Connect() { lock (this) { if (connected) { throw new InvalidOperationException("Connection with IRC server already opened."); } client = new TcpClient(); client.Connect(connectionArgs.Hostname, connectionArgs.Port); connected = true; writer = new StreamWriter(client.GetStream(), TextEncoding); writer.AutoFlush = true; reader = new StreamReader(client.GetStream(), TextEncoding); socketListenThread = new Thread(new ThreadStart(ReceiveIRCMessages)); socketListenThread.Name = Name; socketListenThread.Start(); sender.RegisterConnection(connectionArgs); } }
/// <summary> /// Connect to the IRC server and start listening for messages /// on a new thread. /// </summary> /// <exception cref="SocketException">If a connection cannot be established with the IRC server</exception> public void Connect() { lock (this) { if (connected) { throw new Exception("Connection with IRC server already opened."); } Debug.WriteLineIf(Rfc2812Util.IrcTrace.TraceInfo, "[" + Thread.CurrentThread.Name + "] Connection::Connect()"); client = new TcpClient(); client.Connect(connectionArgs.Hostname, connectionArgs.Port); connected = true; writer = new StreamWriter(client.GetStream(), TextEncoding); writer.AutoFlush = true; reader = new StreamReader(client.GetStream(), TextEncoding); socketListenThread = new Thread(new ThreadStart(ReceiveIRCMessages)); socketListenThread.Name = Name; socketListenThread.Start(); sender.RegisterConnection(connectionArgs); } }
/// <summary><see cref="Ch.Elca.Iiop.ITranport.CloseConnection/></summary> public void CloseConnection() { try { if (m_socket != null) { m_socket.Close(); } } catch { // ignore } m_socket = null; try { if (m_stream != null) { m_stream.Close(); // close the stream and the socket. } } catch { // ignore } m_stream = null; }
static void Main(string[] args) { SecureTcpServer server = null; SecureTcpClient client = null; try { int port = 443; string certPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\server.p12"; RemoteCertificateValidationCallback certValidationCallback = null; certValidationCallback = new RemoteCertificateValidationCallback(IgnoreCertificateErrorsCallback); Console.WriteLine("Loading Cert From: " + certPath); X509Certificate2 serverCert = new X509Certificate2(certPath, "password"); server = new SecureTcpServer(port, serverCert, new SecureConnectionResultsCallback(OnServerConnectionAvailable)); server.StartListening(); //client = new SecureTcpClient(new SecureConnectionResultsCallback(OnClientConnectionAvailable), certValidationCallback); //client.StartConnecting("localhost", new IPEndPoint(IPAddress.Loopback, port)); } catch (Exception ex) { Console.WriteLine(ex); } //sleep to avoid printing this text until after the callbacks have been invoked. Thread.Sleep(4000); //Console.WriteLine("Press any key to continue..."); Console.ReadKey(); if (server != null) { server.Dispose(); } if (client != null) { client.Dispose(); } }
private void InitClient(XElement securityXml, bool isSecurityEnabled, string host, int port) { TcpClientCom client; if (isSecurityEnabled) { var secureClient = new SecureTcpClient(); secureClient.ServerName = securityXml.GetConfigParameterValue <string>(ConfigParamServerName); secureClient.ProvideClientCertificate = securityXml.GetConfigParameterValueOrDefault <bool>(false, ConfigParamProvideClientCertificate); if (secureClient.ProvideClientCertificate) { secureClient.ClientCertificateName = securityXml.GetConfigParameterValue <string>(ConfigParamClientCertificateName); } client = secureClient; } else { client = new TcpClientCom(); } InitClientInternal(host, port, client); }
/// <summary><see cref="Ch.Elca.Iiop.IClientTranport.OpenConnection/></summary> public void OpenConnection() { if (IsConnectionOpen()) { return; // already open } m_socket = new SecureTcpClient(m_options); if (m_targetHostIp != null) { m_socket.Connect(m_targetHostIp, m_port); } else if (m_targetHost != null) { m_socket.Connect(m_targetHost, m_port); } else { throw new INTERNAL(547, CompletionStatus.Completed_No); } m_socket.NoDelay = true; // send immediately; (TODO: what is better here?) m_socket.ReceiveTimeout = m_receiveTimeOut; m_socket.SendTimeout = m_sendTimeOut; m_stream = m_socket.GetStream(); }
public void InitClient(string host, int port, SecureTcpClient secureTcpClient) { InitClientInternal(host, port, secureTcpClient); }
private SecureTcpClient CreateConnection() { var connection = new SecureTcpClient(serverAddress, serverPort); return(connection); }
private static SecureTcpClient CreateConnection() { var connection = new SecureTcpClient(ServerAddress, ServerPort); return(connection); }
public SslServerTransport(SecureTcpClient theClient) { m_socket = theClient; m_stream = m_socket.GetStream(); }