HttpListener多个连接失败

时间:2015-02-01 12:22:34

标签: c# httplistener

我正在尝试通过5个或6个标签同时在服务器上打开C#连接。

第一个连接有效。

其他人挂起(浏览器为waiting

public class HtServer {
    HttpListener HL = new HttpListener();

    public void startServer(){
        HL.Prefixes.Add("http://127.0.0.1:800/");
        HL.Start();
        IAsyncResult HLC = HL.BeginGetContext(new AsyncCallback(clientConnection),HL);
    }

    public static void clientConnection(IAsyncResult res){
        HttpListener listener = (HttpListener)res.AsyncState;
        HttpListenerContext context = listener.EndGetContext(res);
        HttpListenerRequest request = context.Request;
        // Obtain a response object.
        HttpListenerResponse response = context.Response;
        // Construct a response. 
        string responseString = "<HTML><BODY> Hello world! " + DateTime.Now + "</BODY></HTML>";
        byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
        // Get a response stream and write the response to it.
        response.ContentLength64 = buffer.Length;
        System.IO.Stream output = response.OutputStream;
        output.Write(buffer, 0, buffer.Length);
        // You must close the output stream.
        Thread.Sleep(4000);
        output.Close();
    }
}

0 个答案:

没有答案