2017-10-10 15 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 *ローカル*システムの日付と時刻?または*リモート*システムのもの? –

+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

関連する問題