gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:51:32 26 4
gpt4 key购买 nike

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

财富服务器.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();
}

财富线程.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

财富线程.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
}
}

另一种以阻塞方式读取的方法在阻塞财富示例下找到:

    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

希望对您有所帮助。

关于php - 运行 QTcpServer,如何正确接收或发送数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32408948/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com