drinkwater
13th August 2012, 20:34
Hi,
This should be a simple problem for those QT experts out there. I'm trying to send and receive UDP datagrams using a single QT application (don't ask why). I thought this would work just like it would with two applications, which have worked for me in the past. I have two threads running. The first sends out the message and the second thread reads in the message. The application successfully sends the message out on the network b/c I've used the QT broadcast receiver application to see if it can read the message and it does. But when I put this same code into the same application as the sender it doesn't work. I get no errors when i compile nor run the program...just nothing is received. I've inserted a qDebug() statement that tells me when a datagram is ready and I never see that text. So, I'm assuming that the message is never received. Now, I did change the port number between the sending and receiving code segments b/c I get error messages with binding to the same port. It is the same application so it perhaps makes sense that I can't bind to the same port, but with implementing the same thing with two applications (the QT examples do this as well), I use the same port number for both sending and receiving. I'm thinking there is a simple solution here b/c I'm sending and receiving on the same machine and same application. Anyway, thanks in advance for any help.
1st thread code segment:
QByteArray message;
message.append(msg);
//qDebug() << message;
QByteArray datagram = message;
udpSocket->writeDatagram(datagram.data(), datagram.size(),
QHostAddress::Broadcast, 45454); //I've tried using "localhost" here as well
2nd thread code segment:
receiving_network::receiving_network()
{
rcvSocket = new QUdpSocket(this);
rcvSocket->bind(45453, QUdpSocket::ShareAddress);
connect(rcvSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
}
void receiving_network::processPendingDatagrams(){
qDebug() << "Processing datagram";
while (rcvSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(rcvSocket->pendingDatagramSize());
rcvSocket->readDatagram(datagram.data(), datagram.size());
received_data = datagram.data();
}
}
This should be a simple problem for those QT experts out there. I'm trying to send and receive UDP datagrams using a single QT application (don't ask why). I thought this would work just like it would with two applications, which have worked for me in the past. I have two threads running. The first sends out the message and the second thread reads in the message. The application successfully sends the message out on the network b/c I've used the QT broadcast receiver application to see if it can read the message and it does. But when I put this same code into the same application as the sender it doesn't work. I get no errors when i compile nor run the program...just nothing is received. I've inserted a qDebug() statement that tells me when a datagram is ready and I never see that text. So, I'm assuming that the message is never received. Now, I did change the port number between the sending and receiving code segments b/c I get error messages with binding to the same port. It is the same application so it perhaps makes sense that I can't bind to the same port, but with implementing the same thing with two applications (the QT examples do this as well), I use the same port number for both sending and receiving. I'm thinking there is a simple solution here b/c I'm sending and receiving on the same machine and same application. Anyway, thanks in advance for any help.
1st thread code segment:
QByteArray message;
message.append(msg);
//qDebug() << message;
QByteArray datagram = message;
udpSocket->writeDatagram(datagram.data(), datagram.size(),
QHostAddress::Broadcast, 45454); //I've tried using "localhost" here as well
2nd thread code segment:
receiving_network::receiving_network()
{
rcvSocket = new QUdpSocket(this);
rcvSocket->bind(45453, QUdpSocket::ShareAddress);
connect(rcvSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
}
void receiving_network::processPendingDatagrams(){
qDebug() << "Processing datagram";
while (rcvSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(rcvSocket->pendingDatagramSize());
rcvSocket->readDatagram(datagram.data(), datagram.size());
received_data = datagram.data();
}
}