只要一次只发送一个请求,我的服务器代码就可以正常工作。数据流如下:客户端执行某些操作,例如单击按钮,数据将发送到服务器。类型uint16_t
的标头前置于此数据,以便服务器知道可用于读取此一个请求的字节中有多少字节 - 我认为这个解决方案可以解决我遇到的问题。在服务器上,我将readyRead
信号从QTcpSocket
连接到一个读取标题的函数,如下所示:
void Server::getHeader(){
QTcpSocket *client = qobject_cast<QTcpSocket *>(sender());
if (!client)
return;
forever {
int N = sizeof(uint16_t);
qint64 bytes = client->bytesAvailable();
if (bytes < N) break;
QByteArray buf = client->read(N);
emit hasData(buf, client); //contains the number of bytes in the stream for this request and a reference to the current client
}
}
hasData
信号将触发一个与getHeader
函数执行相同操作的函数,但会等待bytes
个字节数而不是{{1} }字节可用。一旦它看到所需的字节数可用,它会从流中读取许多字节并将它们存储在sizeof(uint16_t)
中,然后将其发送到一个能够理解数据(发生问题的地方)的函数,如这样:
char[]
我将此代码放入try / catch以防止服务器崩溃。它现在并不意味着描述性。如果我把它拿出来,一旦我尝试访问应该在void Server::handleConnection(std::string serverReceiveBuf, QTcpSocket *client){
receivedData data; //a struct that organizes the data to be easily readable by a human
QString stringBuilder;
try{
data.activityID = serverReceiveBuf.at(0);
data.stringCount = serverReceiveBuf.at(1);
int i,
j = 2, //placeholder for next slot to be used in the receive buffer
k = 0;
for (i = 0; i < data.stringCount; i++){ //while there are strings whose lengths we need to document
data.stringLength.insert(i, serverReceiveBuf.at(2+i));
j++;
}
while(k < data.stringCount){ //while there are strings in the buffer to be put into the data structure
i = 0;
while(i < data.stringLength.at(k)){ //while there are characters in the buffer to be built into a string
stringBuilder.append(serverReceiveBuf.at(j));
j++;
i++;
}
//we have assembled a string from chars in the buffer
//now we put them into the struct
data.stringData << stringBuilder;
stringBuilder = ""; //empty string builder for next string.
k++;
}
}
catch(std::exception e){
qDebug() << "[" << currentTime() << "] " << "An undefined buffer error occurred.";
}
//Handle based on activity ID
}
中的数据,服务器就会崩溃 - 从可用字节流中读取的字符串 - 因为它是空的。使用try / catch块,当一个处理器仍在处理时发送的任何请求都将被忽略,并且错误消息将显示在服务器控制台上。
现在这是令人沮丧的部分:当我尝试使用调试器逐步执行代码时,它可以工作。可能是因为在调试器返回处理第二个请求时,请求已完成处理。我认为请求应该排队,但显然不是。有什么特别的东西我必须强制排队数据,以便我可以确保在尝试处理下一个请求之前处理当前请求吗?
答案 0 :(得分:1)
forever
{
int N = sizeof(uint16_t);
qint64 bytes = client->bytesAvailable();
if (bytes < N) break;
QByteArray buf = client->read(N);
emit hasData(buf, client); //contains the number of bytes in the stream for this request and a reference to the current client
}
Qt是事件驱动的,依赖于处理信号的事件,例如你的emit hasData调用。
假设您的服务器收到的数据大于uint16_t的大小(很可能),服务器将陷入永久循环,无法处理自己的事件,例如处理对emit hasData的调用。 / p>
您可以通过在循环底部添加对QApplication :: processEvents()的调用来测试这一点,以确保处理Qt事件。理想情况下,您不应该使用该函数,而是重构代码,以便从函数中正常返回,直到收到更多数据进行处理。