简体   繁体   English

TcpListener间歇性地无法正常工作

[英]TcpListener intermittently not working

Attempting to write a basic proxy I have implemented code along the following lines as a start. 尝试编写基本代理我已经按照以下几行实现了代码作为开始。

private Thread _proxyThread;

public void Start() 
{
    this._proxyThread = new Thread(StartListener);
    this._proxyThread.Start();
}

protected void StartListener(object data)
{
    this._listener = new TcpListener(IPAddress.Any, 1234);
    this._listener.Start();

    while (true)
    {
        TcpClient client = this._listener.AcceptTcpClient();

        while (client.Connected)
        {
            NetworkStream stream = client.GetStream();

            StringBuilder request = new StringBuilder();

            byte[] bytes = new byte[1024];

            while (stream.DataAvailable && stream.CanRead)
            {   
                int i = stream.Read(bytes, 0, bytes.Length);
                request.Append(System.Text.Encoding.ASCII.GetString(bytes, 0, i));                        
            }

            if (stream.CanWrite)
            {
                byte[] response = System.Text.Encoding.Default.GetBytes("HTTP/1.1 200 OK" + Environment.NewLine + "Content-length: 4\r\n\r\n" + "Test");
                stream.Write(response, 0, response.Length);
            }

            client.Close();
        }
    }
}

I then set my LAN proxy like this 然后我像这样设置我的LAN代理

代理

My current goal (as you can hopefully deduce from my code) is to return just a text value with no headers or anything. 我目前的目标(因为你可以从我的代码中推断出来)是只返回一个没有标题或任何内容的文本值。

The problem that I am running into is that any of my attempts to run the code as is results in an intermittent behaviour where Chrome and IE9 will usually just return a ERR_CONNECTION_ABORTED or Internet Explorer cannot display the webpage respectively and very occasionally display the expected output "TEST". 我遇到的问题是,我尝试运行代码的任何尝试都会导致间歇性行为,Chrome和IE9通常只返回ERR_CONNECTION_ABORTED,或者Internet Explorer无法分别显示网页 ,偶尔会显示预期的输出“测试”。

I originally tried using a asynchronous approach (Using BeginAcceptTcpClient()) but have reverted back to this simpler technique because I thought this problem might stem from me doing something incorrectly in the asynchronous implementation but it seems that the cause is something else. 我最初尝试使用异步方法(使用BeginAcceptTcpClient()),但已经恢复到这种更简单的技术,因为我认为这个问题可能源于我在异步实现中做错了但似乎原因是别的。

Could anyone please provide any guidance? 有人可以提供任何指导吗?

After your first iteration of the loop while (client.Connected) you close the connection. 在第一次循环迭代后while (client.Connected)您关闭连接。 Only close it when you are done transferring all data. 只有在完成传输所有数据后才关闭它。

It's not whether it's synchronous or asynchronous, you're just not responding with a proper HTTP response. 它不是同步还是异步,你只是没有用正确的HTTP响应做出响应。 Take a tool like Fiddler and look at the data being received when you request a regular webpage. 使用像Fiddler这样的工具,查看您请求常规网页时收到的数据。

If you return this, it should work: 如果你返回这个,它应该工作:

HTTP/1.1 200 OK
Content-length: 4

TEST

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM