简体   繁体   English

(WINSOCK)C ++游戏服务器:TCPAccept上一次TCP连接过多

[英](WINSOCK) C++ Game Server: Too many TCP connections at once on TCPAccept

I'm trying to do a stress test in case attackers try to attempt a lot of TCP connections to my C++ game server at once. 我正在尝试进行压力测试,以防攻击者尝试一次尝试与我的C ++游戏服务器进行大量TCP连接。

I made a simple program that connects 1000 times via the TCP port to my game server. 我编写了一个简单的程序,该程序通过TCP端口将1000次连接到我的游戏服务器。 Here is where my server accepts connections: 这是我的服务器接受连接的位置:

bool serverIsOn = true;
double listen = tcplisten(12345, 30000, 1);
setnagle(listen, true);

...

while(serverIsOn){
    double playerSocket = tcpaccept(listen, 1);
    if(playerSocket > -1){
        cout << "Got a new connection, socket ID: " << playerSocket << endl;

        std::string ip = tcpip(playerSocket);
        if(ipIsBanned(ip))
            closesocket(playerSocket);
    }
}

Here are the definitions of tcplisten and tcpaccept: 以下是tcplisten和tcpaccept的定义:

double tcplisten(double port, double max, double mode)
{
    CSocket* sock = new CSocket();
    if(sock->tcplisten((int)port, (int)max,(int)mode))
        return AddSocket(sock);
    delete sock;
    return -1;
}

double tcpaccept(double sockid, double mode)
{
    CSocket*sock = (CSocket*)sockets.item((int)sockid);
    if(sock == NULL)return -1;
    CSocket*sock2 = sock->tcpaccept((int)mode);
    if(sock2 != NULL)return AddSocket(sock2);
    return -1;
}

The problem: The server stops accepting connections after my test program has ended. 问题: 测试程序结束后,服务器停止接受连接。 For example, if I connect on my game after (which only requests one connection as opposed to my test-attack program which requests 1000 connections), the game can not even connect. 例如,如果我之后连接游戏(它只请求一个连接,而我的测试攻击程序则请求1000个连接),则游戏甚至无法连接。 I also do not see any connections via console output on my server anymore. 我也看不到服务器上通过控制台输出的任何连接。 The last connection made was from the attack program. 最后建立的连接来自攻击程序。 It's as if it can't accept anymore and something has crashed but I can't figure out what, no exceptions or errors. 好像它已经不能再接受并且某些东西崩溃了,但是我不知道是什么,没有异常或错误。

This is a huge problem for my server as anyone can easily just crash the server and force it to have to restart to start accepting new connections again. 对于我的服务器而言,这是一个巨大的问题,因为任何人都可以轻松地使服务器崩溃,并迫使它必须重新启动以再次开始接受新连接。

What can I do to combat this? 我该如何应对呢? I mean I tried even setting the max number of connections at once to 30,000 but that didn't work. 我的意思是我什至尝试一次将最大连接数设置为30,000,但这没有用。 Even if it did work, someone could just try to connect more than 30,000 times making that useless. 即使它确实起作用,也有人可以尝试连接30,000次以上,从而使它毫无用处。

EDIT: 编辑:

More in-depth definitions of tcplisten and tcpaccept functions above: 上面的tcplisten和tcpaccept函数的更深入的定义:

bool CSocket::tcplisten(int port, int max, int mode)
{
    if((sockid = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) return false;
    SOCKADDR_IN addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = INADDR_ANY;
    addr.sin_port = htons(port);
    if(mode)setsync(1);
    if(bind(sockid, (LPSOCKADDR)&addr, sizeof(SOCKADDR_IN)) == SOCKET_ERROR)
    {
        closesocket(sockid);
        return false;
    }
    if(listen(sockid, max) == SOCKET_ERROR)
    {
        closesocket(sockid);
        //TEMP ADD
        sockid = INVALID_SOCKET;
        return false;
    }
    return true;
}


CSocket* CSocket::tcpaccept(int mode)
{
    //TEMP REMOVE: if(sockid<0)return NULL;
    if(sockid == INVALID_SOCKET) return NULL;
    SOCKET sock2;
    if((sock2 = accept(sockid, (SOCKADDR *)&SenderAddr, &SenderAddrSize)) != INVALID_SOCKET)
    {
        CSocket*sockit = new CSocket(sock2);
        if(mode >=1)sockit->setsync(1);
        return sockit;
    }

    return NULL;
}

EDIT 2: Here is the definition of AddSocket: 编辑2:这是AddSocket的定义:

int AddSocket(CSocket*b)
{
    for(int i = 0; i < sockets.count; i ++)
    {
        if(sockets[i] == NULL)
        {
            sockets.set(i, b);
            return i;
        }
    }
    sockets.Add(b);
    return sockets.count-1;
}

如果您使用的是Linux,则可以使用tcp_tw_reuse sysctl。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM