diff --git a/main.cpp b/main.cpp index 103eebc..0eb7644 100644 --- a/main.cpp +++ b/main.cpp @@ -10,7 +10,8 @@ int main(int argc, char* argv[]) client.SayHello(); server.SayHello(); - client.SayMsg("Msg"); + client.SayMsg("MsgC"); + server.SayMsg("MsgS"); return a.exec(); } diff --git a/socket.cpp b/socket.cpp index c55afdb..574558b 100644 --- a/socket.cpp +++ b/socket.cpp @@ -1,10 +1,12 @@ #include "socket.h" +#include -UDP_Socket::UDP_Socket(QObject* parent) : - QObject(parent) +UDP_Socket::UDP_Socket(QObject* parent, quint16 port) : + QObject(parent), + the_port(port) { socket = new QUdpSocket(this); - socket->bind(QHostAddress::LocalHost, 1234); + socket->bind(QHostAddress::LocalHost, the_port); connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead())); } @@ -13,11 +15,19 @@ void UDP_Socket::SayHello() SayMsg("Connected :))"); } -void UDP_Socket::SayMsg(QString a) +void UDP_Socket::SayMsg(QString Msg) { QByteArray Data; - Data.append(a); - socket->writeDatagram(Data, QHostAddress::LocalHost, 1234); + Data.append(Msg); + socket->writeDatagram(Data, QHostAddress::LocalHost, the_port); +} + +QString UDP_Socket::GetTime() +{ + QDateTime current_time_object = QDateTime::currentDateTime(); + QString formatted_time = current_time_object.toString("yyyy/MM/dd - hh:mm:ss"); + + return formatted_time; } void UDP_Socket::readyRead() @@ -28,7 +38,8 @@ void UDP_Socket::readyRead() quint16 sender_port{}; socket->readDatagram(Buffer.data(), Buffer.size(), &sender_addr, &sender_port); - qDebug() << "Message from :" << sender_addr.toString(); + qDebug() << "New Message arrived at" << GetTime(); + qDebug() << "Address :" << sender_addr.toString(); qDebug() << "Port : " << sender_port; - qDebug() << "Content : " << Buffer; + qDebug() << "Content : " << Buffer << '\n'; } diff --git a/socket.h b/socket.h index 7d0dc18..5de209c 100644 --- a/socket.h +++ b/socket.h @@ -9,10 +9,12 @@ class UDP_Socket : public QObject Q_OBJECT public: - explicit UDP_Socket(QObject* parent = nullptr); - + explicit UDP_Socket(QObject* parent = nullptr, quint16 port = 1234); void SayHello(); - void SayMsg(QString a); + void SayMsg(QString Msg); + +private: + QString GetTime(); signals: @@ -21,6 +23,7 @@ public slots: private: QUdpSocket* socket; + const quint16 the_port; }; #endif //UDP_Socket_H