我有一个用C#.NET编写的服务器应用程序,并在Windows XP SP3上运行。
我正在使用异步套接字编程来处理大约500个客户端。
但我遇到了超过15个客户端连接的问题。
我的应用程序在15个客户端之后再连接1个客户端时关闭;我不是 了解问题是出在我的操作系统上还是Windows XP中的tcp连接限制问题。
请帮助解决此问题,请提出任何解决方案。
这是我的一段代码。
public const int MAX_CLIENTS = 200;
public AsyncCallback pfnWorkerCallBack;
private Socket[] m_workerSocket = new Socket[MAX_CLIENTS];
// class for worker socket & callback method & data buffer
private SocketPacket[] m_workerSocketPkt = new SocketPacket[MAX_CLIENTS];
// page Load
m_mainSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPEndPoint ipLocal = new IPEndPoint(IPAddress.Any, 4321);
// Bind to local IP Address...
m_mainSocket.Bind(ipLocal);
// Start listening...
m_mainSocket.Listen(MAX_CLIENTS);
m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null);
// called when client is connected
public void OnClientConnect(IAsyncResult asyn)
{
try
{
m_clientCount = setclient();
SocketPacket abc = new SocketPacket();
m_workerSocketPkt[m_clientCount] = abc; //assigning with SocketPacket class
m_workerSocketPkt[m_clientCount].m_currentSocket =
m_mainSocket.EndAccept(asyn); //transferring connection to other thread
m_workerSocketPkt[m_clientCount].m_clientCount = m_clientCount;
m_workerSocketPkt[m_clientCount].templist = abcde; //assigning Amj (list) class
m_workerSocket[m_clientCount] = m_workerSocketPkt[m_clientCount].m_currentSocket;
pfnWorkerCallBack = new AsyncCallback(m_workerSocketPkt[m_clientCount].OnDataReceived); //defining AsynCallBack function for the accepted socket
m_workerSocketPkt[m_clientCount].oldpfnWorkerCallBack = pfnWorkerCallBack;
m_workerSocketPkt[m_clientCount].data_rec = false;
m_workerSocketPkt[m_clientCount].data_sent = false;
m_workerSocketPkt[m_clientCount].m_currentSocket.BeginReceive(
m_workerSocketPkt[m_clientCount].dataBuffer, //assigning data buffer for receiving for the socket
0, //assigning data buffer offset for receiving for the socket
m_workerSocketPkt[m_clientCount].dataBuffer.Length, //assigning maximum data length for receiving for the socket
SocketFlags.None, //socket flags
pfnWorkerCallBack, //AysnCallBack delegate
m_workerSocketPkt[m_clientCount].m_currentSocket //state
);
m_mainSocket.BeginAccept(new AsyncCallback(OnClientConnect), null); //start invitation for other new sockets
}
最大客户端值在这里也是200个工作套接字,因此它应该处理200个客户端但我认为它在从超过15个客户端接收数据时产生了问题,因为连接的每个客户端都是连续连接的并且将数据发送到服务器
答案 0 :(得分:1)
将m_workerSocketPkt
数组(或m_workerSocket
)增加到15以上或将其转换为List<>
。 (我猜是因为你没有表明它的声明)
答案 1 :(得分:0)
即使是最基本的Windows XP系统也应该能够处理1000个连接。 TCP允许每个客户端机器数千个连接。 (我不建议每台服务器超过100K)
如果您的程序失败,则可能是您的代码中的错误。有可能产生你忽略的错误吗?你的程序有什么错误?