处理用C多个请求#的HttpListener [英] Handling multiple requests with C# HttpListener

查看:291
本文介绍了处理用C多个请求#的HttpListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.NET Windows服务而产生一个线程,基本上只是充当的HttpListener 。在同步模式的例子这是工作的罚款......

I have a .NET Windows Service which spawns a thread that basically just acts as an HttpListener. This is working fine in synchronous mode example...

private void CreateLListener()
{
    HttpListenerContext context = null;
    HttpListener listener = new HttpListener();
    bool listen = true;

    while(listen)
    {
        try
        {
            context = listener.GetContext();
        }
        catch (...)
        {
            listen = false;
        }
        // process request and make response
    }
}

这个问题我现在是我需要这与多个请求的工作,并让他们回答同时或至少是重叠的方式。

The problem I now have is I need this to work with multiple requests and have them responded to simultaneously or at least in an overlapped way.

要进一步解释 - 客户端是媒体播放器应用程序,它开始通过与请求头属性的媒体文件的请求字节范围为0 - 。至于我可以告诉它这样做是为了制定出媒体容器是什么。

To explain further - the client is a media player app which starts by making a request for a media file with the request header property Range bytes=0-. As far as I can tell it does this to work out what the media container is.

在已经阅读了块(或者,如果它已经读够确定媒体类型),那么它使另一个请求(来自不同的客户端套接字号)与字节范围= XY 。在这种情况下,Y是在所述第一响应中返回的内容长度和X比小于250000字节(使用IIS作为测试发现)。在这个阶段,它是获得最后的块,看它是否能得到媒体的时间戳来衡量长度。

After it has read a 'chunk' (or if it has read enough to ascertain media type) it then makes another request (from a different client socket number) with Range bytes=X-Y. In this case Y is the Content-Length returned in the first response and X is 250000 bytes less than that (discovered using IIS as a test). At this stage it is getting the last 'chunk' to see if it can get a media time-stamp to gauge length.

看了如此,它与另一个请求范围字节= 0 - (从另一个套接字号)开始正常的流媒体文件

Having read that, it makes another request with Range bytes=0- (from another socket number) to start streaming the media file properly.

在任何时候不过,如果客户端的用户执行与 A'跳过'运行它,然后发送另一个请求(来自另一个插槽数)范围字节= Z - 其中Z是跳转到在媒体文件中的位置。

At any time though, if the user of the client performs a 'skip' operation it then sends another request (from yet another socket number) with Range bytes=Z- where Z is the position to jump to in the media file.

我不是很好用HTTP的东西,但据我可以告诉我需要使用多个线程来处理每个请求/响应,同时允许原的HttpListener 来回到倾听。我已经做了很多的寻找却找不到这似乎拟合模型。

I'm not very good with HTTP stuff but as far as I can tell I need to use multiple threads to handle each request/response while allowing the original HttpListener to return to listening. I've done plenty of searching but can't find a model which seems to fit.

编辑:

确认和感谢里克·斯特劳在下面的例子中,我能够适应适合我的需要...

Acknowledgement and gratitude to Rick Strahl for the following example which I was able to adapt to suit my needs...

<一个href=\"http://www.west-wind.com/weblog/posts/2005/Dec/04/Add-a-Web-Server-to-your-NET-20-app-with-a-few-lines-of-$c$c\">Add Web服务器到您的.NET 2.0的应用程序有几行code

推荐答案

如果你需要一个更简单的替代BeginGetContext,你可以只排队,而不是在主线程上执行他们的ThreadPool工作。喜欢这样:

If you need a more simple alternative to BeginGetContext, you can merely queue jobs in ThreadPool, instead of executing them on the main thread. Like such:

private void CreateLListener() {
    //....
    while(true) {
        ThreadPool.QueueUserWorkItem(Process, listener.GetContext());    
    }
}
void Process(object o) {
    var context = o as HttpListenerContext;
    // process request and make response
}

这篇关于处理用C多个请求#的HttpListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆