【问题标题】:running a QTcpServer, how do i receive or send data correctly?运行 QTcpServer,我如何正确接收或发送数据?
【发布时间】:2015-12-01 06:07:23
【问题描述】:

我正在尝试将 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);

服务器

对话框.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

对话框.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();
}

【问题讨论】:

    标签: php c++ qt sockets tcp


    【解决方案1】:

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

    注意添加的内联 cmets。

    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 Example下可以找到另一种以阻塞方式读取的方法:

        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();
    

    下面这 3 个函数位于 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

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-02-17
      • 2022-01-05
      • 1970-01-01
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多