我正在设计和制作一个服务器,应该能够处理大约每秒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