2017-10-10 68 views
2

我在這裏發送從客戶端到服務器的簡單消息並且它的工作正常,但是如果客戶端想要獲取服務器系統日期和時間會怎麼樣。如何使用套接字編程獲取系統日期和時間

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

你MEA n本地*系統日期和時間?還是那個* remote *系統? –

+0

客戶端無法獲得服務器時間,您必須將其與您的數據包一起發送。'QDateTime dateTime = dateTime.currentDateTime();'和 'QString dateTimeString = dateTime.toString(「yyyy-MM-dd_hh-mm-ss」);' – aghilpro

+0

@aghilpro你能告訴我how.detail代碼。我用你的代碼替換消息,它只是發送到服務器。 – explorer104

回答

1

我覺得這部分代碼,你發佈的是客戶端,你應該像常見的手抖動服務器提供來自客戶端的請求。

在服務器端,您以特定格式提供日期/時間,客戶端可以識別併發送它。看起來更像是客戶/服務器編程檢查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); 
    } 
} 

主窗口標題

#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; 
} 
+0

@ explorer104如果你想要一個完整的代碼,我會給你發一個 – saeed

+0

是的,我需要完整的代碼。非常感謝你 – explorer104

+0

@ explorer104我添加了服務器頭文件/ cpp文件,它只是一個簡單的服務器演示客戶端消息請求。如果您有任何其他問題,請不要猶豫,問。 – saeed

1

有沒有辦法從客戶端獲取服務器系統的日期/時間。

你必須將它作爲數據包發送。

void Client::startTransfer() 
{ 
    QDateTime dateTime = dateTime.currentDateTime(); 
    QString dateTimeString = dateTime.toString("yyyy-MM-dd_hh-mm-ss"); 
    // send "dateTimeString" here 
} 
+0

@ explorer104你必須發送日期/時間你的自我。首次連接發送日期/時間,服務器和客戶端可以同步。或發送數據包請求並獲取日期/時間。 – aghilpro

+0

謝謝。現在服務器如何迴應我們剛剛發送的數據包給客戶端。 – explorer104

+0

@ explorer104客戶端發送他的請求,例如'client.write(「Req_Date_time」);'和服務器收到它,並確保它是正確的,併發送日期/時間回到我的答案。 – aghilpro

相關問題