QTcpSocket如何将数据写入客户端?

时间:2014-04-06 12:13:06

标签: qt qtcpsocket

我想制作一个简单的Qt应用程序,它可以执行如下图所示的任务:

enter image description here

我已经学会了Fortune Server的例子,实际上我希望这个应用程序就像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 is listening to port" << listener->serverPort();
            ui->btnListen->setText(tr("Listening"));
            this->setWindowTitle(tr("Listening and Running"));
        }
    }
    else
    {
        listener->close();
        listener->deleteLater();

        ui->tmbListen->setText(tr("Listen again"));
        this->setWindowTitle(tr("Taking a 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 is 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();
}

问题是我不知道如何将数据从远程主机返回到本地客户端,如下图所示:

enter image description here

它发送请求,但没有响应。如何做到这一点?

0 个答案:

没有答案