簡體   English   中英

運行QTcpServer,我如何正確接收或發送數據?

[英]running a QTcpServer, how do i receive or send data correctly?

我試圖將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();
}

回送示例顯示了更多的讀寫功能。

請注意添加的嵌入式注釋。

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子類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

希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM