如何使用套接字编程获取系统日期和时间
我在这里发送从客户端到服务器的简单消息并且它的工作正常,但是如果客户端想要获取服务器系统日期和时间会怎么样。如何使用套接字编程获取系统日期和时间
Client
mainwindow.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 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;
}
@ explorer104如果你想要一个完整的代码,我会给你发一个 – saeed
是的,我需要完整的代码。非常感谢你 – explorer104
@ explorer104我添加了服务器头文件/ cpp文件,它只是一个简单的服务器演示客户端消息请求。如果您有任何其他问题,请不要犹豫,问。 – saeed
有没有办法从客户端获取服务器系统的日期/时间。
你必须将它作为数据包发送。
void Client::startTransfer()
{
QDateTime dateTime = dateTime.currentDateTime();
QString dateTimeString = dateTime.toString("yyyy-MM-dd_hh-mm-ss");
// send "dateTimeString" here
}
@ explorer104你必须发送日期/时间你的自我。首次连接发送日期/时间,服务器和客户端可以同步。或发送数据包请求并获取日期/时间。 – aghilpro
谢谢。现在服务器如何回应我们刚刚发送的数据包给客户端。 – explorer104
@ explorer104客户端发送他的请求,例如'client.write(“Req_Date_time”);'和服务器收到它,并确保它是正确的,并发送日期/时间回到我的答案。 – aghilpro
你MEA n本地*系统日期和时间?还是那个* remote *系统? –
客户端无法获得服务器时间,您必须将其与您的数据包一起发送。'QDateTime dateTime = dateTime.currentDateTime();'和 'QString dateTimeString = dateTime.toString(“yyyy-MM-dd_hh-mm-ss”);' – aghilpro
@aghilpro你能告诉我how.detail代码。我用你的代码替换消息,它只是发送到服务器。 – explorer104