qt - How does QTcpSocket write data to client? -
i wanna make simple qt app can perform task image below:
i have learnt fortune server examples , want app combination of fortune server , fortune client.
dialog.cpp
void dialog::on_btnlisten_clicked(bool checked) { if(checked) { listener = new serversock(this); if(!listener->listen(qhostaddress::localhost,ui->boxport->value())) { qmessagebox::critical(this,tr("error"),tr("cannot listen: %1").arg(listener->errorstring())); ui->btnlisten->setchecked(false); return; } else { qdebug() << "server listening port" << listener->serverport(); ui->btnlisten->settext(tr("listening")); this->setwindowtitle(tr("listening , running")); } } else { listener->close(); listener->deletelater(); ui->tmblisten->settext(tr("listen again")); this->setwindowtitle(tr("taking rest")); } }
serversock.cpp:
#include "serversock.h" serversock::serversock(qobject *parent) : qtcpserver(parent) { } void serversock::incomingconnection(int descriptor) { thread = new threading(descriptor,this); connect(thread,signal(finished()),thread,slot(deletelater())); thread->start(); }
threading.cpp:
#include "threading.h" threading::threading(int descriptor, qobject *parent) : qthread(parent) { this->descriptorsocket = descriptor; } void threading::run() { qdebug() << "thread started..."; socketthread = new qtcpsocket(); if(!socketthread->setsocketdescriptor(this->descriptorsocket)) { emit error(socketthread->error()); return; } connect(socketthread,signal(readyread()),this,slot(readytoread()),qt::directconnection); connect(socketthread,signal(disconnected()),this,slot(unconnected()),qt::directconnection); qdebug() << descriptorsocket << "connected"; exec(); //loop } void threading::readytoread() { qbytearray data = socketthread->readall(); socketout = new qtcpsocket(); socketout->connecttohost("192.168.0.1",8080); if(socketout->waitforconnected(5000)) { qbytearray query("get http:// mysite. com/page http/1.1\r\nhost: anothersite.com\r\r\r\n" + data); //example socketout->write(query); socketout->write("\r\r\r\n"); socketout->waitforbyteswritten(1000); socketout->waitforreadyread(3000); output = socketout->readall(); if(socketout->error() != socketout->unknownsocketerror) { qdebug() << socketout->errorstring(); socketout->disconnectfromhost(); this->quit(); socketthread->abort(); } else { socketout->close(); } } else { qdebug() << "cannot connected"; } if(!output.isnull()) socketthread->write(output); qdebug() << descriptorsocket << " data in: " << data; void threading::unconnected() { qdebug() << "disconnected"; socketthread->abort(); exit(0); deletelater(); }
the problem don't know how return data remote host local client, image below:
it sends request, there no response. how accomplish this?
Comments
Post a Comment