View Full Version : Strange problems with QUdpSocket

13th October 2008, 15: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

private slots:
void processPendingDatagrams ();

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!!!

13th October 2008, 17:38
Are you trying to bind two sockets to the same port? Not sure if that is possible.
(acc. to the docs it is not possible with the default value QUdpSocket::DefaultForPlatform on linux/mac. Try the the bind with 3 args and pass QUdpSocket::ShareAddress|QUdpSocket::ReuseAddressH int ?)

Try to print the return value of bind(), maybe it is false?


14th October 2008, 08:51
thanks for your reply!

Finally got it working :)

Actually the other application i had was using the same port so when i tried to bind to the same port, bind returned an error. I think my application will work on 2 different machines, it's just that on a single machine it won't work properly as i have to bind to the same port.