简体   繁体   中英

Unable to send message from client to server

I am developing an application where i need to send message from client to server and notify server if there are changes in files on client. I am using QTcpServer and QTcpSocket class in Qt ..I am writing on socket and reading from socket on server side but my read fails.

Can i know how to post a message to server. I need implementation in Qt and C++..

Thanks in advance

Code on client side for writing :

QByteArray block; QDataStream out(&block, 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);

Code on server side: QDataStream in(clientConnection); in.setVersion(QDataStream::Qt_4_0); QString nextFortune; in >> nextFortune; statusLabel->setText(nextFortune); qDebug()<canReadLine(); connect(clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater()));

There is an example of client-server application coming with QT. Take a look at it

PS: format your text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM