gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 02:09:19 25 4
gpt4 key购买 nike

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

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();
}

最佳答案

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

在服务器端,您以客户端可以识别并发送的特定格式提供日期/时间。看起来更多关于客户端/服务器编程检查 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++ - 如何使用套接字编程获取系统日期和时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46659451/

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