c++ - 如何使用socket编程获取系统日期和时间

标签 c++ qt qt4

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

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

main.cpp

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



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

最佳答案

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

在服务器端,您以客户端可以识别并发送的特定格式提供日期/时间。看起来更多关于客户端/服务器编程检查Local Fortune ClientLocal 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);
    }
}

MainWindow header

#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

MainWindow cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    server = new ServerSocket();
}

MainWindow::~MainWindow()
{
    delete ui;
}

关于c++ - 如何使用socket编程获取系统日期和时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46659451/

相关文章:

c++ - 将 typeid 转换为命名空间以进行静态成员访问 (C++)

c++ - 两次设置 QApplication::style 后程序崩溃

qt - 如何设置 QMenu 中包含的 QActions 的可访问名称

qt - 在 Qt 中创建自定义 Qwidget?

c++ - 如何从另一个 lambda 函数调用 lambda 函数

c++ - 不带 'GetString()' 的 CString 到 std::string 的转换

c++ - 为什么 Qt Creator 在包含的路径中找不到包含的 header - 即使 qmake 能够找到它们

c++ - 将误差条添加到 VTK 二维散点图

algorithm - 布置重叠的矩形

c++ - 为了让 QtSql 正常运行,我需要哪些 .dll 文件?