QTcpsocket Server and Clinet Problem
-
Hi All,
I have created one server and one ClientClient ---send Data-----to-----> Server
Server----Read Data----->But Could Not Reply after Prasing
I have taken reference code of fortune server and client:
Server.cpp------------------------------
Server::Server(QWidget *parent): QDialog(parent) , statusLabel(new QLabel) , tcpServer(Q_NULLPTR) , networkSession(0) , blockSize(0) , tcpSocket(new QTcpSocket(this)) { QString ipAddress; statusLabel->setTextInteractionFlags(Qt::TextBrowserInteraction); statusLabel->setText(tr("Opening network session.")); tcpServer = new QTcpServer(this); QHostAddress hostadd("127.0.0.1"); if (!tcpServer->listen(hostadd,8003)) { QMessageBox::critical(this, tr("Fortune Server"),tr("Unable to start the server: %1.") .arg(tcpServer->errorString())); close(); return; } ipAddress = QHostAddress(QHostAddress::LocalHost).toString(); statusLabel->setText(tr("The server is running on\n\nIP: %1\nport: %2\n\n Run the Fortune Client example now.") .arg(ipAddress).arg(tcpServer->serverPort())); qDebug() <<"IP ADD and PORT: "<< ipAddress << tcpServer->serverPort(); fortunes << tr("You've been leading a dog's life. Stay off the furniture.") << tr("You've got to think about tomorrow.") << tr("You will be surprised by a loud noise.") << tr("You will feel hungry again in another hour.") << tr("You might have mail.") << tr("You cannot kill time without injuring eternity.") << tr("Computers are not intelligent. They only think they are."); QPushButton *quitButton = new QPushButton(tr("Quit")); lbl = new QPushButton("lbl"); quitButton->setAutoDefault(false); connect(quitButton, &QAbstractButton::clicked, this, &QWidget::close); connect(tcpServer, &QTcpServer::newConnection, this, &Server::sendWelcomeMessage); typedef void (QAbstractSocket::*QAbstractSocketErrorSignal)(QAbstractSocket::SocketError); connect(tcpSocket, static_cast<QAbstractSocketErrorSignal>(&QAbstractSocket::error),this, &Server::displayError); ///I have created One function as follows for handling NewConnection() void Server::sendWelcomeMessage() { typedef QVector<QTcpSocket*> Connections; // <-- Define this at the class level Connections connections; // This too lbl->setText(QString("%1").arg("Inwelcome")); blockSize = 0; QDataStream out(&block, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_0); out << (quint16)0; out << fortunes.at(qrand() % fortunes.size()); out.device()->seek(0); out << (quint16)(block.size() - sizeof(quint16)); if(tcpServer->hasPendingConnections ()) { lbl->setText(QString("%1").arg("hasPen")); QTcpSocket *client = tcpServer->nextPendingConnection(); QSignalMapper *signalMapper = new QSignalMapper(this); connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater())); connect(client, SIGNAL(readyRead()), signalMapper, SLOT(map())); // Signal mapper is defined in the class definition and created in the constructor, see QSignalMapper. signalMapper->setMapping(client, client); connect(signalMapper,SIGNAL(mapped(QObject *)),this,SLOT(clientReadyRead(QObject *))); client1=client; client->write(block); } } void Server::clientReadyRead(QObject *socket) { lbl->setText(QString("%1").arg("CReadyRead")); QTcpSocket *clientSocket = qobject_cast<QTcpSocket *>(socket); int Bytes(clientSocket->bytesAvailable()); lbl->setText(QString("%1").arg(QString::number(Bytes))); QByteArray bufferData = QByteArray::fromRawData(clientSocket->readAll(),Bytes); for(int tst=0;tst<bufferData.size();tst++){ lbl->setText(QString("%1").arg(bufferData[tst])); int mycounter = bufferData.indexOf("?R",0); bufferData = bufferData.mid(mycounter); lbl->setText(QString("%1").arg(bufferData[0])); } QDataStream out(&bufferData, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_0); out << bufferData; client1->write(block); client1->disconnectFromHost(); delete client1; }
Client.cpp--------------Clinet.cpp This set of code able to receive and send data from NewConnection Slot not from ReadyReady Slot to Server ----------------
void Client::readFortune() { QDataStream in(tcpSocket); in.setVersion(QDataStream::Qt_4_0); this->lbl->setText(QString("%1").arg(QString::number(0))); QByteArray ba("POST / HTTP/1.1\r\nUser-Agent: Mozilla/Firefox3.6.12\r \nContent-Type: application/json\r\n\Content-Length: 48\r\nConnection: Keep-Alive\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: en-IN,*\r\nHost: 127.0.0.1:8003\r\n\r\n""?RegName=NA&RegAdd=NA&EnterData=Status&Value=@@@"); int bytes = tcpSocket->write(ba,ba.size()); this->lbl->setText(QString("%1").arg(QString::number(bytes))); 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, &Client::requestNewFortune); return; } currentFortune = nextFortune; statusLabel->setText(currentFortune); getFortuneButton->setEnabled(true); }
I am sending data from Client and I can able to receive in ReadyRead Slot on ReadyRead Signal Called I have extracted required data but not able to send data from ReadyRead Slot
Data received from Client and NewConnection Slot Called and I am only write data to client from NewConnection but when data received and ReadyRead is called I am not able to send data from there how to send data from readyRead() Slot ---> clientReadyRead(QObject *socket) in Sever.cpp
Thanks and Regards
Praveen -
This is a piece of art
QDataStream out(&bufferData, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_0); out << bufferData;
check out page 361 of http://www.bogotobogo.com/cplusplus/files/c-gui-programming-with-qt-4-2ndedition.pdf