0
Answer

my HTTP request handler code correct?

George George

George George

16y
2.1k
1

Hello everyone,


I can not find an asynchronous HTTP server from Google. So, I write my own code. My two questions,

1. I am wondering whether my code is correct? (I have tested basic function, but I would like guru like you to give comments) do you have any better implementations for reference to achieve better throughput of server?

2. I call EndGetContext before retrieving request/response stream from HTTP context, in order to wake up server (so that another incoming HTTP request could be handled while current request is handled in process). My concern is whether call EndGetContext early before complete read/write request/response stream is safe -- is it safe to read/write request/response stream after calling EndGetContext - the data in the streams are still valid and not released?

My code,

[Code]
        public void StartListener()
        {

            IAsyncResult result;
            server = new HttpListener();
            // server.Prefixes.Add
            server.Start();
            while (true)
            {
                result = server.BeginGetContext(new AsyncCallback(this.MyHttpCallback), server);
                result.AsyncWaitHandle.WaitOne();
            }

        }

        // callback function when there is HTTP request received
        private void HttpCallback(IAsyncResult result)
        {
                // notify server call is complete, so that server could have better throughput
                HttpListenerContext context = server.EndGetContext(result);

                // Handle Request here, e.g. get request stream and response stream from context;
        }
[/Code]


thanks in advance,
George