多个客户端与服务器应用程序超过 15 个客户端处理问题

发布于 2024-10-10 13:14:32 字数 2913 浏览 2 评论 0原文

我有一个用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画▽骨i 2024-10-17 13:14:32

m_workerSocketPkt 数组(或 m_workerSocket)增加到 15 以上或将其转换为 List。 (我猜是因为你没有出示它的声明)

Increase the m_workerSocketPkt array (or m_workerSocket) to more than 15 or convert it to a List<>. (I'm guessing since you did not show it's declaration)

小…楫夜泊 2024-10-17 13:14:32

即使是最基本的 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文