您好,欢迎来到保捱科技网。
搜索
您的当前位置:首页利用.net Socket 的Acceptex方法加速你的连接

利用.net Socket 的Acceptex方法加速你的连接

来源:保捱科技网
今天因为一个需要,要研究一下sock的异步性能,就用Reflector看了看。发现在调用的时候.net优先调用的是acceptex的 api,而不是accept api。在msdn里查看了一下这两者的区别:

The AcceptEx function uses overlapped I/O, unlike the accept function. If your application uses AcceptEx, it can service a large number of clients with a relatively small number of threads. As with all overlapped Windows functions, either Windows events or completion ports can be used as a completion notification mechanism.

 

Another key difference between the AcceptEx function and the function is that AcceptEx requires the caller to already have two sockets:

  • One that specifies the socket on which to listen.
  • One that specifies the socket on which to accept the connection.


it can service a large number of clients with a relatively small number of threads
这句话那是非常的诱人啊!

那我们再来看看什么情况下可以使用这个扩展的Accept呢?

首先在.net的Accept非异步函数里面是不可以调用这个的,只有在异步调用里面才有:BeginAccept方法
其反编译后如下:

下面是CanuseAcceptEx属性的反编译:
private   bool  CanUseAcceptEx
{
    
get
    
{
        
if (!ComNetOS.IsWinNt)
        
{
            
return false;
        }

        
if (!Thread.CurrentThread.IsThreadPoolThread && !SettingsSectionInternal.Section.AlwaysUseCompletionPortsForAccept)
        
{
            
return this.m_IsDisconnected;
        }

        
return true;
    }

}

 
那个iswinnt还没有找到相关资料,不过看字面意思看起来是平台相关的,是winnt系统应该都可以。
另外检验其他的是否为线程池和是否总是使用完成端口的检测如果不通过的话就检测连接是否还连接着。

其中再各个重载方法中,最重要的是检测IsWinNt选项,要不然就不会执行这个AcceptEx方法。

转载于:https://www.cnblogs.com/perfectdesign/archive/2008/04/10/Socket_AcceptEx.html

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baoaiwan.cn 版权所有 赣ICP备2024042794号-3

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务