UDP communication readyread
-
Hi,
I try do UDP communication.Firstly I'm sending to broadcast addres
192.0.1.255
with frame to get IP addres device wihich I should exchange data.
This recive properly IP addres, after them I send data to binded devicesendData
so in ready read I get only one datagram (Len=1).. But rest of data I do not have in my readyread... I should get other data (Len=28,Len=28, Len=74.....) Like below:
my code below:
MyUdpCom::MyUdpCom(QObject *parent) : QObject{parent} { socket = new QUdpSocket(this); socket->bind(QHostAddress::LocalHost, 5010); connect(socket, &QUdpSocket::readyRead, this, &MyUdpCom::readyRead); } void MyUdpCom::bindWithDevice(QByteArray name, QByteArray ipAddr) { QByteArray buff; buff += name; buff += ipAddr; socket->writeDatagram(buff, QHostAddress("192.0.1.255"), 5000); } void MyUdpCom::sendData(QString ipAddr) { socket->writeDatagram(buff, QHostAddress(ipAddr), 5010); } void MyUdpCom::readyRead() { QByteArray buff; buff.resize(socket->pendingDatagramSize()); QHostAddress sender; quint16 senderPort; socket->readDatagram(buff.data(), buff.size(),&sender, &senderPort); qDebug() << "Message from sender: " << sender.toString(); qDebug() << "Message port: " << senderPort; qDebug() << "Message" << buff; if (buff.at(0) == 0x2F){ qDebug() << "We have Ip addres destination"; sendData(sender.toString()); } }
main:
MyUdpCom server; MyUdpCom client; client.bindWithDevice("devMy01","192.0.1.169");
-
@JonB Any idea?
Maybe this is a problem with creating client and server UDP:MyUdpCom server; MyUdpCom client; client.bindWithDevice("devMy01","192.0.1.169");
If I call function inside MyUdpComm, I do twice times ? and this generate problems ... Any idea how to do client and server in one object ?
@JonB mayby the fact that remte device change UDP port when transmit next frames...? From 59185 (3row form log) to 12810(4,5,6...row from log) ?
It can be a problem ? -
Hi,
I try do UDP communication.Firstly I'm sending to broadcast addres
192.0.1.255
with frame to get IP addres device wihich I should exchange data.
This recive properly IP addres, after them I send data to binded devicesendData
so in ready read I get only one datagram (Len=1).. But rest of data I do not have in my readyread... I should get other data (Len=28,Len=28, Len=74.....) Like below:
my code below:
MyUdpCom::MyUdpCom(QObject *parent) : QObject{parent} { socket = new QUdpSocket(this); socket->bind(QHostAddress::LocalHost, 5010); connect(socket, &QUdpSocket::readyRead, this, &MyUdpCom::readyRead); } void MyUdpCom::bindWithDevice(QByteArray name, QByteArray ipAddr) { QByteArray buff; buff += name; buff += ipAddr; socket->writeDatagram(buff, QHostAddress("192.0.1.255"), 5000); } void MyUdpCom::sendData(QString ipAddr) { socket->writeDatagram(buff, QHostAddress(ipAddr), 5010); } void MyUdpCom::readyRead() { QByteArray buff; buff.resize(socket->pendingDatagramSize()); QHostAddress sender; quint16 senderPort; socket->readDatagram(buff.data(), buff.size(),&sender, &senderPort); qDebug() << "Message from sender: " << sender.toString(); qDebug() << "Message port: " << senderPort; qDebug() << "Message" << buff; if (buff.at(0) == 0x2F){ qDebug() << "We have Ip addres destination"; sendData(sender.toString()); } }
main:
MyUdpCom server; MyUdpCom client; client.bindWithDevice("devMy01","192.0.1.169");
@Damian7546
InreadyRead()
you should loop for possible multiple datagrams received:while (socket->hasPendingDatagrams()) { QByteArray buf(socket->pendingDatagramSize(), Qt::Uninitialized); socket->readDatagram(buf.data(), buf.size(), &sender, &senderPort);
Does that resolve your issue?
-
@Damian7546
InreadyRead()
you should loop for possible multiple datagrams received:while (socket->hasPendingDatagrams()) { QByteArray buf(socket->pendingDatagramSize(), Qt::Uninitialized); socket->readDatagram(buf.data(), buf.size(), &sender, &senderPort);
Does that resolve your issue?
@JonB No, I still the same problem.. In my qt app I recived only first and third row form wireshark log published in my first post.
Below updated readyRead() function:
void MyUdpCom::readyRead() { QHostAddress sender; quint16 senderPort; while (socket->hasPendingDatagrams()) { QByteArray buff(socket->pendingDatagramSize(), Qt::Uninitialized); socket->readDatagram(buff.data(), buff.size(), &sender, &senderPort); qDebug() << "Message from sender: " << sender.toString(); qDebug() << "Message port: " << senderPort; qDebug() << "Message" << buff; if (buff.at(0) == 0x2F){ qDebug() << "We have Ip addres destination"; sendData(sender.toString()); } } }
-
@JonB No, I still the same problem.. In my qt app I recived only first and third row form wireshark log published in my first post.
Below updated readyRead() function:
void MyUdpCom::readyRead() { QHostAddress sender; quint16 senderPort; while (socket->hasPendingDatagrams()) { QByteArray buff(socket->pendingDatagramSize(), Qt::Uninitialized); socket->readDatagram(buff.data(), buff.size(), &sender, &senderPort); qDebug() << "Message from sender: " << sender.toString(); qDebug() << "Message port: " << senderPort; qDebug() << "Message" << buff; if (buff.at(0) == 0x2F){ qDebug() << "We have Ip addres destination"; sendData(sender.toString()); } } }
@Damian7546
Well that's already an improvement since you said you only received one datagram and now you receive two.Does your software still work if try commenting out the
sendData(sender.toString());
just to see whether that affects reception?You say " I recived only first and third row form wireshark log" but row #2 has different source/destination addresses from the others. If you mean you also did not receive rows #4 onward then this does not apply.
Other than this I know that UDP does not guarantee delivery so normally missing datagrams can happen. I do not know whether the fact that wireshark shows various packets/datagrams means that no loss should occur.
-
@Damian7546
Well that's already an improvement since you said you only received one datagram and now you receive two.Does your software still work if try commenting out the
sendData(sender.toString());
just to see whether that affects reception?You say " I recived only first and third row form wireshark log" but row #2 has different source/destination addresses from the others. If you mean you also did not receive rows #4 onward then this does not apply.
Other than this I know that UDP does not guarantee delivery so normally missing datagrams can happen. I do not know whether the fact that wireshark shows various packets/datagrams means that no loss should occur.
@JonB
My host IP:192.0.1.169
From my host I write UDP frame to broadcast address:192.0.1.255
and I get recive from192.0.1.167
... row 1 from logNext I write from my host to
192.0.1.167
-sendData(sender.toString());
, where sender.toString() is192.0.1.167
... row 2 from logNext I get data from
192.0.1.167
- row 3 from log...
and after that silence....
Other data 4,5,6,7..rows from log doesn't receive to my app....I can't not call
sendData(sender.toString());
because the device will not send data to my host (3 to 6 rows from log).
In this function I only send data like below:void MyUdpCom::sendData(QString ipAddr) { QByteArray buff; buff += "01202032403420445550" buff += crc(buff); socket->writeDatagram(buff, QHostAddress(ipAddr), 5010); }
-
@JonB
My host IP:192.0.1.169
From my host I write UDP frame to broadcast address:192.0.1.255
and I get recive from192.0.1.167
... row 1 from logNext I write from my host to
192.0.1.167
-sendData(sender.toString());
, where sender.toString() is192.0.1.167
... row 2 from logNext I get data from
192.0.1.167
- row 3 from log...
and after that silence....
Other data 4,5,6,7..rows from log doesn't receive to my app....I can't not call
sendData(sender.toString());
because the device will not send data to my host (3 to 6 rows from log).
In this function I only send data like below:void MyUdpCom::sendData(QString ipAddr) { QByteArray buff; buff += "01202032403420445550" buff += crc(buff); socket->writeDatagram(buff, QHostAddress(ipAddr), 5010); }
@JonB Any idea?
Maybe this is a problem with creating client and server UDP:MyUdpCom server; MyUdpCom client; client.bindWithDevice("devMy01","192.0.1.169");
If I call function inside MyUdpComm, I do twice times ? and this generate problems ... Any idea how to do client and server in one object ?
-
@JonB Any idea?
Maybe this is a problem with creating client and server UDP:MyUdpCom server; MyUdpCom client; client.bindWithDevice("devMy01","192.0.1.169");
If I call function inside MyUdpComm, I do twice times ? and this generate problems ... Any idea how to do client and server in one object ?
@JonB mayby the fact that remte device change UDP port when transmit next frames...? From 59185 (3row form log) to 12810(4,5,6...row from log) ?
It can be a problem ? -