Note that there are some explanatory texts on larger screens.

plurals
  1. POSocket: an existing connection was forcibly closed by remote host
    text
    copied!<p>I am trying to implement HTTP+HTTPS+POP3+IMAP proxy server. As of now i have implemented HTTP and HTTPS proxy. But while trying to use HTTPS proxy, i Have the following exception: <code>an existing connection was forcibly closed by remote host</code></p> <p>Here's my code that handles socket from TCP listener:</p> <pre><code> static void PerformConnection(object o) { try { Socket receiver = o as Socket; if (receiver != null) { NetworkStream ns = new NetworkStream(socket: receiver); byte[] buffer = new byte[450000]; ns.Read(buffer, 0, Convert.ToInt32(450000)); string data = Encoding.UTF8.GetString(buffer); string line = data.Replace("\r\n", "\n").Split(new string[] {"\n"}, StringSplitOptions.None)[0]; if (line != "" &amp;&amp; line != null) { Uri uri = new Uri("http://someforbiddenhost.org"); uri = new Uri(line.Split(new string[] {" "}, StringSplitOptions.None)[1]); if (uri.Host.Contains("shelesta.org")) { string Html = "&lt;html&gt;&lt;body&gt;&lt;h1&gt;It works!&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;"; string response = "HTTP/1.1 200 OK\nContent-type: text/html\nContent-Length:" + Html.Length.ToString() + "\n\n" + Html; receiver.Send(Encoding.UTF8.GetBytes(response)); } else { if (line.StartsWith("CONNECT")) { string host = uri.ToString().Substring(0, uri.ToString().IndexOf(':')); TcpClient clDestination = new TcpClient(); clDestination.Connect(host, 443); SslStream sslStream = new SslStream(clDestination.GetStream()); sslStream.AuthenticateAsClient(host); byte[] request = Encoding.UTF8.GetBytes(String.Format("GET https://{0}:443/ HTTP/1.1\r\nHost: {0}\r\n\r\n", host)); sslStream.Write(request, 0, request.Length); sslStream.Flush(); int bytes = 10; receiver.ReceiveTimeout = 6000000; do { bytes = sslStream.Read(buffer, 0, buffer.Length); Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes)); receiver.Send(buffer,buffer.Length,SocketFlags.Partial);// HERE EXCEPTION RAISES } while (bytes != 0); } else { IPHostEntry rh = Dns.GetHostEntry(uri.Host); Socket webserver = new Socket(rh.AddressList[0].AddressFamily, SocketType.Stream, ProtocolType.IP); webserver.Connect(new IPEndPoint(rh.AddressList[0], 80)); byte[] databytes = Encoding.ASCII.GetBytes(data); webserver.Send(databytes, databytes.Length, SocketFlags.None); byte[] ReceiverBuffer = new byte[2000048]; webserver.Receive(ReceiverBuffer); receiver.Send(ReceiverBuffer); } } } } } catch { } } </code></pre> <p>And sorry about code formatting, I can't see SO WYSIWYG editor, just message: Stack Overflow requires external JavaScript from another domain, which is blocked or failed to load.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload