UDP communication
-
How to send and receive the data through UDP communication in same .cpp file? I'm using local host for both sender and receiver. I have my code below where all the initialization of variables and declarations made in header file. So, no need to bother about that
#include "sender.h" #include <QtNetwork/QUdpSocket> #include <QMessageBox> #include <exception> #include <QTimer> #include <QObject> Sender::Sender() { send_socket = new QUdpSocket(this); socket = new QUdpSocket(this); connect(send_socket, &QUdpSocket::readyRead, this, &Sender::processResponse); if (send_socket->bind(QHostAddress::LocalHost, 4500)) { qDebug() << "Listening for incoming data on port :"<<4500; } else { qDebug() << "Error binding UDP socket!"; } QTimer* timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &Sender::sendDatagram); timer->start(1000); // Adjust the interval as needed (milliseconds) } void Sender::sendDatagram() { send_socket->writeDatagram(UDP_DATA, receiverAddress, receiverPort); qDebug() << "Sent datagram to" << receiverAddress.toString() << ":" << receiverPort << "-" <<UDP_DATA; //processResponse(); } void Sender::processResponse() { send_socket->bind(QHostAddress("192.168.0.110"),4500); datagram.resize(send_socket->pendingDatagramSize()); QHostAddress senderAddress; quint16 senderPort; socket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort); qDebug() << "Received datagram from" << senderAddress.toString() << ":" << senderPort << "-" << datagram; } } -
How to send and receive the data through UDP communication in same .cpp file? I'm using local host for both sender and receiver. I have my code below where all the initialization of variables and declarations made in header file. So, no need to bother about that
#include "sender.h" #include <QtNetwork/QUdpSocket> #include <QMessageBox> #include <exception> #include <QTimer> #include <QObject> Sender::Sender() { send_socket = new QUdpSocket(this); socket = new QUdpSocket(this); connect(send_socket, &QUdpSocket::readyRead, this, &Sender::processResponse); if (send_socket->bind(QHostAddress::LocalHost, 4500)) { qDebug() << "Listening for incoming data on port :"<<4500; } else { qDebug() << "Error binding UDP socket!"; } QTimer* timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &Sender::sendDatagram); timer->start(1000); // Adjust the interval as needed (milliseconds) } void Sender::sendDatagram() { send_socket->writeDatagram(UDP_DATA, receiverAddress, receiverPort); qDebug() << "Sent datagram to" << receiverAddress.toString() << ":" << receiverPort << "-" <<UDP_DATA; //processResponse(); } void Sender::processResponse() { send_socket->bind(QHostAddress("192.168.0.110"),4500); datagram.resize(send_socket->pendingDatagramSize()); QHostAddress senderAddress; quint16 senderPort; socket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort); qDebug() << "Received datagram from" << senderAddress.toString() << ":" << senderPort << "-" << datagram; } }@Vijaykarthikeyan
My only comment is that you have already boundsend_socketin the constructor. I don't know what you are doing re-bindingsend_socketduringprocessResponse(), which is called fromreadyRead()so socket must be already bound correctly? -
@Vijaykarthikeyan
My only comment is that you have already boundsend_socketin the constructor. I don't know what you are doing re-bindingsend_socketduringprocessResponse(), which is called fromreadyRead()so socket must be already bound correctly?@JonB Ok.. I removed that line and executed. but, the output is like this :
Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#"
Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#"
Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#"
Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#" -
@JonB Ok.. I removed that line and executed. but, the output is like this :
Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#"
Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#"
Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#"
Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#"@Vijaykarthikeyan And? What is wrong with that output?
-
@Vijaykarthikeyan And? What is wrong with that output?
@JonB It is only sending. I have also included the receiving function in the same cpp file. both having common host and port.does it possible to send and receive in same cpp file
-
@JonB It is only sending. I have also included the receiving function in the same cpp file. both having common host and port.does it possible to send and receive in same cpp file
@Vijaykarthikeyan
First thing is to check return result fromsend_socket->writeDatagram(UDP_DATA, receiverAddress, receiverPort);. -
@Vijaykarthikeyan
First thing is to check return result fromsend_socket->writeDatagram(UDP_DATA, receiverAddress, receiverPort);.@JonB this line ```
send_socket->writeDatagram(UDP_DATA, receiverAddress, receiverPort);
qDebug() << "Sent datagram to" << receiverAddress.toString() << ":" << receiverPort << "-" <<UDP_DATA;gives the output continously..obviously its correct: Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#" Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#" Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#" Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#" -
@JonB this line ```
send_socket->writeDatagram(UDP_DATA, receiverAddress, receiverPort);
qDebug() << "Sent datagram to" << receiverAddress.toString() << ":" << receiverPort << "-" <<UDP_DATA;gives the output continously..obviously its correct: Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#" Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#" Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#" Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#"@Vijaykarthikeyan said in UDP communication:
obviously its correct:
How so? Your sending code outputs that unconditionally after
writeDatagram()without checking the result code, so it tells us nothing about whether it was sent. In the time taken for you to ask about this and me to type a reply you could have made the change to test it. Up to you.For sending/receiving to same host you might want to read e.g. https://stackoverflow.com/questions/18584679/testing-udp-on-localhost.
P.S.
I'm not an expert, but maybe you don't receive a datagram on the same socket as you send it on? Maybe you need to connect a second, separate socket to receive? -
@Vijaykarthikeyan said in UDP communication:
obviously its correct:
How so? Your sending code outputs that unconditionally after
writeDatagram()without checking the result code, so it tells us nothing about whether it was sent. In the time taken for you to ask about this and me to type a reply you could have made the change to test it. Up to you.For sending/receiving to same host you might want to read e.g. https://stackoverflow.com/questions/18584679/testing-udp-on-localhost.
P.S.
I'm not an expert, but maybe you don't receive a datagram on the same socket as you send it on? Maybe you need to connect a second, separate socket to receive?@JonB I have used two sockets- send_socket,socket
Also,I couldn't understand the line :
Your sending code outputs that unconditionally after writeDatagram() without checking the result code,
how to check this,i didnt get it
-
@JonB I have used two sockets- send_socket,socket
Also,I couldn't understand the line :
Your sending code outputs that unconditionally after writeDatagram() without checking the result code,
how to check this,i didnt get it
@Vijaykarthikeyan said in UDP communication:
@JonB I have used two sockets- send_socket,socket
The
socket(member?) variable you declare inSender::Sender()and use inprocessResponse()is not bound/connected, it is simply a default empty socket. Why should it receive anything you send? Also you callsend_socket->pendingDatagramSize()butsocket->readDatagram().If it still doesn't work, maybe try getting/connecting the second socket via
connectToHost()?Also,I couldn't understand the line :
Your sending code outputs that unconditionally after writeDatagram() without checking the result code,
how to check this,i didnt get it
Look at the documentation for
writeDatagram(). The reason it returns a result is for people to check it.... Same forreadDatagram(). -
@Vijaykarthikeyan said in UDP communication:
@JonB I have used two sockets- send_socket,socket
The
socket(member?) variable you declare inSender::Sender()and use inprocessResponse()is not bound/connected, it is simply a default empty socket. Why should it receive anything you send? Also you callsend_socket->pendingDatagramSize()butsocket->readDatagram().If it still doesn't work, maybe try getting/connecting the second socket via
connectToHost()?Also,I couldn't understand the line :
Your sending code outputs that unconditionally after writeDatagram() without checking the result code,
how to check this,i didnt get it
Look at the documentation for
writeDatagram(). The reason it returns a result is for people to check it.... Same forreadDatagram().@JonB ok..I have changed the mistakes..still receiving function is not working
#include "sender.h" #include <QtNetwork/QUdpSocket> #include <QMessageBox> #include <exception> #include <QTimer> #include <QObject> Sender::Sender() { send_socket = new QUdpSocket(this); socket = new QUdpSocket(this); send_socket->bind(QHostAddress("192.168.0.110"),500); socket->bind(QHostAddress("192.168.0.110"),4500); connect(socket, &QUdpSocket::readyRead, this, &Sender::processResponse); QTimer* timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &Sender::sendDatagram); timer->start(1000); // Adjust the interval as needed (milliseconds) } void Sender::sendDatagram() { send_socket->writeDatagram(UDP_DATA, receiverAddress, receiverPort); qDebug() << "Sent datagram to" << receiverAddress.toString() << ":" << receiverPort << "-" <<UDP_DATA; } void Sender::processResponse() { if (socket->hasPendingDatagrams()) { datagram.resize(socket->pendingDatagramSize()); QHostAddress senderAddress; quint16 senderPort; socket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort); QString data = QString::fromUtf8(datagram); qDebug() << "Received datagram from" << senderAddress.toString() << ":" << senderPort << "-" << data; } else { qDebug() << "No pending datagrams."; } } -
@Vijaykarthikeyan said in UDP communication:
@JonB I have used two sockets- send_socket,socket
The
socket(member?) variable you declare inSender::Sender()and use inprocessResponse()is not bound/connected, it is simply a default empty socket. Why should it receive anything you send? Also you callsend_socket->pendingDatagramSize()butsocket->readDatagram().If it still doesn't work, maybe try getting/connecting the second socket via
connectToHost()?Also,I couldn't understand the line :
Your sending code outputs that unconditionally after writeDatagram() without checking the result code,
how to check this,i didnt get it
Look at the documentation for
writeDatagram(). The reason it returns a result is for people to check it.... Same forreadDatagram().@JonB On looking through documentation writeDatagram() returns the number of bytes it sent..
on my case,the number of bytes returned every time are 30:
30
Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#"
30
Sent datagram to "192.168.0.110" : 4500 - "0,S,M,A,-1,-1,S,S,4000,10000,#"
30 -
@JonB ok..I have changed the mistakes..still receiving function is not working
#include "sender.h" #include <QtNetwork/QUdpSocket> #include <QMessageBox> #include <exception> #include <QTimer> #include <QObject> Sender::Sender() { send_socket = new QUdpSocket(this); socket = new QUdpSocket(this); send_socket->bind(QHostAddress("192.168.0.110"),500); socket->bind(QHostAddress("192.168.0.110"),4500); connect(socket, &QUdpSocket::readyRead, this, &Sender::processResponse); QTimer* timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &Sender::sendDatagram); timer->start(1000); // Adjust the interval as needed (milliseconds) } void Sender::sendDatagram() { send_socket->writeDatagram(UDP_DATA, receiverAddress, receiverPort); qDebug() << "Sent datagram to" << receiverAddress.toString() << ":" << receiverPort << "-" <<UDP_DATA; } void Sender::processResponse() { if (socket->hasPendingDatagrams()) { datagram.resize(socket->pendingDatagramSize()); QHostAddress senderAddress; quint16 senderPort; socket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort); QString data = QString::fromUtf8(datagram); qDebug() << "Received datagram from" << senderAddress.toString() << ":" << senderPort << "-" << data; } else { qDebug() << "No pending datagrams."; } }@Vijaykarthikeyan
You have chosen not to verify thewriteDatagram()(and for that matter thereadDatagram(). Nor do you check thebind()results. I also suggested you try withconnectToHost(). I leave this to you now, you have your own way of developing. -
@Vijaykarthikeyan
You have chosen not to verify thewriteDatagram()(and for that matter thereadDatagram(). Nor do you check thebind()results. I also suggested you try withconnectToHost(). I leave this to you now, you have your own way of developing.@JonB Where I have chosen? You said to check the return of writeDatagram().. I thought it is debugging the return byte. How could you tell me that i have chosen not to verify? I have gone through all that documentation ,tried with different ip's. Also they have came out well.. I came here to seek help to clear my doubt? how could you say like this? If the suggestion is clear and I didn't go for that..it is agreeable. But, I have tried all the ways you have said and you are accusing me I'm not interested? Is that the right way of concluding the topic
-
@JonB Where I have chosen? You said to check the return of writeDatagram().. I thought it is debugging the return byte. How could you tell me that i have chosen not to verify? I have gone through all that documentation ,tried with different ip's. Also they have came out well.. I came here to seek help to clear my doubt? how could you say like this? If the suggestion is clear and I didn't go for that..it is agreeable. But, I have tried all the ways you have said and you are accusing me I'm not interested? Is that the right way of concluding the topic
When you want to write to a socket there is no need for bind() as written in the documentation. There is even a big red note about it. And the documentation also show you what to do to receive an udp packet. And moreover you can find two examples there which show your requested functionality.
-
When you want to write to a socket there is no need for bind() as written in the documentation. There is even a big red note about it. And the documentation also show you what to do to receive an udp packet. And moreover you can find two examples there which show your requested functionality.
@Christian-Ehrlicher ok ...i modified it..removed that binding line for sender. on looking through documentation,i have checked the return statement for both writeDatagram() and readDatagram().The former one returns 30 bytes but the latter one returns -1 which indicates it is not receiving bytes. I think the reson why it is not calling the receving function might be due to the connect function which connects readyRead() with receving function. but upon debugging that also,it gives ture..
where can i found the error?
-
@Christian-Ehrlicher ok ...i modified it..removed that binding line for sender. on looking through documentation,i have checked the return statement for both writeDatagram() and readDatagram().The former one returns 30 bytes but the latter one returns -1 which indicates it is not receiving bytes. I think the reson why it is not calling the receving function might be due to the connect function which connects readyRead() with receving function. but upon debugging that also,it gives ture..
where can i found the error?
@Vijaykarthikeyan said in UDP communication:
returns -1 which indicates it is not receiving bytes. I think the reson why it is not calling the receving function might be due to
This does not match since your code correctly calls readDatagram() when the receive function was called due to readyRead.
Post yor actual code and a proper error description./edit: also try out the examples and make sure the windows firewall does not block the udp packets
-
@Vijaykarthikeyan said in UDP communication:
returns -1 which indicates it is not receiving bytes. I think the reson why it is not calling the receving function might be due to
This does not match since your code correctly calls readDatagram() when the receive function was called due to readyRead.
Post yor actual code and a proper error description./edit: also try out the examples and make sure the windows firewall does not block the udp packets
@Christian-Ehrlicher i have turned off all the firewall securities,sir..examples in qt documentation explains separate separate modules.Here is the modified code:
#include "sender.h" #include <QtNetwork/QUdpSocket> #include <QMessageBox> #include <exception> #include <QTimer> #include <QObject> Sender::Sender() { send_socket = new QUdpSocket(this); socket = new QUdpSocket(this); qDebug()<<socket->bind(QHostAddress::Any,55000); connect(socket, &QUdpSocket::readyRead, this, &Sender::processResponse); QTimer* timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &Sender::sendDatagram); timer->start(1000); // Adjust the interval as needed (milliseconds) } void Sender::sendDatagram() { qDebug()<<send_socket->writeDatagram(UDP_DATA, receiverAddress, receiverPort); qDebug() << "Sent datagram to" << receiverAddress.toString() << ":" << receiverPort << "-" <<UDP_DATA; //processResponse(); } void Sender::processResponse() { if (socket->state() == QUdpSocket::BoundState) { while (socket->hasPendingDatagrams()) { datagram.resize(socket->pendingDatagramSize()); QHostAddress senderAddress; quint16 senderPort; int bytesRead = socket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort); QString data = QString::fromUtf8(datagram); qDebug() << "Received datagram from" << senderAddress.toString() << ":" << senderPort << "-" << data; if (bytesRead == -1) { qDebug() << "Failed to read datagram!"; } else { QString data = QString::fromUtf8(datagram); qDebug() << "Received datagram from" << senderAddress.toString() << ":" << senderPort << "-" << data; } } } } -
@Christian-Ehrlicher i have turned off all the firewall securities,sir..examples in qt documentation explains separate separate modules.Here is the modified code:
#include "sender.h" #include <QtNetwork/QUdpSocket> #include <QMessageBox> #include <exception> #include <QTimer> #include <QObject> Sender::Sender() { send_socket = new QUdpSocket(this); socket = new QUdpSocket(this); qDebug()<<socket->bind(QHostAddress::Any,55000); connect(socket, &QUdpSocket::readyRead, this, &Sender::processResponse); QTimer* timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &Sender::sendDatagram); timer->start(1000); // Adjust the interval as needed (milliseconds) } void Sender::sendDatagram() { qDebug()<<send_socket->writeDatagram(UDP_DATA, receiverAddress, receiverPort); qDebug() << "Sent datagram to" << receiverAddress.toString() << ":" << receiverPort << "-" <<UDP_DATA; //processResponse(); } void Sender::processResponse() { if (socket->state() == QUdpSocket::BoundState) { while (socket->hasPendingDatagrams()) { datagram.resize(socket->pendingDatagramSize()); QHostAddress senderAddress; quint16 senderPort; int bytesRead = socket->readDatagram(datagram.data(), datagram.size(), &senderAddress, &senderPort); QString data = QString::fromUtf8(datagram); qDebug() << "Received datagram from" << senderAddress.toString() << ":" << senderPort << "-" << data; if (bytesRead == -1) { qDebug() << "Failed to read datagram!"; } else { QString data = QString::fromUtf8(datagram); qDebug() << "Received datagram from" << senderAddress.toString() << ":" << senderPort << "-" << data; } } } }@Vijaykarthikeyan said in UDP communication:
examples in qt documentation explains separate separate modules.
I don't see what this should matter. You should test if it works. Merging the two functions into one class (for whatever abstruse reason) is a no brainer afterwards.
Why do you use port 55000 and QHostAddress::Any on the one side and receiver_port and receiver_host on the other?
Why do you check for a socket state in your slot? No example does this.
How does your debug output look like now?
-
@Vijaykarthikeyan said in UDP communication:
examples in qt documentation explains separate separate modules.
I don't see what this should matter. You should test if it works. Merging the two functions into one class (for whatever abstruse reason) is a no brainer afterwards.
Why do you use port 55000 and QHostAddress::Any on the one side and receiver_port and receiver_host on the other?
Why do you check for a socket state in your slot? No example does this.
How does your debug output look like now?
@Christian-Ehrlicher ok.. now, I corrected my code and finally it works. It's because the mismatch between the Ip address and ports.. I have to use 5 digit port number instead of 4..
-
V Vijaykarthikeyan has marked this topic as solved on