Results 1 to 4 of 4

Thread: QUdpSocket hasPendingDatagrams()

  1. #1
    Join Date
    Jan 2006
    Location
    Minnesota
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default QUdpSocket hasPendingDatagrams()

    I am setting up a QUdpSocket and connecting the readyRead() signal to my readDatagrams() method, however when I get into the method and check the socket with hasPendingDatagrams() it never enters my loop, i.e:

    Qt Code:
    1. void Listener::readDatagrams()
    2. {
    3. while ( m_aSocket->hasPendingDatagrams() )
    4. {
    5. // Never gets here
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

    however, if i skip that loop and just read a datagram from the socket I get my data:

    Qt Code:
    1. QByteArray datagram;
    2. datagram.resize(m_aSocket->pendingDatagramSize());
    3. QHostAddress sender;
    4. quint16 senderPort;
    5.  
    6. m_aSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
    7.  
    8. if ( datagram.size() > 0 )
    9. {
    10. // Gets here without loop
    11. }
    To copy to clipboard, switch view to plain text mode 

    Can anyone help out?

  2. #2
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QUdpSocket hasPendingDatagrams()

    Please,post how and when you call the readDatagram function.

  3. #3
    Join Date
    Jan 2006
    Location
    Minnesota
    Posts
    14
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QUdpSocket hasPendingDatagrams()

    Qt Code:
    1. ControlsListener::ControlsListener(int nPort)
    2. {
    3. m_nPort = nPort;
    4. m_aSocket = new QUdpSocket();
    5. if ( !m_aSocket->bind(m_nPort) )
    6. {
    7. std::cout << "Could not bind udp socket" << std::endl;
    8. }
    9. else
    10. {
    11. std::cout << "Successfully created udp socket on port " << nPort << std::endl;
    12. }
    13. connect(m_aSocket,SIGNAL(readyRead()),
    14. this, SLOT(readPendingDatagrams()));
    15. }
    16.  
    17. ControlsListener::~ControlsListener()
    18. {
    19. delete m_aSocket;
    20. }
    21.  
    22. void ControlsListener::readPendingDatagrams()
    23. {
    24.  
    25. //std::cout << "readPendingDatagrams" << std::endl;
    26. //while ( m_aSocket->hasPendingDatagrams() )
    27. // {
    28. QByteArray datagram;
    29. datagram.resize(m_aSocket->pendingDatagramSize());
    30. QHostAddress sender;
    31. quint16 senderPort;
    32.  
    33. m_aSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
    34.  
    35. if ( datagram.size() > 0 )
    36. {
    37. double * dData = (double*)datagram.data();
    38.  
    39. ByteSwap5(*dData);
    40.  
    41. double dValue = *dData;
    42.  
    43. QString sValue = QString("%1").arg(dValue);
    44. emit messageReceived(sValue);
    45. }
    46. //}
    47. }
    To copy to clipboard, switch view to plain text mode 

    This is the code for the whole listener. I have the while loop commented out right now, but if I uncomment the loop it never enters it. If I uncomment the std::cout << "readPendingDatagrams" << std::end; it gets printed a whole lot, but nothing ever enters the loop.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QUdpSocket hasPendingDatagrams()

    Quote Originally Posted by mbjerkne
    If I uncomment the std::cout << "readPendingDatagrams" << std::end; it gets printed a whole lot, but nothing ever enters the loop.
    Then maybe it's one of these bugs:
    http://www.trolltech.com/developer/t...entry&id=86944
    http://www.trolltech.com/developer/t...ntry&id=104325

Similar Threads

  1. QUdpSocket -- Rebinding with Different Port Numbers
    By swamyonline in forum Qt Programming
    Replies: 0
    Last Post: 22nd January 2009, 13:39
  2. Strange problems with QUdpSocket
    By montylee in forum Qt Programming
    Replies: 2
    Last Post: 14th October 2008, 07:51
  3. UDP - QUdpSocket
    By denwelzie in forum Qt Programming
    Replies: 7
    Last Post: 29th April 2008, 09:02
  4. QUdpSocket binding
    By db in forum Qt Programming
    Replies: 0
    Last Post: 13th March 2008, 11:24
  5. QUdpSocket and ShareAddress
    By mdecandia in forum Qt Programming
    Replies: 5
    Last Post: 26th March 2007, 17:06

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.