...
QDataStream
QUdpSocket
Code Block |
---|
#include "sender.h"
SenderReceiver::SenderReceiver(QObject *parent) :
QObject(parent)
{
sendUdpSocket = new QUdpSocket(0);
receiveUdpSocket = new QUdpSocket(0);
receiveUdpSocket->bind(45455,QUdpSocket::ShareAddress);
connect(receiveUdpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
}
void SenderReceiver::broadcastDatagram(QVector<float> v)
{
int error,i;
QByteArray datagram;
qDebug() << v;
QDataStream out (&datagram,QIODevice::WriteOnly);
foreach(float f,v)
{
out << f;
}
//kirjoitetaan udp-viesti, mahdollinen virhe kannattaisi tarkastaa
error=sendUdpSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress::Broadcast, 45455);
}
void SenderReceiver::processPendingDatagrams()
{
float f;
//onko vastaanotettu viesteja
while (receiveUdpSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(receiveUdpSocket->pendingDatagramSize());
receiveUdpSocket->readDatagram(datagram.data(), datagram.size());
QDataStream in(datagram);
while (!in.atEnd()){
in >> f;
v2 << f;
}
qDebug() << v2;
emit sendReceivedData(v2);
}
}
|
Code Block |
---|
#include "sender.h"
SenderReceiver::SenderReceiver(QObject *parent) :
QObject(parent)
{
sendUdpSocket = new QUdpSocket(0);
receiveUdpSocket = new QUdpSocket(0);
receiveUdpSocket->bind(45455,QUdpSocket::ShareAddress);
connect(receiveUdpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
}
void SenderReceiver::broadcastDatagram(QVector<float> v)
{
int error,i;
QByteArray datagram;
qDebug() << v;
QDataStream out (&datagram,QIODevice::WriteOnly);
foreach(float f,v)
{
out << f;
}
//kirjoitetaan udp-viesti, mahdollinen virhe kannattaisi tarkastaa
error=sendUdpSocket->writeDatagram(datagram.data(), datagram.size(), QHostAddress::Broadcast, 45455);
}
void SenderReceiver::processPendingDatagrams()
{
float f;
//onko vastaanotettu viesteja
while (receiveUdpSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(receiveUdpSocket->pendingDatagramSize());
receiveUdpSocket->readDatagram(datagram.data(), datagram.size());
QDataStream in(datagram);
while (!in.atEnd()){
in >> f;
v2 << f;
}
qDebug() << v2;
emit sendReceivedData(v2);
}
}
|