Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 简单代理服务器_C#_Tcp - Fatal编程技术网

C# 简单代理服务器

C# 简单代理服务器,c#,tcp,C#,Tcp,我正在尝试用C#编写一个简单的代理服务器代码,使用套接字(在GET only atm上工作),下面的代码“有效”,它确实通过我运行的代理显示网页,但大多数时候速度很慢,并且总是以套接字异常结束,我不知道如何处理 指针会很好 class ProxyServer { static void Main(string[] args) { const int DEFAULT_PORT_NO = 27015; int port = -1; if(arg

我正在尝试用C#编写一个简单的代理服务器代码,使用套接字(在GET only atm上工作),下面的代码“有效”,它确实通过我运行的代理显示网页,但大多数时候速度很慢,并且总是以套接字异常结束,我不知道如何处理

指针会很好

class ProxyServer {
    static void Main(string[] args) {
        const int DEFAULT_PORT_NO = 27015;
        int port = -1;
        if(args.Length > 0 && args[0] != null) {
            port = Int32.Parse(args[0]); 
        } else if(port == -1) {
            port = DEFAULT_PORT_NO;
        }

        const int BACKLOG = 10;
        Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        IPAddress ipAddress = IPAddress.Loopback;
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);
        listener.Bind(localEndPoint);
        listener.Listen(BACKLOG);
        Console.WriteLine(localEndPoint.ToString() + " Listening...\n");

        while(true) {
            Socket listenSocket = listener.Accept();
            RequestHandler requestHandler = new RequestHandler(listenSocket);
            Thread requestThread = new Thread(new ThreadStart(requestHandler.handle));
            requestThread.Start();
        }
    }
}

class RequestHandler {
    private Socket serverSocket;

    public RequestHandler(Socket s) {
        serverSocket = s;
    }

    public void handle() {
        string header = getHeader(serverSocket);
        string host = getHost(header);
        Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        IPAddress[] addresslist = Dns.GetHostAddresses(new Uri(host).Host);
        IPEndPoint remoteEndPoint = new IPEndPoint(addresslist[0], new Uri(host).Port);
        clientSocket.Connect(remoteEndPoint);
        sendRequest(clientSocket, header);

        header = getHeader(clientSocket);
        sendRequest(serverSocket, header);
        const int BUFFER_SIZE = 1024;
        byte[] buffer = new byte[BUFFER_SIZE];
        int receivedBytes = clientSocket.Receive(buffer);
        while(receivedBytes > 0) {
            serverSocket.Send(buffer, receivedBytes, SocketFlags.None);
            receivedBytes = clientSocket.Receive(buffer);
        }

        serverSocket.Shutdown(SocketShutdown.Both);
        serverSocket.Close();
    }

    string getHeader(Socket s) {
        string header = "";
        while(true){
            byte[] bytes = new byte[1];
            int bytesRec = s.Receive(bytes);
            header += System.Text.Encoding.ASCII.GetString(bytes, 0, bytesRec);
            if (header.IndexOf("\r\n\r\n") > -1 || header.IndexOf("\n\n") > -1){
                break;
            }
        }
        return header;
    }

    string getHost(String h) {
        return h.Split(new char[] {'\n'})[0].Split(new char[] {' '})[1];
    }

    void sendRequest(Socket s, string h) {
        byte[] bytesSent = System.Text.Encoding.ASCII.GetBytes(h);
        s.Send(bytesSent, bytesSent.Length, SocketFlags.None);
    }

}