montylee
13th October 2008, 14:04
I am facing a very strange problem with QUdpSocket. I want to listen to a socket port, for e.g. port number 8085 and receive messages on that port. The messages are sent by some other application through the network on the same port.
Now, the strange thing about the problem is:
I am using QUdpSocket in 2 classes in my code. I am able to get messages in one class in my code but in the other class i am not able to receive any messages on the same port.
This is the working code:
Header file:
class LogWindow : public QDialog
{
Q_OBJECT
private slots:
void processPendingDatagrams ();
private:
QUdpSocket *m_pUdpSocket;
}
CPP File:
LogWindow::LogWindow ()
{
m_pUdpSocket = new QUdpSocket (this);
m_pUdpSocket->bind (8085);
connect(m_pUdpSocket, SIGNAL(readyRead ()), this, SLOT (processPendingDatagrams ()));
}
void LogWindow::processPendingDatagrams()
{
// Get the messages from the Automation Agents
while (m_pUdpSocket->hasPendingDatagrams()) {
char *data;
QByteArray datagram;
QHostAddress address;
datagram.resize (m_pUdpSocket->pendingDatagramSize ());
m_pUdpSocket->readDatagram (datagram.data (), datagram.size (), &address);
data = datagram.data ();
---
---
}
}
Now, whenever i send a message from the other application to my Qt application, processPendingDatagrams () is called and i am able to receive the message.
Now, in the 2nd class i have exactly the same code i.e. i have a private member variable of type QUdpSocket * which i am allocating and then binding with socket 8085.
Now, whenever i send messages from the other application to my Qt application, i am not able to receive it in the 2nd class. Basically i want to send a message from the Qt application when a push button is clicked and then receive an acknowledgement from the other application. I am able to send message from the Qt application when i click the button but i am not getting any acknowledgment from the same socket. I even tried calling processPendingDatagrams() in the push button click handler. When i call it explicitly i get the following error when i run the click button code:
QNativeSocketEngine::hasPendingDatagrams() was called in QAbstractSocket::UnconnectedState
So, i think there is something wrong in the Qt socket creation itself. I am able to send messages from the socket but not able to receive any messages inspite of using bind () on the socket.
processPendingDatagrams() is not getting called automatically and if i call it explicitly i get the error given above.
The working code is almost as i mentioned so i am not able to find the source of the problem.
Please help me!!!
Now, the strange thing about the problem is:
I am using QUdpSocket in 2 classes in my code. I am able to get messages in one class in my code but in the other class i am not able to receive any messages on the same port.
This is the working code:
Header file:
class LogWindow : public QDialog
{
Q_OBJECT
private slots:
void processPendingDatagrams ();
private:
QUdpSocket *m_pUdpSocket;
}
CPP File:
LogWindow::LogWindow ()
{
m_pUdpSocket = new QUdpSocket (this);
m_pUdpSocket->bind (8085);
connect(m_pUdpSocket, SIGNAL(readyRead ()), this, SLOT (processPendingDatagrams ()));
}
void LogWindow::processPendingDatagrams()
{
// Get the messages from the Automation Agents
while (m_pUdpSocket->hasPendingDatagrams()) {
char *data;
QByteArray datagram;
QHostAddress address;
datagram.resize (m_pUdpSocket->pendingDatagramSize ());
m_pUdpSocket->readDatagram (datagram.data (), datagram.size (), &address);
data = datagram.data ();
---
---
}
}
Now, whenever i send a message from the other application to my Qt application, processPendingDatagrams () is called and i am able to receive the message.
Now, in the 2nd class i have exactly the same code i.e. i have a private member variable of type QUdpSocket * which i am allocating and then binding with socket 8085.
Now, whenever i send messages from the other application to my Qt application, i am not able to receive it in the 2nd class. Basically i want to send a message from the Qt application when a push button is clicked and then receive an acknowledgement from the other application. I am able to send message from the Qt application when i click the button but i am not getting any acknowledgment from the same socket. I even tried calling processPendingDatagrams() in the push button click handler. When i call it explicitly i get the following error when i run the click button code:
QNativeSocketEngine::hasPendingDatagrams() was called in QAbstractSocket::UnconnectedState
So, i think there is something wrong in the Qt socket creation itself. I am able to send messages from the socket but not able to receive any messages inspite of using bind () on the socket.
processPendingDatagrams() is not getting called automatically and if i call it explicitly i get the error given above.
The working code is almost as i mentioned so i am not able to find the source of the problem.
Please help me!!!