Results 1 to 11 of 11

Thread: QTcpSocket/QTcpServer on a heavy load server

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1

    Default 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:

    Qt Code:
    1. TCPInterface::TCPInterface(QObject *parent): QTcpServer(parent)
    2. {
    3. //start listening for tcp traffic on port 80
    4. listen(QHostAddress::Any, 80);
    5.  
    6. connect(this,SIGNAL(sendInfo(QTcpSocket*, QString *)), databaseThread, SLOT(recieveInfo(QTcpSocket*, QString*)));
    7. connect(databaseThread, SIGNAL(sendToTCPSend(QTcpSocket *, QString *)), this, SLOT(TCPSend(QTcpSocket*, QString*)));
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void TCPInterface::incomingConnection(int socket)
    2. {
    3. QTcpSocket *s = new QTcpSocket(this);
    4.  
    5. connect(s, SIGNAL(readyRead()), this, SLOT(readClient()));
    6. // connect(s, SIGNAL(disconnected()), this, SLOT(discardClient()));
    7.  
    8. s->setSocketDescriptor(socket);
    9. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //void TCPInterface::discardClient()
    2. //{
    3. // QTcpSocket* socket = (QTcpSocket*)sender();
    4. // socket->deleteLater();
    5. //}
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void TCPInterface::readClient()
    2. {
    3. QTcpSocket* socket = (QTcpSocket*)sender();
    4.  
    5. QString header;
    6. while(socket->canReadLine())
    7. {
    8. header += socket->readLine();
    9. }
    10.  
    11. emit sendInfo(socket, headerInfo);
    12. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void databaseThread::recieveInfo(QTcpSocket* socket, QString* headerInfo)
    2. {
    3. QString*outputInfo = getDatabaseInfo(headerInfo);
    4. emit sendToTCPSend(socket, outputInfo);
    5. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void TCPInterface::TCPSend(QTcpSocket* socket, QString* outputInfo);
    2. {
    3. QString response = "HTTP/1.0 200 Ok\r\n";
    4. response += "Content-Type: text/html; charset=\"utf-8\"\r\n";
    5. response += "\r\n" + *outputInfo + "\n";
    6.  
    7. if(socket->isWritable() && socket->isOpen())
    8. {
    9. socket->write(response.toAscii());
    10. }
    11. //socket->disconnectFromHost();
    12. socket->close();
    13. delete headerInfo;
    14. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpSocket/QTcpServer on a heavy load server

    I think the problem is that you try to delete the socket when it is not ready sending/receiving data
    connect to the signal disconnected() of the QTcpSocket use disconnectFromHost() to initiate the disconnect.
    then when the signal is fired disconnect the signal and kill the QTcpSocket

  3. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTcpSocket/QTcpServer on a heavy load server

    Side note to StrikeByte's. Kill them using QObject::deleteLater.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  4. #4
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1

    Default Re: QTcpSocket/QTcpServer on a heavy load server

    Sorry, I think I made everything kind of confusing. I have tried using deleteLater.

    I have tried:

    Qt Code:
    1. void TCPInterface::discardClient()
    2. {
    3. QTcpSocket* socket = (QTcpSocket*)sender();
    4. socket->deleteLater();
    5. }
    To copy to clipboard, switch view to plain text mode 

    Which is connected to the signal disconnected. And when doing this I call disconnectFromHost() instead of close() in my last function.

    When using this method, my program will crash. So i am assuming something weird is happening when I delete the socket, but I have no clue as to what it is.

    Also another note. It does not crash every time on delete. I am stress testing my server with a script that is constantly sending me get requests, and It will crash anywhere between the 2nd and 200th packet. When I see a lower amount of traffic going into the server, I have no problem with the server staying up. Any ideas what the cause of this problem could be?

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTcpSocket/QTcpServer on a heavy load server

    As I understand, you call "deleteLater" then "disconnectFromHost"? You should try to call "disconnectFromHost" and then "deleteLater".
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

Similar Threads

  1. My server (using QTcpServer and QTcpSocket) crashes
    By supergillis in forum Qt Programming
    Replies: 3
    Last Post: 2nd June 2010, 15:32
  2. QTcpServer - get the correct QTcpSocket
    By elcuco in forum Qt Programming
    Replies: 4
    Last Post: 11th May 2010, 09:49
  3. QTcpServer QTcpSocket problem
    By jmsbc in forum Qt Programming
    Replies: 0
    Last Post: 20th November 2009, 17:42
  4. QTcpSocket, QTcpServer problem
    By frido in forum Qt Programming
    Replies: 3
    Last Post: 3rd April 2009, 23:16
  5. QTcpServer & QTcpSocket questions...
    By jxmot in forum Qt Programming
    Replies: 2
    Last Post: 24th April 2008, 21:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.