Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTcpSocket/QTcpServer on a heavy load server

QTcpSocket/QTcpServer on a heavy load server

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Eumcoz
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • F Offline
      F Offline
      franku
      wrote on last edited by
      #2

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

      into:

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

      This, Jen, is the internet.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        franku
        wrote on last edited by
        #3

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

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

        This, Jen, is the internet.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved