난 당신이 해제 코드의이 부분은 클라이언트 측, 당신은 일반적인 손 흔들어 같은 서버에 클라이언트의 요청을 제공해야한다 생각합니다.
서버 측에서는 클라이언트가이를 인식하고 보낼 수있는 특정 형식으로 날짜/시간을 제공합니다. 클라이언트/서버 프로그래밍 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);
}
}
MainWindow를 헤더
#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;
}
당신에게 수행 MEA n * local * 시스템 날짜와 시간? 아니면 * 원격 * 시스템의 것입니까? –
클라이언트가 서버 시간을 가져올 수 없으면 패킷과 함께 보내야합니다.'QDateTime dateTime = dateTime.currentDateTime();'및 QString dateTimeString = dateTime.toString ("yyyy-MM-dd_hh-mm-ss"); – aghilpro
@aghilpro 어떻게 말해 줄 수 있습니까? 나는 당신의 코드로 메시지를 대체하고 서버로 보내는 것 뿐이다. – explorer104