【发布时间】: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();
}
【问题讨论】: