2010-11-19 210 views
0

我正在开发一个应用程序,我需要从客户端发送消息到服务器,并通知服务器,如果客户端上的文件有变化。 我在Qt中使用QTcpServer和QTcpSocket类..我写在套接字上,并从服务器端的套接字读取,但我的阅读失败。无法发送消息从客户端到服务器

我可以知道如何发布消息到服务器。我需要在Qt和C++实现..

在此先感谢

守则写入客户端:

的QByteArray块; QDataStream输出(&块,QIODevice :: ReadWrite); out.setVersion(QDataStream :: Qt_4_0); // statusLabel-> setText(fortunes); getFortuneButton-> setEnabled(true); //! [4] //! [6] out < <(quint16)0; out < < fortunes.at(qrand()%fortunes.size()); out.device() - > seek(0); out < <(quint16)(block.size() - sizeof(quint16)); //! [6] //! [7]

tcpSocket->connectToHost("172.31.28.140",1331,QIODevice::ReadWrite); 
    qint64 t = tcpSocket->write(block); 

在服务器端代码: QDataStream在(clientConnection); in.setVersion(QDataStream :: Qt_4_0); QString nextFortune; in >> nextFortune; statusLabel-> setText(nextFortune); qDebug()< canReadLine(); (客户端连接,信号(断开连接()), clientConnection,SLOT(deleteLater()));

+1

我们可能需要更多的信息来冒险一个有用的猜测。就像,你用什么代码发送/接收? – 2010-11-19 04:27:21

+0

请重新格式化您的代码。这是不可读的。 – BastiBen 2010-11-19 10:53:32

回答

0

有一个QT客户端 - 服务器应用程序的例子。看看它

PS:格式化您的文本

相关问题