Browse Source

Added Time Stamp

master
Hatef 3 years ago
parent
commit
0c46ddef30
  1. 3
      main.cpp
  2. 27
      socket.cpp
  3. 9
      socket.h

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

27
socket.cpp

@ -1,10 +1,12 @@
#include "socket.h"
#include <QDateTime>
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';
}

9
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

Loading…
Cancel
Save