Reputation: 13521
I am using SocketAsyncEventArgs and I can perform only a few accepts per second (I do not know why).
How can I maximize number of accepts per second? Should I call Accept(null) multiple times?
Code: After calling Listen on the listening socket I call Accept(null) and this is my code:
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);
}
}
Upvotes: 1
Views: 369
Reputation: 2646
There is a connection limit by default. You can change it using the DefaultConnectionLimit http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.defaultconnectionlimit.aspx
Upvotes: 0