如何使用套接字编程获取系统日期和时间
How to get the system date and time using socket programming
我在这里从客户端向服务器发送简单消息并且工作正常,但是如果客户端想要获取服务器系统日期和时间怎么办。
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();
}
无法从客户端获取服务器系统date/time。
您必须将其作为数据包发送。
void Client::startTransfer()
{
QDateTime dateTime = dateTime.currentDateTime();
QString dateTimeString = dateTime.toString("yyyy-MM-dd_hh-mm-ss");
// send "dateTimeString" here
}
我认为你发布的这部分代码是客户端,你应该像普通握手一样提供从客户端到服务器的请求。
在服务器端,您以客户端可以识别并发送的特定格式提供 date/time。看起来更多关于 client/server 编程检查 Local Fortune Client and Local Fortune Server 示例。
这是您的客户端简单示例:
void Client::startTransfer()
{
client.write("Hi server send time");
client.flush();
client.waitForBytesWritten(300);
}
还有你的服务器端示例:
on 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;
}
我在这里从客户端向服务器发送简单消息并且工作正常,但是如果客户端想要获取服务器系统日期和时间怎么办。
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();
}
无法从客户端获取服务器系统date/time。
您必须将其作为数据包发送。
void Client::startTransfer()
{
QDateTime dateTime = dateTime.currentDateTime();
QString dateTimeString = dateTime.toString("yyyy-MM-dd_hh-mm-ss");
// send "dateTimeString" here
}
我认为你发布的这部分代码是客户端,你应该像普通握手一样提供从客户端到服务器的请求。
在服务器端,您以客户端可以识别并发送的特定格式提供 date/time。看起来更多关于 client/server 编程检查 Local Fortune Client and Local Fortune Server 示例。
这是您的客户端简单示例:
void Client::startTransfer()
{
client.write("Hi server send time");
client.flush();
client.waitForBytesWritten(300);
}
还有你的服务器端示例:
on 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;
}