Search EggHeadCafe's Job Board
EggHeadCafe Silverlight WPF ASP.NET VB.NET C# Excel SQL Server SharePoint
search
MicrosoftArticlesForumsFAQs
C# .NET
VB.NET
Visual Studio .NET
ADO.NET
Xml / Xslt
VB 6.0
.NET CF
GDI+
LINQ
Deployment
Security
FoxPro
Silverlight / WPF
Entity Framework
RIA Services

WebArticlesForumsFAQs
JavaScript
ASP
ASP.NET
WCF

DatabasesArticlesForumsFAQs
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsFAQs
Excel
Word
Powerpoint
Outlook
Publisher
Money

Non-MicrosoftArticlesForumsFAQs
NHibernate
Perl
PHP
Ruby
Java
Linux / Unix
Apple
Open Source

Operating SysArticlesForumsFAQs
Windows 7
Windows Server
Windows Vista
Windows XP
Windows Update
MAC
Linux / UNIX

Server PlatformsArticlesForumsFAQs
BizTalk
Site Server
Exhange Server
IIS

Graphic DesignArticlesForumsFAQs
Macromedia Flash
Adobe PhotoShop
Expression Blend
Expression Design
Expression Web

OtherArticlesForumsFAQs
Lounge
Subversion / CVS
Ask Dr. Dotnetsky
Active Directory
Networking
Uninstall Virus
Job Openings
Product Reviews
Search Engines
Resumes

 

  View Other C# .NET Posts   Ask New Question  Ask New Question With Power Editor

How to handle multiple requests simultaneously using HttpListeners
Rishi Mishra posted at Wednesday, November 19, 2008 2:12 AM

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.

 

 

Reply    Reply Using Power Editor
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0
re
Web star replied to Rishi Mishra on Wednesday, November 19, 2008 2:18 AM

webservice that will receive/process multiple
client requests simultaneously. For this purpose, I wrote the following
code, but it does not seem to be handling more than two at a time. I
put in a few console prints and have also attached the output. Here it
goes -

public void serverStart() {

   HttpListener _listener = new HttpListener();

   _listener.Prefixes.Add(.......);

   _listener.Start();

   while (true)

       ProcessRequest();

}

public void ProcessRequest()

{

    Console.WriteLine("PR " + ++count); //count initialized to 0 in
beginning

    IAsyncResult result = _listener.BeginGetContext(new AsyncCallback
ListenerCallback),this._listener);

    result.AsyncWaitHandle.WaitOne();

}

protected void ListenerCallback(IAsyncResult result)

{

    if (this._listener == null) return;

    HttpListenerContext context = this._listener.EndGetContext(result);

    Console.WriteLine("LC " + count);

    this.ProcessRequest2(context);

}

public void ProcessRequest2(HttpListenerContext ctx)

{

    Console.WriteLine("PR2 " + count);

    string str = ctx.Request.HttpMethod;

    HttpListenerWorkerRequest workerRequest =

    new HttpListenerWorkerRequest(ctx,_virtualDir, _physicalDir,
_logCallback, _pxeb);

    HttpRuntime.ProcessRequest(workerRequest);

}
Reply    Reply Using Power Editor
  Rank Winnings Points
November 5 $55.00 143
October 10 $28.00 94

use HttpListener
Web star replied to Rishi Mishra on Wednesday, November 19, 2008 2:19 AM

HttpListener allows only one request to be handled concurrently?
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.IO;
 
sealed class HttpServer
{
public static void Main()
{
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://*:" + 1234 + "/");
listener.Start();
for(int i =0; i< 10; i++)
{
IAsyncResult result = listener.BeginGetContext(new AsyncCallback(HttpWorker.Handle), listener);
}
Console.WriteLine("Press ENTER to quit.");
Console.Read();
}
}
public class HttpWorker{
 
public static void Handle(IAsyncResult ar)
{
HttpListener listener = (HttpListener) ar.AsyncState;
try
{
HttpListenerContext context = listener.EndGetContext(ar);
StreamWriter writer = new StreamWriter(context.Response.OutputStream);
writer.Write("<html><body>");
writer.Write("This call was handled in " + Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(5000);
writer.Write("<br>");
writer.Write("Done");
writer.Write("</body></html>");
writer.Close();
context.Response.OutputStream.Close();
context.Response.Close();
}
finally
{
listener.BeginGetContext(new AsyncCallback(HttpWorker.Handle), listener);
}
}
}
 
Reply    Reply Using Power Editor
  Rank Winnings Points
November 5 $55.00 143
October 10 $28.00 94

Re :: Handle multiple requests simultaneously using HttpListeners
Sanjay Verma provided a rated reply to Rishi Mishra on Wednesday, November 19, 2008 2:19 AM

See the following articles

http://social.msdn.microsoft.com/forums/en-US/netfxnetcom/thread/b6108c08-fe15-4692-9392-248b91a95dea/

http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework/topic57727.aspx

Hope this helps.

Reply    Reply Using Power Editor
Asp.Net Developer
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0

read this
C_A P replied to Rishi Mishra on Wednesday, November 19, 2008 2:26 AM

Ok the question you are asking is a bit tricky.  Deep down httplistener is using IOCompletion ports.  You can easily verify this yourself by placing the following code in your Handle function.

int ioThreads, workerThreads;

System.Threading.ThreadPool.GetAvailableThreads( out workerThreads, out ioThreads );

Console.WriteLine( "IOThreads: " + ioThreads.ToString() + " WorkerThreads: " + workerThreads.ToString() );

System.Threading.ThreadPool.GetMaxThreads( out workerThreads, out ioThreads );

Console.WriteLine( "IOThreadsMax: " + ioThreads.ToString() + " WorkerThreadsMax: " + workerThreads.ToString() );

you should see that the available IO threads are 999 and the max is 1000 which means that your request is coming in on an iothread (if it wasn't you would see the worker threads are 1 less than max), typically you only use 1 io thread per cpu with a 1 overflow so 2 per cpu.  I have no idea what rules the OS uses to decide if needs to use that 1 io thread overflow .  Plus you are using Sleep which may also be causing erroneous behaviour. 

Basically you are placing the sleep there to mimic work am I correct?

Problem is that Thread.Sleep is a very poor example for the system "working" its just not treated the same.  I suggest a different test.  Perhaps using microsofts free load tester and take the sleep out and see how many connections you can handle per second.  That is far better test for a system.  That is unless all you are trying to prove is that two requests on httplistener are happening at the same exact time.
Reply    Reply Using Power Editor
  Rank Winnings Points
November 0 $0.00 0
October 0 $0.00 0

How to handle multiple requests simultaneously using HttpListeners
Ravi Danum provided a rated reply to C_A P on Friday, November 20, 2009 1:35 PM

Hello,

I also saw that only two requests could be handled at a time by HttpListener.

To get around this, use the following.  I now have 10 executing at the same time with HttpListener.  The solution involves making a change to the registery.

1. Start the registry editor.

2. Locate the following key in the registry:

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings

3. On the Edit menu, point to New, click DWORD Value, and then add the following registry values:

Value name: MaxConnectionsPer1_0Server

Value data: 10

Base: Decimal

Value name: MaxConnectionsPerServer

Value data: 10

Base: Decimal

4. Exit the registry.

 

 

Reply    Reply Using Power Editor
  Rank Winnings Points
November 38 $0.00 3
October 0 $0.00 0