1

我正在使用套接字在 .NET 中的代理服务器上工作,并且我发现了对代码执行非常感兴趣的时刻:

  1. 当我逐步调试代码时 - 代理服务器工作正常
  2. 当我在没有调试的情况下启动代理服务器时 - 我在浏览器中出现错误。

我真的不知道为什么它像我上面描述的那样工作。也许,有人已经解决了这个问题?

这是我的代理服务器的代码:

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace Statistiks.Lib
{
    internal class NetworkMonitor
    {
        private readonly IList<string> _denied;
        private readonly Socket _srvSocket;
        private readonly Thread _srvThread;

        public NetworkMonitor(int port, IList<string> deniedList)
        {
            _denied = deniedList;
            _srvSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
                                    ProtocolType.Tcp);
            _srvSocket.Bind(new IPEndPoint(IPAddress.Loopback, port));
            _srvThread = new Thread(ProxyServerThread);
            _srvThread.Start(_srvSocket);
        }

        private void ProxyServerThread(object obj)
        {
            var srv = obj as Socket;
            srv.Listen(1024);
            while (true)
            {
                Socket clientSocket = srv.Accept();
                ThreadPool.QueueUserWorkItem(cSocket =>
                    {
                        var client = cSocket as Socket;
                        if (client.Available <= 0)
                        {
                            client.Shutdown(SocketShutdown.Both);
                            client.Close();
                            return;
                        }
                        var recieveBuf = new byte[client.Available];
                        client.Receive(recieveBuf, SocketFlags.None);
                        var ni = new NetworkInfo();
                        try
                        {
                            ni.Url = new Uri(Encoding.ASCII.GetString(recieveBuf)
                                                     .Split(new[] {"\r\n"}, StringSplitOptions.RemoveEmptyEntries)[0]
                                                 .Split(' ')[1]);
                        }
                        catch (ArgumentOutOfRangeException)
                        {
                            client.Shutdown(SocketShutdown.Both);
                            client.Close();
                            return;
                        }
                        var forwardedSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
                                                         ProtocolType.Tcp);
                        forwardedSocket.Connect(Dns.GetHostAddresses(ni.Url.Host)[0], ni.Url.Port);
                        forwardedSocket.Send(recieveBuf, SocketFlags.None);
                        recieveBuf = new byte[forwardedSocket.Available];
                        forwardedSocket.Receive(recieveBuf, SocketFlags.None);
                        client.Send(recieveBuf, SocketFlags.None);
                        forwardedSocket.Shutdown(SocketShutdown.Both);
                        forwardedSocket.Disconnect(false);
                        client.Shutdown(SocketShutdown.Both);
                        client.Disconnect(false);
                        forwardedSocket.Close();
                        client.Close();
                    }, clientSocket);
            }
        }
    }

    internal struct NetworkInfo
    {
        internal Uri Url;
    }
}
4

1 回答 1

0

调试器下的不同结果可能是因为您的所有数据都适合套接字缓冲区,并且调试的额外延迟允许所有数据在调用Receive.

然而,在正常执行下,情况并非如此。数据将以数据包的形式到达,为了知道数据的结尾在哪里,您必须解析您的 HTTP 请求。Socket 的属性只表示此时缓冲区中有Available多少数据,而不是整个消息的长度。虽然使用原始套接字绝对可以读取,但使用 a 更容易:NetworkStream

IEnumerable<string> ReadRequestHeaders(Socket s)
{
    using (var stream = new NetworkStream(s, false))
    using (var reader = new StreamReader(stream))
    {
        while (true)
        {
            var line = reader.ReadLine();
            if (line == "")
                break;

            yield return line;
        }
    }
}

HTTP 消息的其余部分也是如此——为了正确转发它,您必须知道它在哪里结束,这意味着理解 Content-Length 或 Transfer-Encoding 字段并进行相应的解析。

于 2013-03-31T23:36:44.833 回答