fortuneclient 学习 (客户端接受数据) GIS

1 建立tcpsocket 

  tcpSocket = new QTcpSocket(this);

connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));

2 主动连接tcpsever

tcpSocket->abort();
tcpSocket->connectToHost(hostLineEdit->text(),portLineEdit->text().toInt());

3 读取数据  

void Client::readFortune()
{
//! [9]
QDataStream in(tcpSocket);
in.setVersion(QDataStream::Qt_4_0);

if (blockSize == 0) {
if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
return;
//! [8]

//! [10]
in >> blockSize;
}

if (tcpSocket->bytesAvailable() < blockSize)
return;
//! [10] //! [11]

QString nextFortune;
in >> nextFortune;

if (nextFortune == currentFortune) {
QTimer::singleShot(0, this, SLOT(requestNewFortune()));
return;
}
//! [11]

//! [12]
currentFortune = nextFortune;
//! [9]
statusLabel->setText(currentFortune);
getFortuneButton->setEnabled(true);
}

原文地址:https://www.cnblogs.com/gisbeginner/p/2790659.html