HttpListener leaves connections in TIME_WAIT

ⅰ亾dé卋堺 提交于 2019-12-25 06:56:35

问题


I have a simple C# Windows Service that should respond "OK" to GET requests on port 8080. Everything works fine, with one simple itch...

Every single request made to the service stays in TIME_WAIT. Am I missing something, I've looked around and most samples I've seen on HTTPListeners and Windows Services are doing it in a similar fashion?!

    private HttpListener _listener;

    protected override void OnStart(string[] args)
    {
        _listener = new HttpListener();
        _listener.Prefixes.Add("http://*:8080/");
        _listener.Start();
        _listener.BeginGetContext(new AsyncCallback(OnRequestReceive), _listener);
    }

    protected override void OnStop()
    {
        _listener.Stop();
    }

    private void OnRequestReceive(IAsyncResult result)
    {
        if (!_listener.IsListening)
            return;

        //Get context for a request.
        HttpListenerContext context = _listener.EndGetContext(result);
        HttpListenerRequest request = context.Request;

        //Obtain a response object.
        HttpListenerResponse response = context.Response;
        response.ContentType = "application/json";
        response.KeepAlive = false;

        //Our return message...
        string responseString = "OK";

        //Construct the response. 
        byte[] buffer = Encoding.UTF8.GetBytes(responseString);

        Stream output = response.OutputStream;
        response.ContentLength64 = buffer.Length;
        output.Write(buffer, 0, buffer.Length);

        //Close and send response
        try
        {
            output.Flush();
            output.Close();
            response.Close();
        }
        finally
        {   
            //Wait for another request
            _listener.BeginGetContext(new AsyncCallback(OnRequestReceive), _listener);
        }
    }      

Edit: Fixed Local declaration of _listener.


回答1:


Thanks to rene for pointing out the correct direction...

TIME-WAIT (either server or client) represents waiting for enough time to pass to be sure the remote TCP received the acknowledgment of its connection termination request. [According to RFC 793 a connection can stay in TIME-WAIT for a maximum of four minutes known as a MSL (maximum segment lifetime).]

For anyone else who wants to change this behavior: The TIME_WAIT period is configurable by modifying the following DWORD registry setting that represents the TIME_WAIT period in seconds.

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\TCPIP\Parameters\TcpTimedWaitDelay



来源:https://stackoverflow.com/questions/22159213/httplistener-leaves-connections-in-time-wait

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!