public async Task RunTest() { using (var tcp = new TcpClient()) { _log.WriteLine("[Client] Connecting to {0}:{1}", _server, _port); await tcp.ConnectAsync(_server, _port); using (var tls = new SslStream(tcp.GetStream(), false, CertificateValidation)) { _log.WriteLine("[Client] Connected. Authenticating..."); await tls.AuthenticateAsClientAsync(_server, null, System.Security.Authentication.SslProtocols.Tls, false); string requestString = "GET / HTTP/1.0\r\nHost: servername.test.contoso.com\r\nUser-Agent: Testing application\r\n\r\n"; byte[] requestBuffer = Encoding.UTF8.GetBytes(requestString); _log.WriteLine("[Client] Sending request ({0} Bytes)", requestBuffer.Length); await tls.WriteAsync(requestBuffer, 0, requestBuffer.Length); _log.WriteLine("[Client] Waiting for reply..."); int bytesRead = 0; int chunks = 0; do { byte[] responseBuffer = new byte[2048]; bytesRead = await tls.ReadAsync(responseBuffer, 0, responseBuffer.Length); if (bytesRead > 0) { string responseString = Encoding.UTF8.GetString(responseBuffer, 0, bytesRead); _log.WriteLine("[Client {0}: {2} Bytes] Response: <<<<<{1}>>>>>", chunks, responseString, bytesRead); } if (bytesRead == 1 && chunks == 0) { AuxRecordDetected = true; } chunks++; }while (bytesRead > 0); } } }
/// <summary> /// Starts the server. /// </summary> /// <returns>The local port that the server is bound to.</returns> public void StartServer() { if (_listener != null) { throw new InvalidOperationException("Cannot restart server."); } IPAddress address = IPAddress.Loopback; _listener = new TcpListener(address, _port); _listener.Start(1); _log.WriteLine("[Server] waiting for connections ({0}:{1})", address, _port); _port = ((IPEndPoint)_listener.LocalEndpoint).Port; }
protected override void Dispose(bool disposing) { _log.WriteLine(this.GetHashCode() + " Dispose (_numConnectedSockets={0})", _numConnectedSockets); if (disposing && (_listenSocket != null)) { lock (_listenSocketLock) { if (_listenSocket != null) { _listenSocket.Dispose(); _listenSocket = null; } } } }