1

I want to listen web browser's request on client. I know that I should use HttpListener but I had no idea that use from asynchronous or synchronous? Use BeginGetContext or GetContext?

try
{
    HttpListener listener = new HttpListener();
    listener.Prefixes.Add[this is not a link]("http://127.0.0.1:9080/");  
    listener.Start();

    Console.WriteLine("im listening...");

    HttpListenerContext context = listener.GetContext();
    HttpListenerRequest request = context.Request;
}
catch(exception e)
{
   console.writeline(e.tostring());
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
hadi
  • 11
  • 1

1 Answers1

1

I have written before about the trade-off between synchronous and asynchronous IO. Because there will only ever be one thread occupied with accepting new clients synchronous IO is fine. You could at most save one thread which is an insignificant amount of savings.

You might want to make the actual request processing async. If you have many concurrent requests that can be a good idea. If you have few concurrent requests you gain nothing.

Remember that async IO is generally more work and more error prone. Use it when there is a good reason to do it.

Community
  • 1
  • 1
usr
  • 168,620
  • 35
  • 240
  • 369