运行QTcpServer,我如何正确接收或发送数据?

时间:2015-09-05 02:31:17

标签: php c++ qt sockets tcp

我正在尝试将char数据从PHP脚本传递到QTcpServer,服务器接收连接,也可以回复,但我无法获取php发送的数据。

我在这里做错了什么?

PHP脚本:

<?php

$addr = gethostbyname("127.0.0.1");

$client = stream_socket_client("tcp://$addr:51235", $errno, $errorMessage);

if ($client === false) {
    throw new UnexpectedValueException("Failed to connect: $errorMessage");
}

$datatopost = 'a';
fwrite($client, $datatopost);
echo stream_get_contents($client);
fclose($client);

服务器

dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QWidget>
#include "fortuneserver.h"

QT_BEGIN_NAMESPACE
class QLabel;
class QPushButton;
QT_END_NAMESPACE

class Dialog : public QWidget
{
    Q_OBJECT

public:
    Dialog(QWidget *parent = 0);

private:
    QLabel *statusLabel;
    QPushButton *quitButton;
    FortuneServer server;
};

#endif

dialog.cpp

#include <QtWidgets>
#include <QtNetwork>

#include <stdlib.h>

#include "dialog.h"
#include "fortuneserver.h"

Dialog::Dialog(QWidget *parent)
    : QWidget(parent)
{
    statusLabel = new QLabel;
    statusLabel->setWordWrap(true);
    quitButton = new QPushButton(tr("Quit"));
    quitButton->setAutoDefault(false);

    if (!server.listen(QHostAddress::LocalHost, 51235)) {
        QMessageBox::critical(this, tr("Threaded Fortune Server"),
                              tr("Unable to start the server: %1.")
                              .arg(server.errorString()));
        close();
        return;
    }

    QString 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(server.serverPort()));

    connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));

    QHBoxLayout *buttonLayout = new QHBoxLayout;
    buttonLayout->addStretch(1);
    buttonLayout->addWidget(quitButton);
    buttonLayout->addStretch(1);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(statusLabel);
    mainLayout->addLayout(buttonLayout);
    setLayout(mainLayout);
    setWindowTitle(tr("Threaded Fortune Server"));
}

fortuneserver.h

#ifndef FORTUNESERVER_H
#define FORTUNESERVER_H

#include <QStringList>
#include <QTcpServer>
#include <QTcpSocket>

class FortuneServer : public QTcpServer
{
    Q_OBJECT

public:
    FortuneServer(QObject *parent = 0);

protected:
    void incomingConnection(qintptr socketDescriptor) Q_DECL_OVERRIDE;

private:
    QStringList fortunes;

};

#endif

fortuneserver.cpp

#include "fortuneserver.h"
#include "fortunethread.h"
#include <stdlib.h>
#include <QDataStream>

FortuneServer::FortuneServer(QObject *parent)
    : QTcpServer(parent)
{
    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.");
}

void FortuneServer::incomingConnection(qintptr socketDescriptor)
{
    qDebug() << "incoming connection";
    QString fortune = fortunes.at(qrand() % fortunes.size());
    FortuneThread *thread = new FortuneThread(socketDescriptor, fortune, this);
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    thread->start();
}

fortunethread.h

#ifndef FORTUNESERVER_H
#define FORTUNESERVER_H

#include <QStringList>
#include <QTcpServer>
#include <QTcpSocket>

class FortuneServer : public QTcpServer
{
    Q_OBJECT

public:
    FortuneServer(QObject *parent = 0);

protected:
    void incomingConnection(qintptr socketDescriptor) Q_DECL_OVERRIDE;

private:
    QStringList fortunes;

};

#endif

fortunethread.cpp

#include "fortunethread.h"

#include <QtNetwork>

FortuneThread::FortuneThread(int socketDescriptor, const QString &fortune, QObject *parent)
    : QThread(parent), socketDescriptor(socketDescriptor), text(fortune)
{

}

void FortuneThread::run()
{
    tcpSocket = new QTcpSocket;
    if (!tcpSocket->setSocketDescriptor(socketDescriptor)) {
        emit error(tcpSocket->error());
        return;
    }

    connect(tcpSocket, SIGNAL(readyRead()), SLOT(newData()));

    QByteArray block;
    QDataStream out(&block, QIODevice::WriteOnly);
    out.setVersion(QDataStream::Qt_4_0);
    out << (quint16)0;
    out << text;
    out.device()->seek(0);
    out << (quint16)(block.size() - sizeof(quint16));

    tcpSocket->write(block);
//    tcpSocket->disconnectFromHost();
//    tcpSocket->waitForDisconnected();
}

void FortuneThread::newData(){
    qDebug() << "readData";

        QByteArray data = tcpSocket->readAll();
        qDebug() << data.data();
}

最后但并非最不重要:main.cpp

#include <QApplication>
#include <QtCore>
#include <stdlib.h>
#include "dialog.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    Dialog dialog;
    dialog.show();
    qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    return app.exec();
}

1 个答案:

答案 0 :(得分:1)

环回示例显示了更多的读写功能。

请注意添加的内嵌注释。

tcpServerConnection = tcpServer.nextPendingConnection();
connect(tcpServerConnection, SIGNAL(readyRead()),// notification of data to read
        this, SLOT(updateServerProgress()));
connect(tcpServerConnection, SIGNAL(error(QAbstractSocket::SocketError)),
        this, SLOT(displayError(QAbstractSocket::SocketError)));

...

void Dialog::updateServerProgress()
{
    bytesReceived += (int)tcpServerConnection->bytesAvailable();// what is available???
    tcpServerConnection->readAll();// This returns a QByteArray which is basically a QString!  save it and do something with it.

    serverProgressBar->setMaximum(TotalBytes);
    serverProgressBar->setValue(bytesReceived);
    serverStatusLabel->setText(tr("Received %1MB")
                               .arg(bytesReceived / (1024 * 1024)));

    if (bytesReceived == TotalBytes) {
        tcpServerConnection->close();
        startButton->setEnabled(true);
#ifndef QT_NO_CURSOR
        QApplication::restoreOverrideCursor();
#endif
    }
}

另一种以阻塞方式读取的方法可以在Blocking Fortune示例中找到:

    if (!socket.waitForConnected(Timeout)) {
        emit error(socket.error(), socket.errorString());
        return;
    }

    while (socket.bytesAvailable() < (int)sizeof(quint16)) {
        if (!socket.waitForReadyRead(Timeout)) {
            emit error(socket.error(), socket.errorString());
            return;
        }
    }

    quint16 blockSize;
    QDataStream in(&socket);// Here it links to the socket to get the info
    in.setVersion(QDataStream::Qt_4_0);
    in >> blockSize;

    while (socket.bytesAvailable() < blockSize) {
        if (!socket.waitForReadyRead(Timeout)) {
            emit error(socket.error(), socket.errorString());
            return;
        }
    }

    mutex.lock();
    QString fortune;
    in >> fortune;// Here the data is changed to a string
    emit newFortune(fortune);

    cond.wait(&mutex);
    serverName = hostName;
    serverPort = port;
    mutex.unlock();

以下这三项功能位于QIODevice下,QAbstractSocket子类,QTCPSocket子类。

http://doc.qt.io/qt-5/qiodevice.html#readAll

http://doc.qt.io/qt-5/qiodevice.html#readyRead

http://doc.qt.io/qt-5/qiodevice.html#waitForReadyRead

有关使用QAbstractSocket的一些细节隐藏在:

http://doc.qt.io/qt-5/qabstractsocket.html#details

并在:

http://doc.qt.io/qt-5/qtcpsocket-members.html

将TCP与QTcpSocketQTcpServer

一起使用

http://doc.qt.io/qt-5/qtnetwork-programming.html#using-tcp-with-qtcpsocket-and-qtcpserver

希望有所帮助。