如何使用套接字编程获取系统日期和时间

How to get the system date and time using socket programming

本文关键字:日期 时间 系统 获取 何使用 套接字 编程      更新时间:2023-10-16

我在这里从客户端向服务器发送简单的消息并且工作正常,但是如果客户端想要获取服务器系统日期和时间怎么办。

Client

主窗口.cpp

Client::Client(QObject* parent): QObject(parent)
{
    connect(&client, SIGNAL(connected()),
    this, SLOT(startTransfer()));
}
Client::~Client()
{
    client.close();
}
void Client::start(QString address, quint16 port)
{
    QHostAddress addr(address);
    client.connectToHost(addr, port);
}
void Client::startTransfer()
{
    client.write("Hi server this is client", 80);
}

我不知道该怎么做,因为我是QT c ++的新手。 提前非常感谢...

server

主.cpp

    #include "mainwindow.h"
  #include <QApplication>

  int main(int argc, char** argv)
  {
    QApplication app(argc, argv);
    Server server;
    return app.exec();
  }

我认为您发布的这部分代码是客户端的,您应该提供从客户端到服务器的请求,就像常见的握手一样。

在服务器端,您以客户端可以识别并发送的特定格式提供日期/时间。 要了解有关客户端/服务器编程的更多信息,请查看本地财富客户端和本地财富服务器示例。

这是您的客户端简单示例:

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

服务器套接字.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;
}

无法从客户端获取服务器系统日期/时间。

您必须将其作为数据包发送。

void Client::startTransfer()
{
    QDateTime dateTime = dateTime.currentDateTime(); 
    QString dateTimeString = dateTime.toString("yyyy-MM-dd_hh-mm-ss");
    // send "dateTimeString" here
}