none
How to handle multiple requests simultaneously using HttpListeners RRS feed

  • Question

  • How to handle multiple requests simultaneously using <g class="gr_ gr_7 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Style multiReplace" data-gr-id="7" id="7">HttpListeners ?</g> I am working on an application which <g class="gr_ gr_14 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Grammar multiReplace" data-gr-id="14" id="14"><g class="gr_ gr_13 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Grammar only-ins replaceWithoutSep" data-gr-id="13" id="13">listen</g></g> the <g class="gr_ gr_8 gr-alert gr_spell gr_inline_cards gr_disable_anim_appear ContextualSpelling ins-del multiReplace" data-gr-id="8" id="8">http</g> request but how will have to implement the multiple <g class="gr_ gr_12 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Grammar multiReplace" data-gr-id="12" id="12">request</g> simultaneously processed by the client application at <g class="gr_ gr_11 gr-alert gr_gramm gr_inline_cards gr_disable_anim_appear Grammar only-ins replaceWithoutSep" data-gr-id="11" id="11">same</g> time
    Wednesday, April 11, 2018 2:32 PM

All replies

  • How to handle multiple requests simultaneously using HttpListeners ? I am working on an application which listen the http request but how will have to implement the multiple request simultaneously processed by the client application at same time

    Wednesday, April 11, 2018 2:32 PM
  • It's going to work similarly to how TCP servers work. You'll create a listener in your main thread. Then you'll use GetContext or (more likely) GetContextAsync to start listening for connections.

    When a client connects you'll get back an HttpListenerContext which represents the client request. You can then process the request normally on a worker thread. Meanwhile the main listening thread can call GetContext(Async) and listen for the next connection.

    //Not tested!!!
    
    //Listener thread - assuming you're not using async/await here
    //Init the listener...
    var keepWaiting = true;
    while (keepWaiting)
    {
       //Start listening (cancellation not supported)
       var context = listener.GetContext();
    
       //Handle request on worker thread (not waiting for task
       //to complete)
       Task.Run(() => HandleRequest(conext));   
    };
    
    //Worker thread logic
    void HandleRequest ( HttpListenerContext context )
    {
    }

    Alternatively you could use the older async method BeginGetContext. The docs link has an example of how to use this method. The benefit of this older approach is that it already handles moving the request to a separate thread but it requires more work to implement.

    The code will get more complex as you add features such as being able to cancel outstanding requests that may be in flight, blocking until all requests have been processed (because of shutdown), error handling, etc. But the base algorithm is the same.


    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, April 11, 2018 4:24 PM
    Moderator
  • Hello,

     Well, you can not handle request simultaneously even on multi core CPUs, unless you have

    multiple NICs and a HTTP Listener for each. But, lets go with 1 NIC.  Here you want to consider,

    multi Thread access to single HTTP Port is bad.  Have one, App Main Thread preferred, do the

    Listen and for each request start a new Thread to do the work.  However, the incoming data

    request must be identified so no 2 or more Threads duplicate outgoing results. The outgoing

    messages should be handle by App Main Thread because you don't want multi Threads to

    collide.  And, yes, networking can get very complex very fast.  Michael has a good start for you.

    If that does not work then please give more details and what doesn't work for you.

     

    Thanks :)

    Wednesday, April 11, 2018 4:43 PM
  • Shouldn't your subject matter be more like "How can you do it concurrently?". There is no simultaneously.
    Wednesday, April 11, 2018 8:40 PM
  • Hey,

     You know as well as I do what most of these OP are really looking for.

    I can google every post title and return more than 50k hits, most over at SO.

    Its not like the OP requests are "new", right? They just don't want to search,

    read and repeat.  Where here some will give the answer in code form and

    forget about the OP learning curve. I have a learning curve too but also do

    some searches way before my post.

     ARGH! :p

    Wednesday, April 11, 2018 11:28 PM