You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					51 lines
				
				1.2 KiB
			
		
		
			
		
	
	
					51 lines
				
				1.2 KiB
			| 
								 
											3 years ago
										 
									 | 
							
								#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';
							 | 
						||
| 
								 | 
							
								}
							 |