search
Japanese Chinese Nederlands Espanol Italiano Deutsch Francais Twitter Rss Feeds
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

Web ProgrammingArticlesForumsFAQs
JavaScript
ASP
ASP.NET
Web Services

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

DatabasesArticlesForumsFAQs
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsFAQs
Excel
Word
Powerpoint
Outlook
Publisher
Money

Operating SystemsArticlesForumsFAQs
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
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 
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.

 

 


 
re
Web star replied to Rishi Mishra at 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);

}
 
use HttpListener
Web star replied to Rishi Mishra at 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);
}
}
}
 
 
  Re :: Handle multiple requests simultaneously using HttpListeners
Sanjay Verma replied to Rishi Mishra at 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.

 
read this
C_A P replied to Rishi Mishra at 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.