Results 1 to 3 of 3

Thread: Strange problems with QUdpSocket

  1. #1
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Strange problems with QUdpSocket

    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:
    Qt Code:
    1. class LogWindow : public QDialog
    2. {
    3. Q_OBJECT
    4.  
    5. private slots:
    6. void processPendingDatagrams ();
    7.  
    8. private:
    9. QUdpSocket *m_pUdpSocket;
    10. }
    To copy to clipboard, switch view to plain text mode 

    CPP File:
    Qt Code:
    1. LogWindow::LogWindow ()
    2. {
    3. m_pUdpSocket = new QUdpSocket (this);
    4. m_pUdpSocket->bind (8085);
    5. connect(m_pUdpSocket, SIGNAL(readyRead ()), this, SLOT (processPendingDatagrams ()));
    6. }
    7.  
    8. void LogWindow::processPendingDatagrams()
    9. {
    10. // Get the messages from the Automation Agents
    11. while (m_pUdpSocket->hasPendingDatagrams()) {
    12. char *data;
    13. QByteArray datagram;
    14. QHostAddress address;
    15. datagram.resize (m_pUdpSocket->pendingDatagramSize ());
    16. m_pUdpSocket->readDatagram (datagram.data (), datagram.size (), &address);
    17. data = datagram.data ();
    18. ---
    19. ---
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. QNativeSocketEngine::hasPendingDatagrams() was called in QAbstractSocket::UnconnectedState
    To copy to clipboard, switch view to plain text mode 

    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!!!
    Last edited by montylee; 13th October 2008 at 14:11.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Strange problems with QUdpSocket

    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::ReuseAddressHint ?)

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

    HTH

  3. The following user says thank you to caduel for this useful post:

    montylee (14th October 2008)

  4. #3
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Talking Re: Strange problems with QUdpSocket

    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.

Similar Threads

  1. Replies: 0
    Last Post: 3rd April 2008, 19:42
  2. [Win32/VC++ 8.0] Strange problems with qrc_*.cpp files
    By mloskot in forum Installation and Deployment
    Replies: 6
    Last Post: 6th March 2006, 10:28

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.