Thanks a lot for giving so much help. 
I have tried some examples with QUdpSocket, but the data was updated too fast. Is there a way to slow it down? And the example I tried is sending and receiving data with a type like String or char. Is there a way to send and receive integer or float value?
This is the example code.
main.cpp
#include "udp.h"
#include <QApplication>
int main(int argc, char *argv[])
{
UDP w;
UDP Server;
UDP client;
char *data = ba.data();
while (*data) {
client.SayHello(*data);
++data;
}
w.show();
return a.exec();
}
#include "udp.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
UDP w;
UDP Server;
UDP client;
QByteArray ba("Hello world");
char *data = ba.data();
while (*data) {
client.SayHello(*data);
++data;
}
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
udp.cpp
#include "myudp.h"
{
connect(socket,SIGNAL(readyRead()),this,SLOT(ReadyRead()));
}
void MyUDP::SayHello()
{
Data.append("Hello from UDP Land");
}
void MyUDP::ReadyRead()
{
Buffer.resize(socket->pendingDatagramSize());
quint16 senderPort;
socket->readDatagram(Buffer.data(),Buffer.size(),&sender,&senderPort);
qDebug() << "Message from: " << sender.toString();
qDebug() << "Message port: " << senderPort;
qDebug() << "Message: " << Buffer;
}
#include "myudp.h"
MyUDP::MyUDP(QObject *parent) :
QObject(parent)
{
socket = new QUdpSocket(this);
socket->bind(QHostAddress::LocalHost,1234);
connect(socket,SIGNAL(readyRead()),this,SLOT(ReadyRead()));
}
void MyUDP::SayHello()
{
QByteArray Data;
Data.append("Hello from UDP Land");
socket->writeDatagram(Data,QHostAddress::LocalHost,1234);
}
void MyUDP::ReadyRead()
{
QByteArray Buffer;
Buffer.resize(socket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
socket->readDatagram(Buffer.data(),Buffer.size(),&sender,&senderPort);
qDebug() << "Message from: " << sender.toString();
qDebug() << "Message port: " << senderPort;
qDebug() << "Message: " << Buffer;
}
To copy to clipboard, switch view to plain text mode
Bookmarks