hi,all.I want to realize the communication between the server and client,but I have tested that the client can't send data,although I have thought for quite a while,I couldn't find the reason,because I have studied to use it for a short time.The send function for client and receive function for server are as following:
void Client::sendFortune(){
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_2);
out << (quint16)0;
out << fortunes.at(qrand() % fortunes.size());
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
tcpSocket->write(block);
}
void Server::readFortune(){
QDataStream in(tcpsocket);
in.setVersion(QDataStream::Qt_4_2);
if (blockSize == 0) {
if (tcpsocket->bytesAvailable() < (int)sizeof(quint16))
return;
in >> blockSize;
}
if (tcpsocket->bytesAvailable() < blockSize)
return;
QString nextFortune;
in >> nextFortune;
if (nextFortune == currentFortune) {
QTimer::singleShot(0, this, SLOT(requestNewConnect()));
return;
}
currentFortune = nextFortune;
}
Anyone who knows why,please tell me,thank you very much!
Bookmarks