混合同步和异步调用插座 [英] Mixing synchronous and asynchronous socket calls

查看:159
本文介绍了混合同步和异步调用插座的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时它认为不好的做法,同步和异步套接字调用同一服务器上的结合?例如(从MSDN修改):

Is it considered bad practice to combine synchronous and asynchronous socket calls on the same server? For example (modified from the msdn):

// Initalize everything up here

while (true) {
     // Set the event to nonsignaled state.
     allDone.Reset();  //allDone is a manual reset event

     // Start an asynchronous socket to listen for connections.
     Console.WriteLine("Waiting for a connection...");
     listener.BeginAccept( 
     new AsyncCallback(AcceptCallback), listener);

      // Wait until a connection is made before continuing.
      allDone.WaitOne();
}

public static void AcceptCallback(IAsyncResult ar) {
    // Signal the main thread to continue.
    allDone.Set();

    // Handle the newly connected socket here, in my case have it
    // start receiving data asynchronously
}

在这种情况下,因为你等到每个连接在听取下一个连接前作出,好像这是pretty太大的阻塞调用。鉴于它应该是一个有点快,因为客户端的初始处理将完成它在不同的线程,但在理论上应是一个相对小的开销。

In this case, because you are waiting until each connection has been made before listening to the next connection, it seems like this is pretty much a blocking call. Given it should be a little bit faster because the initial handling of the client will be done it a different thread, but that should in theory be a relatively small overhead.

鉴于此,它会被认为是不好的做法,这样做:

Given this, would it be considered bad practice to do something like:

while (true) {
     // Start listening for new socket connections
     Socket client = listener.Accept(); // Blocking call


    // Handle the newly connected socket here, in my case have it
    // start receiving data asynchronously
}

在我看来,这code就简单多了那么code以上,而如果我正确的应该有一个比较接近的性能上面code为好。

as in my mind, this code is much simpler then the code above, and if I'm correct should have a relatively close performance to the above code as well.

推荐答案

看你的两个例子,我在什么发生很少看到差异(如有的话)。我斗胆说你的第二个表(直行的同步调用)是更好,因为它远远那么复杂,行为上是相同的。如果你可以看看为接受,在某些时候(可能在操作系统)源它会做一样一样的你更详细的code。

Looking at your two examples, I see very little difference (if any at all) in what's occurring. I'd venture that your second form (going straight for the synchronous call) is better because it's far less complicated and is behaviourally identical. If you could look at the source for Accept, at some point (probably in the OS) it will be doing much the same as your more verbose code.

不过...

有一个非常快速的mod将旧code消除了所有的封锁,让万物异步发生:

A very quick mod to your old code eliminates all blocking and lets everything occur asynchronously:

void Accept()
{
    Console.WriteLine("Waiting for a connection...");
    listener.BeginAccept(AcceptCallback, listener);
}
public static void AcceptCallback(IAsyncResult ar) {
            var listener = (Socket)ar.AsyncState;
            //Always call End async method or there will be a memory leak. (HRM)                
            listener.EndAccept(ar); 
    Accept();

    //bla
}

MMMM ...好多了。

mmmm... much better.

这篇关于混合同步和异步调用插座的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆