这听起来像是一个愚蠢的问题,但我一再寻找答案无济于事。
我想知道的是SocketAsyncEventArgs读/写池的合理大小,应该在侦听套接字上将backlog大小设置为什么,并且两者是直接链接的。这些值也直接与套接字服务器的性能相关联,也可能是改变其中一个或两者的影响。
如果侦听套接字积压已满,会发生什么情况,这会禁止客户端连接? (我现在正在假设它)如果当前正在使用池中的所有读写SocketAsyncEventArgs,那么监听套接字会等到一个空闲吗?
当时两者都使用相同的10。
如果这取决于它运行的机器类型,它将是一个单核心的Windows 2012服务器,开始时有2GB的RAM,并且一旦正常工作,将扩展到6核,20gb。
下面将显示我的套接字侦听器的初始化
/// <summary>
/// The socket used to listen for incoming connection requests.
/// </summary>
private Socket listenSocket;
/// <summary>
/// Mutex to synchronize server execution.
/// </summary>
private static Mutex mutex = new Mutex();
/// <summary>
/// Buffer size to use for each socket I/O operation.
/// </summary>
private Int32 bufferSize;
/// <summary>
/// The total number of clients connected to the server.
/// </summary>
private Int32 numConnectedSockets;
/// <summary>
/// the maximum number of connections the sample is designed to handle simultaneously.
/// </summary>
private Int32 numConnections;
/// <summary>
/// Pool of reusable SocketAsyncEventArgs objects for write, read and accept socket operations.
/// </summary>
private SocketAsyncEventArgsPool readWritePool;
/// <summary>
/// Controls the total number of clients connected to the server.
/// </summary>
private Semaphore semaphoreAcceptedClients;
/// <summary>
/// Create an uninitialized server instance.
/// To start the server listening for connection requests
/// call the Init method followed by Start method.
/// </summary>
/// <param name="numConnections">Maximum number of connections to be handled simultaneously.</param>
/// <param name="bufferSize">Buffer size to use for each socket I/O operation.</param>
internal SocketListener(Int32 numConnections, Int32 bufferSize)
{
this.numConnectedSockets = 0;
this.numConnections = numConnections;
this.bufferSize = bufferSize;
this.readWritePool = new SocketAsyncEventArgsPool(numConnections);
this.semaphoreAcceptedClients = new Semaphore(numConnections, numConnections);
// Preallocate pool of SocketAsyncEventArgs objects.
for (Int32 i = 0; i < this.numConnections; i++)
{
SocketAsyncEventArgs readWriteEventArg = new SocketAsyncEventArgs();
readWriteEventArg.Completed += new EventHandler<SocketAsyncEventArgs>(OnIOCompleted);
readWriteEventArg.SetBuffer(new Byte[this.bufferSize], 0, this.bufferSize);
// Add SocketAsyncEventArg to the pool.
this.readWritePool.Push(readWriteEventArg);
}
}
这表明调用套接字侦听器的start方法时会发生什么。
internal void Start(Int32 port)
{
// Get host related information.
IPAddress[] addressList = Dns.GetHostEntry(Environment.MachineName).AddressList;
// Get endpoint for the listener.
IPEndPoint localEndPoint = new IPEndPoint(addressList[addressList.Length - 1], port);
// Create the socket which listens for incoming connections.
this.listenSocket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
this.listenSocket.ReceiveBufferSize = this.bufferSize;
this.listenSocket.SendBufferSize = this.bufferSize;
if (localEndPoint.AddressFamily == AddressFamily.InterNetworkV6)
{
// Set dual-mode (IPv4 & IPv6) for the socket listener.
// 27 is equivalent to IPV6_V6ONLY socket option in the winsock snippet below,
// based on http://blogs.msdn.com/wndp/archive/2006/10/24/creating-ip-agnostic-applications-part-2-dual-mode-sockets.aspx
this.listenSocket.SetSocketOption(SocketOptionLevel.IPv6, (SocketOptionName)27, false);
this.listenSocket.Bind(new IPEndPoint(IPAddress.IPv6Any, localEndPoint.Port));
}
else
{
// Associate the socket with the local endpoint.
this.listenSocket.Bind(localEndPoint);
}
// Start the server.
this.listenSocket.Listen(this.numConnections);
// Post accepts on the listening socket.
this.StartAccept(null);
// Blocks the current thread to receive incoming messages.
mutex.WaitOne();
}
很抱歉,如果这是一个冗长的问题,我真的不知道还有什么地方可以看。任何帮助将不胜感激
这段代码来自一篇关于Codeproject的非常好的文章,其中作者基于MSDN示例编写了他的代码。它可以在这里找到
http://www.codeproject.com/Articles/22918/How-To-Use-the-SocketAsyncEventArgs-Class