我正在設計和製作一個服務器,應該能夠處理大約每秒100次點擊。我從服務器獲取的信息只是HTTP頭。根據頭信息,它將查詢一個數據庫(不同的線程)的一些信息,並將最終的信息發送回QTcpServer,它創建一個輸出字符串,併發回一個HTTP響應。我遇到了一個很大的問題,我無法調試。我的代碼類似於此:QTcpSocket/QTcpServer內存管理/服務器崩潰
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;
}
我具有我有一個想法是什麼一個主要問題,但無法找到一個解決方案來解決這個問題。
我的問題是我的記憶力不斷增加,因爲我得到更多的命中。我確信這是我的QTcpSockets永遠不會被刪除,因爲我只是關閉它們。但是,當我不使用關閉,並使用disconnectFromHost和斷開連接/ discardClient插槽/信號我的服務器將崩潰與交通繁忙(沒有消息或任何東西,所以我不知道崩潰的確切原因)。有沒有人遇到過這個問題?任何想法。
嘗試創建一個最小的工作示例,在重負載時產生崩潰。如果這是由Qt事件系統中的競爭條件引起的,我不會太驚訝。 – scai 2012-08-03 04:36:29