は、私はあなたがリリースされたコードのこの部分は、クライアント側は、あなたが一般的な手が震えのようなサーバにクライアントからの要求を提供すべきだと思います。
サーバー側では、クライアントが認識して送信できる特定の形式で日付/時刻を指定します。クライアント/サーバプログラミングチェックLocal Fortune ClientとLocal Fortune Serverの例のように見えます。
は、ここでは、クライアント側の簡単な例です:
void Client::startTransfer()
{
client.write("Hi server send time");
client.flush();
client.waitForBytesWritten(300);
}
と、サーバー側例:サーバーで
newconnection
上のスロットは、クライアントのメッセージのようなスロットにクライアントデータを接続します。
void ServerSocket::newConnection()
{
QTcpSocket *clientsocket = mserver->nextPendingConnection();
connect(clientsocket , SIGNAL(readyRead()) , this , SLOT(clientMessage()));
}
とスロットクライアントメッセージでそれに応えるここ
void ServerSocket::clientMessage()
{
QTcpSocket* client = (QTcpSocket*)sender();
QString message(client->readAll());
if (message == "Hi server send time")
{
client->write(QDateTime::currentDateTimeUtc().toString().toLatin1());
client->flush();
client->waitForBytesWritten(300);
}
}
は、要求された完全なコードです:
servesocket.hは
#ifndef SERVERSOCKET_H
#define SERVERSOCKET_H
#include <QObject>
#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>
class ServerSocket : public QObject
{
Q_OBJECT
public:
explicit ServerSocket(QObject *parent = nullptr);
QTcpServer *mserver;
signals:
public slots:
void newConnection();
void clientMessage();
};
#endif // SERVERSOCKET_H
serversocket.cpp
#include "serversocket.h"
#include <QDateTime>
ServerSocket::ServerSocket(QObject *parent) : QObject(parent)
{
mserver = new QTcpServer(this);
mserver->connect(mserver , SIGNAL(newConnection()) , this , SLOT(newConnection()));
if(!mserver->listen(QHostAddress::Any , 1234))
{
qDebug() << "Server initilize failed";
}
}
void ServerSocket::newConnection()
{
QTcpSocket *clientsocket = mserver->nextPendingConnection();
connect(clientsocket , SIGNAL(readyRead()) , this , SLOT(clientMessage()));
}
void ServerSocket::clientMessage()
{
QTcpSocket* client = (QTcpSocket*)sender();
QString message(client->readAll());
if (message == "Hi server send time")
{
client->write(QDateTime::currentDateTimeUtc().toString().toLatin1());
client->flush();
client->waitForBytesWritten(300);
}
}
メインウィンドウのヘッダー
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QLineEdit>
#include <QMainWindow>
#include <QSerialPort>
#include "serversocket.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);//:QMainWindow(parent)
~MainWindow();
private slots:
private:
Ui::MainWindow *ui;
ServerSocket * server;
};
#endif // MAINWINDOW_H
メインウィンドウのcpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
server = new ServerSocket();
}
MainWindow::~MainWindow()
{
delete ui;
}
あなたMEAを実行します。 n *ローカル*システムの日付と時刻?または*リモート*システムのもの? –
クライアントがサーバー時間を取得できなかった場合は、パケットと共に送信する必要があります。'QDateTime dateTime = dateTime.currentDateTime();'と QString dateTimeString = dateTime.toString( "yyyy-MM-dd_hh-mm-ss"); ' – aghilpro
@aghilpro how.detailコードを教えてください。私はあなたのコードでメッセージを置き換え、それだけでサーバーに送信します。 – explorer104