QTcpSocket / QTcpServer内存管理/服务器崩溃 [英] QTcpSocket / QTcpServer memory management / server crash

查看:1468
本文介绍了QTcpSocket / QTcpServer内存管理/服务器崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计和制作一个应该能够处理大约每秒100次点击的服务器。我从服务器获得的信息只是HTTP头。基于来自头的信息,它将查询数据库(不同的线程)以获得一些信息,并将最终信息发送回创建输出字符串的QTcpServer,并发回HTTP响应。我有一个大问题,我不能调试。我的代码看起来像这样:

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.

我的问题是我的记忆不断增加,因为我得到更多的命中。我确信这是因为我的QTcpSockets永远不会被删除,因为我只是关闭它们的原因。但是,当我不使用close,并使用disconnectFromHost和disconnect / discardClient插槽/信号我的服务器将崩溃与交通繁忙(没有消息或任何东西,所以我不确定崩溃的确切原因)。以前有人遇到这个问题吗?任何想法。

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.

推荐答案

我有同样的问题!

close()假设要作为对象的构造函数。 (QT manual QTcpSocket ::〜QTcpSocket())
我建议做一个实验:关闭一个套接字,并尝试重新打开它。如果失败,意味着套接字对象被销毁,如果不是意味着对象应该是deletelater()..

close() suppose to be as de-constructor of the object. (QT manual QTcpSocket::~QTcpSocket()) There for I suggest to do an experiment: close a socket, and try to re open it. If it fails, it means the socket Object was destroyed, if not means the object should be deletelater()..

在我的情况下,连接被客户端关闭,并且有disconnect()SIGNAL被调用,它触发对应的方法到你的discardClient()SLOT,其中我deletelater()套接字。
当我压力测试它,它通常崩溃时,我用一个I5笔记本电脑双核600-800连接同时轰炸。它平均每5次崩溃。

In my case the connection is closed by the client, and there for disconnect() SIGNAL is invoked and it trigger the correspond method to your discardClient() SLOT, where I deletelater() the socket. When I stress test it, it usually crash when I bombard it with 600-800 connection simultaneously on an I5 laptop dual core. it crash every 5 times on average.

其他方面它不会。

Gil

这篇关于QTcpSocket / QTcpServer内存管理/服务器崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆