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