多个客户端与服务器应用程序超过 15 个客户端处理问题
我有一个用 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 个客户端接收数据时会产生问题,因为每个连接的客户端都会持续连接并向服务器发送数据。
I have a Server Application written in C# .NET and running on Windows XP SP3.
I am using asynchronous sockets programming for handling around 500 clients.
But I got a problem to entertain more than 15 client connections.
My application got shut down when 1 more client connected after 15 clients; I am not
understanding whether the problem is with my OS or is a tcp connection limitation problem in Windows XP.
Please help to solve out this issue, please suggest any solution.
Update:
Here is my piece of code.
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
}
The Max Client Value is here 200 and worker sockets also, so it should handle 200 clients but I think it's creating a problem in receiving data from more than 15 clients because every client that is connected is continuously connected and sends data to the server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
m_workerSocketPkt
数组(或m_workerSocket
)增加到 15 以上或将其转换为List
。 (我猜是因为你没有出示它的声明)Increase the
m_workerSocketPkt
array (orm_workerSocket
) to more than 15 or convert it to aList<>
. (I'm guessing since you did not show it's declaration)即使是最基本的 Windows XP 系统也应该能够处理 1000 个连接。 TCP 允许每台客户端计算机有数千个连接。 (我不建议每台服务器超过 100K)
如果您的程序失败,则可能是代码中的错误。它有可能产生您忽略的错误吗?您的程序出现什么错误?
Your even the most basic Windows XP system should be able to handle 1000 connections. TCP is allows thousands of connection per client machine. (I wouldn't suggest more than 100K per server)
If your program is failing its likely to be a bug in your code. Is possible it producing an error your are ignoring? What is the error your program gets?