QTcpSocket/QTcpServer on a heavy load server



  • Hello, I am designing and making a server that should be able to handle about 100+ hits per second. The information I am getting from the server is just the HTTP header. Based on the information from the header, it will query a database(different thread) for some information and send the final information back to the QTcpServer which create an output string, and send back a HTTP Response. I am having a big problem with this that I cannot debug. My code look similar to this:

    @TCPInterface::TCPInterface(QObject *parent): QTcpServer(parent)
    {
    //start listening for tcp traffic on port 80
    listen(QHostAddress::Any, 80);

    connect(this,SIGNAL(sendInfo(QTcpSocket*, QString *)), databaseThread, SLOT(recieveInfo(QTcpSocket*, QString*)));
    connect(databaseThread, SIGNAL(sendToTCPSend(QTcpSocket *, QString *)), this, SLOT(TCPSend(QTcpSocket*, QString*)));
    

    }@

    @void TCPInterface::incomingConnection(int socket)
    {
    QTcpSocket *s = new QTcpSocket(this);

    connect(s, SIGNAL(readyRead()), this, SLOT(readClient()));
    

    // connect(s, SIGNAL(disconnected()), this, SLOT(discardClient()));

    s->setSocketDescriptor(socket);
    

    }@

    @//void TCPInterface::discardClient()
    //{
    // QTcpSocket* socket = (QTcpSocket*)sender();
    // socket->deleteLater();
    //}@

    @void TCPInterface::readClient()
    {
    QTcpSocket* socket = (QTcpSocket*)sender();

    QString header;
    while(socket->canReadLine())
    {
        header += socket->readLine();
    }
    
    emit sendInfo(socket, headerInfo);
    

    }@

    @void databaseThread::recieveInfo(QTcpSocket* socket, QString* headerInfo)
    {
    QString*outputInfo = getDatabaseInfo(headerInfo);
    emit sendToTCPSend(socket, outputInfo);
    }@

    @void TCPInterface::TCPSend(QTcpSocket* socket, QString* outputInfo);
    {
    QString response = "HTTP/1.0 200 Ok\r\n";
    response += "Content-Type: text/html; charset="utf-8"\r\n";
    response += "\r\n" + *outputInfo + "\n";

    if(socket->isWritable() && socket->isOpen())
    {
         socket->write(response.toAscii());
    }
    //socket->disconnectFromHost();
    socket->close();
    delete headerInfo;
    

    }@

    I having one main problem which I have an idea what it is, but cannot find a solution to fix it.

    My problem is my memory is constantly increasing as I get more hits. I am sure the cause of this is my QTcpSockets are never being deleted, since I am just closing them. However when I don't use close, and use disconnectFromHost and disconnected/discardClient slot/signal my server will crash with heavy traffic(no message or anything so I am not sure of the exact reason of the crash). Has anyone run into this problem before? Any ideas at all.



  • Why not change:
    @
    ...}
    //socket->disconnectFromHost();
    socket->close();
    delete headerInfo;
    }@

    into:

    @
    ...}
    //socket->disconnectFromHost();
    socket->close();
    delete socket;
    delete headerInfo;
    }@



  • You'll have to find a way to delete this socket:

    @
    void TCPInterface::incomingConnection(int socket)
    {
    QTcpSocket *s = new QTcpSocket(this);
    ...
    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.