【问题标题】:Difference between asynchronous and synchronous for httplistenerhttplistener 的异步和同步的区别
【发布时间】:2014-10-10 02:07:08
【问题描述】:

我想在客户端监听网络浏览器的请求。我知道我应该使用HttpListener,但我不知道使用异步还是同步?使用BeginGetContextGetContext

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());
}

【问题讨论】:

    标签: sockets http asynchronous httplistener


    【解决方案1】:

    I have written before about the trade-off between synchronous and asynchronous IO. 因为只有一个线程用于接受新的客户端同步 IO 是可以的。您最多可以节省一个线程,这只是微不足道的节省。

    您可能希望使实际的请求处理异步。如果您有许多并发请求,那可能是个好主意。如果您的并发请求很少,您将一无所获。

    请记住,异步 IO 通常工作量更大且更容易出错。有充分理由时使用它。

    【讨论】:

      猜你喜欢
      • 2021-10-16
      • 2015-02-28
      • 1970-01-01
      • 2011-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-26
      相关资源
      最近更新 更多