Browse Source

Comply with bio standard

master
h-4nd-h 3 years ago
parent
commit
c1fb57e280
  1. 12
      main.cpp
  2. 33
      socket.cpp
  3. 50
      socket.cpp.autosave
  4. 30
      socket.h
  5. 30
      socket.h.autosave

12
main.cpp

@ -5,13 +5,13 @@ int main(int argc, char* argv[])
{ {
QCoreApplication a(argc, argv); QCoreApplication a(argc, argv);
UDP_Socket server; UdpSocket server;
UDP_Socket client; UdpSocket client;
client.SayHello(); client.sayHello();
server.SayHello(); server.sayHello();
client.SayMsg("MsgC"); client.sayMsg("MsgC");
server.SayMsg("MsgS"); server.sayMsg("MsgS");
return a.exec(); return a.exec();
} }

33
socket.cpp

@ -1,28 +1,33 @@
#include "socket.h" #include "socket.h"
#include <QDateTime> #include <QDateTime>
UDP_Socket::UDP_Socket(QObject* parent, quint16 port) : UdpSocket::UdpSocket(QObject* parent, quint16 port) :
QObject(parent), QObject(parent),
the_port(port) _port(port)
{ {
socket = new QUdpSocket(this); _socket = new QUdpSocket(this);
socket->bind(QHostAddress::LocalHost, the_port); _socket->bind(QHostAddress::LocalHost, port);
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead())); connect(_socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
} }
void UDP_Socket::SayHello() UdpSocket::~UdpSocket()
{ {
SayMsg("Connected :))"); delete _socket;
} }
void UDP_Socket::SayMsg(QString Msg) void UdpSocket::sayHello()
{
sayMsg("Connected :))");
}
void UdpSocket::sayMsg(QString Msg)
{ {
QByteArray Data; QByteArray Data;
Data.append(Msg); Data.append(Msg);
socket->writeDatagram(Data, QHostAddress::LocalHost, the_port); _socket->writeDatagram(Data, QHostAddress::LocalHost, _port);
} }
QString UDP_Socket::GetTime() QString UdpSocket::getTime()
{ {
QDateTime current_time_object = QDateTime::currentDateTime(); QDateTime current_time_object = QDateTime::currentDateTime();
QString formatted_time = current_time_object.toString("yyyy/MM/dd - hh:mm:ss"); QString formatted_time = current_time_object.toString("yyyy/MM/dd - hh:mm:ss");
@ -30,15 +35,15 @@ QString UDP_Socket::GetTime()
return formatted_time; return formatted_time;
} }
void UDP_Socket::readyRead() void UdpSocket::readyRead()
{ {
QByteArray Buffer; QByteArray Buffer;
Buffer.resize(socket->pendingDatagramSize()); Buffer.resize(_socket->pendingDatagramSize());
QHostAddress sender_addr{}; QHostAddress sender_addr{};
quint16 sender_port{}; quint16 sender_port{};
socket->readDatagram(Buffer.data(), Buffer.size(), &sender_addr, &sender_port); _socket->readDatagram(Buffer.data(), Buffer.size(), &sender_addr, &sender_port);
qDebug() << "New Message arrived at" << GetTime(); qDebug() << "New Message arrived at" << getTime();
qDebug() << "Address :" << sender_addr.toString(); qDebug() << "Address :" << sender_addr.toString();
qDebug() << "Port : " << sender_port; qDebug() << "Port : " << sender_port;
qDebug() << "Content : " << Buffer << '\n'; qDebug() << "Content : " << Buffer << '\n';

50
socket.cpp.autosave

@ -0,0 +1,50 @@
#include "socket.h"
#include <QDateTime>
UdpSocket::UdpSocket(QObject* parent, quint16 port) :
QObject(parent),
_port(port)
{
_socket = new QUdpSocket(this);
_socket->bind(QHostAddress::LocalHost, port);
connect(_socket, &QUdpSocket::readyRead, this, &UdpSocket::readyRead);
}
UdpSocket::~UdpSocket()
{
delete _socket;
}
void UdpSocket::sayHello()
{
sayMsg("Connected :))");
}
void UdpSocket::sayMsg(QString Msg)
{
QByteArray Data;
Data.append(Msg);
_socket->writeDatagram(Data, QHostAddress::LocalHost, _port);
}
QString UdpSocket::getTime()
{
QDateTime current_time_object = QDateTime::currentDateTime();
QString formatted_time = current_time_object.toString("yyyy/MM/dd - hh:mm:ss");
return formatted_time;
}
void UdpSocket::readyRead()
{
QByteArray Buffer;
Buffer.resize(_socket->pendingDatagramSize());
QHostAddress sender_addr;
quint16 sender_port;
_socket->readDatagram(Buffer.data(), Buffer.size(), &sender_addr, &sender_port);
qDebug() << "New Message arrived at" << getTime();
qDebug() << "Address :" << sender_addr.toString();
qDebug() << "Port : " << sender_port;
qDebug() << "Content : " << Buffer << '\n';
}

30
socket.h

@ -1,29 +1,29 @@
#ifndef UDP_Socket_H #ifndef UDPSOCKET_H
#define UDP_Socket_H #define UDPSOCKET_H
#include <QObject> #include <QObject>
#include <QUdpSocket> #include <QUdpSocket>
class UDP_Socket : public QObject class UdpSocket : public QObject
{ {
Q_OBJECT Q_OBJECT
public:
explicit UDP_Socket(QObject* parent = nullptr, quint16 port = 1234);
void SayHello();
void SayMsg(QString Msg);
private: private:
QString GetTime(); QUdpSocket* _socket;
const quint16 _port;
signals: QString getTime();
public:
explicit UdpSocket(QObject* parent = nullptr, quint16 port = 1234);
void sayHello();
void sayMsg(QString Msg);
//uncrustify off
public slots: public slots:
//uncrustify on
void readyRead(); void readyRead();
private:
QUdpSocket* socket;
const quint16 the_port;
}; };
#endif //UDP_Socket_H #endif //UDPSOCKET_H

30
socket.h.autosave

@ -0,0 +1,30 @@
#ifndef UDPSOCKET_H
#define UDPSOCKET_H
#include <QObject>
#include <QUdpSocket>
class UdpSocket : public QObject
{
Q_OBJECT
private:
QUdpSocket* _socket;
const quint16 _port;
QString getTime();
public:
explicit UdpSocket(QObject* parent = nullptr, quint16 port = 1234);
~UdpSocket();
void sayHello();
void sayMsg(QString Msg);
//uncrustify off
public slots:
//uncrustify on
void readyRead();
};
#endif //UDPSOCKET_H
Loading…
Cancel
Save