从流中获取所有字节

时间:2014-05-07 10:03:46

标签: c# sockets http tcp

我正在用c#编写代理,我写了这个方法来从流中获取字节但是在查看了这个帖子中的答案后:Proxy won't work in chrome and only partly in firefox我意识到我没有得到所有的字节。我该如何解决这个问题?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;

namespace LexProxy
{
    class ProxyServer
    {
        private TcpListener tcpListener;

        public ProxyServer()
        {
            this.tcpListener = new TcpListener(IPAddress.Any, 3000);
            this.tcpListener.Start();
            while (true)
            {
                Console.Write("Waiting for a connection... ");
                TcpClient client = tcpListener.AcceptTcpClient();
                Thread thread = new Thread(delegate()
                {
                    Serve(client);
                });
                thread.Start();
            }
        }

        private void Serve(TcpClient client)
        {
            Console.WriteLine("Connected!");
            NetworkStream stream = client.GetStream();
            byte[] request = GetBytesFromStream(stream, client.ReceiveBufferSize);
            if (request != null)
            {
                string requestString = System.Text.Encoding.UTF8.GetString(request);
                string[] requestParts = requestString.Split(' ');
                if (requestParts.Length >= 2)
                {
                    string method = requestParts[0];
                    if (!requestParts[1].Contains("http://") && !requestParts[1].Contains("https://"))
                        requestParts[1] = "http://" + requestParts[1];

                    Uri uri = new Uri(requestParts[1], UriKind.RelativeOrAbsolute);
                    string host = StringUtils.ReplaceFirst(uri.Host, "www.", "");
                    int port = uri.Port;
                    byte[] response = getResponse(host, port, request);
                    if (response != null)
                        stream.Write(response, 0, response.Length);
                    client.Close();
                }
            }
        }

        private byte[] getResponse(string host, int port, byte[] request)
        {
            TcpClient client = new TcpClient(host, port);
            NetworkStream stream = client.GetStream();
            stream.Write(request, 0, request.Length);
            byte[] response = GetBytesFromStream(stream, client.ReceiveBufferSize);
            return response;
        }

        private byte[] GetBytesFromStream(NetworkStream stream, int bufferSize)
        {
            Byte[] bytes = new Byte[bufferSize];
            int i;
            while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
            {
                return bytes;
            }

            return null;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

你做错了。您不应该在向下游发送响应之前尝试收集整个响应,而应该在它们到达时复制字节。无需增加延迟。

但请注意,您在getAllBytesFromStream()中编写的代码是无意义的。你有一个带有无条件return语句的while循环。怎么会不止一次执行?