updated udpclient
This commit is contained in:
parent
bf1c3f40f0
commit
7f3075d061
@ -1,38 +1,22 @@
|
|||||||
|
// main.cpp
|
||||||
|
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <signal.h> // Include the signal header for signal handling
|
|
||||||
#include "udpclient.h"
|
#include "udpclient.h"
|
||||||
|
#include <signal.h> // Include the signal header for handling signals
|
||||||
// Declare a pointer to the UDP client to ensure it's not destroyed prematurely
|
|
||||||
UdpClient* udpClient = nullptr;
|
|
||||||
|
|
||||||
// Signal handler for Ctrl+C (SIGINT)
|
|
||||||
void handleSignal(int signal)
|
|
||||||
{
|
|
||||||
Q_UNUSED(signal);
|
|
||||||
|
|
||||||
// Stop the UDP client gracefully
|
|
||||||
if (udpClient)
|
|
||||||
udpClient->stopClient();
|
|
||||||
|
|
||||||
// Exit the application
|
|
||||||
QCoreApplication::exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
QCoreApplication a(argc, argv);
|
QCoreApplication a(argc, argv);
|
||||||
|
|
||||||
// Create an instance of the UdpClient class
|
// Create and use the UDP client
|
||||||
udpClient = new UdpClient;
|
UdpClient udpClient;
|
||||||
|
udpClient.startClient();
|
||||||
|
|
||||||
// Connect the startClient slot to start the client
|
|
||||||
QObject::connect(udpClient, &UdpClient::startClient, &a, &QCoreApplication::quit);
|
|
||||||
|
|
||||||
// Start the UDP client
|
|
||||||
udpClient->startClient();
|
|
||||||
|
|
||||||
// Set up a signal handler for Ctrl+C (SIGINT)
|
|
||||||
signal(SIGINT, handleSignal);
|
// Set up a signal handler for SIGINT (CTRL+C)
|
||||||
|
signal(SIGINT, [](int) { QCoreApplication::quit(); });
|
||||||
|
|
||||||
return a.exec();
|
return a.exec();
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,37 @@
|
|||||||
|
// udpclient.cpp
|
||||||
|
|
||||||
#include "udpclient.h"
|
#include "udpclient.h"
|
||||||
|
|
||||||
UdpClient::UdpClient(QObject *parent) : QObject(parent)
|
UdpClient::UdpClient(QObject *parent) : QObject(parent)
|
||||||
{
|
{
|
||||||
// Create a UDP socket
|
// Create a QHostAddress for the server's IP address
|
||||||
// udpSocket.bind(QHostAddress::Any, 12345); // Bind to any available port
|
QHostAddress serverAddress("10.0.7.1");
|
||||||
udpSocket.bind(QHostAddress("10.0.7.1"), 0); // Use the desired host address and let the OS choose an available port
|
|
||||||
|
|
||||||
// Set up a signal-slot connection to handle incoming data
|
// Bind the UDP socket to a specific port for receiving data (replace with your desired port)
|
||||||
connect(&udpSocket, &QUdpSocket::readyRead, this, &UdpClient::processPendingDatagrams);
|
udpSocket.bind(QHostAddress::Any, 12345); // Replace 12345 with your desired port
|
||||||
|
|
||||||
|
// Connect the UDP socket's readyRead signal to the receiveData slot
|
||||||
|
connect(&udpSocket, &QUdpSocket::readyRead, this, &UdpClient::receiveData);
|
||||||
|
|
||||||
|
// Set the server's address and port for sending data
|
||||||
|
udpSocket.connectToHost(serverAddress, 12345); // Replace 12345 with the server's port
|
||||||
}
|
}
|
||||||
|
|
||||||
void UdpClient::startClient()
|
void UdpClient::startClient()
|
||||||
{
|
{
|
||||||
qDebug() << "UDP Client is listening for data...";
|
// Start any client functionality here
|
||||||
|
// This method can be used to initialize the client if needed.
|
||||||
// You can add additional client-specific logic here if needed.
|
qDebug() << "UDP Client is listening for data...";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UdpClient::receiveData()
|
||||||
|
{
|
||||||
|
// Process pending datagrams
|
||||||
|
processPendingDatagrams();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void UdpClient::processPendingDatagrams()
|
void UdpClient::processPendingDatagrams()
|
||||||
{
|
{
|
||||||
while (udpSocket.hasPendingDatagrams()) {
|
while (udpSocket.hasPendingDatagrams()) {
|
||||||
|
@ -1,27 +1,25 @@
|
|||||||
|
// udpclient.h
|
||||||
|
|
||||||
#ifndef UDPCLIENT_H
|
#ifndef UDPCLIENT_H
|
||||||
#define UDPCLIENT_H
|
#define UDPCLIENT_H
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QUdpSocket>
|
#include <QUdpSocket>
|
||||||
#include <signal.h> // Include the signal header for signal handling
|
|
||||||
|
|
||||||
class UdpClient : public QObject
|
class UdpClient : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit UdpClient(QObject *parent = nullptr);
|
explicit UdpClient(QObject *parent = nullptr);
|
||||||
|
|
||||||
public slots:
|
|
||||||
void startClient();
|
void startClient();
|
||||||
void stopClient(); // New slot to stop the client gracefully
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QUdpSocket udpSocket;
|
QUdpSocket udpSocket;
|
||||||
|
|
||||||
private slots:
|
public slots:
|
||||||
|
void receiveData();
|
||||||
void processPendingDatagrams();
|
void processPendingDatagrams();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // UDPCLIENT_H
|
#endif // UDPCLIENT_H
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user