首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >每秒接受的SocketAsyncEventArgs和second

每秒接受的SocketAsyncEventArgs和second
EN

Stack Overflow用户
提问于 2012-11-04 22:53:31
回答 1查看 356关注 0票数 1

我正在使用SocketAsyncEventArgs,并且我每秒只能执行几次接受(我不知道为什么)。

如何最大化每秒接受的数量?我应该多次调用Accept(null)吗?

代码:在侦听套接字上调用Listen之后,我调用了Accept(null),这是我的代码:

代码语言:javascript
复制
void Accept(SocketAsyncEventArgs acceptEventArg)
{
    if (acceptEventArg == null)
    {
        acceptEventArg = new SocketAsyncEventArgs();
        acceptEventArg.Completed += AcceptCompleted;
    }
    else
    {
        // socket must be cleared since the context object is being reused
        acceptEventArg.AcceptSocket = null;
    }

    bool willRaiseEvent = _listener.AcceptAsync(acceptEventArg);
    if (!willRaiseEvent)
    {
        Accepted(acceptEventArg);
    }
}

void AcceptCompleted(object sender, SocketAsyncEventArgs e)
{
    if (_onAccepted != null) _onAccepted(e);
}

void Accepted(SocketAsyncEventArgs e)
{
    if (!e.AcceptSocket.Connected) return;

    //1
    var acceptSocket = e.AcceptSocket;
    var readEventArgs = CreateArg(null, e.AcceptSocket, null);

    bool willRaiseEvent = true;
    if (acceptSocket.Connected)
        willRaiseEvent = acceptSocket.ReceiveAsync(readEventArgs);

    //2
    Accept(e);

    //3
    if (!willRaiseEvent)
    {
        Received(readEventArgs);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2012-11-05 03:10:31

默认情况下有连接限制。可以使用DefaultConnectionLimit http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.defaultconnectionlimit.aspx对其进行更改

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13219598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档