Results 1 to 7 of 7

Thread: UDP socket doesn't receive packets

  1. #1
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default UDP socket doesn't receive packets

    Hello!

    I'm having troubles receiving UDP broadcast packets to a low port (port number 1001) on a Windows machine.
    I'm using Wireshark to check if the packets arrive on my host successfully and this is the case. Binding the
    socket to the "any" address and the low port returns true. Nevertheless, I tried running my program with
    administrator privileges and it doesn't change anything. The readyRead() signal is never emitted and when I
    manually test for pendingDatagramSize() it always returns -1. Below is the code of my UDPSocket class, that
    works perfectly fine in a different application, where a high port is used. Any ideas what I'm doing wrong?

    Qt Code:
    1. UDPSocket::UDPSocket()
    2. {
    3. portNumber = 1001;
    4. bool yes = udpSocket.bind(portNumber, QUdpSocket::ShareAddress); // bind returns true!
    5. qDebug() << "udp socket bind says" << yes;
    6. }
    7.  
    8. void UDPSocket::startRecording()
    9. {
    10. processPendingDatagrams(); // Clear the socket to make sure the readyRead() signal will be emitted.
    11. data.clear();
    12. connect(&udpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
    13. qDebug() << "udp socket is connected";
    14. }
    15.  
    16. void UDPSocket::stopRecording()
    17. {
    18. udpSocket.disconnect(); // Disconnects the Qt signal, not the socket.
    19. }
    20.  
    21. void UDPSocket::processPendingDatagrams()
    22. {
    23. QHostAddress sender;
    24. quint16 senderPort;
    25.  
    26. qDebug() << "packet arrived!";
    27.  
    28. while (udpSocket.hasPendingDatagrams())
    29. {
    30. datagram.resize(udpSocket.pendingDatagramSize());
    31. udpSocket.readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);
    32. data.append(datagram);
    33. }
    34. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: UDP socket doesn't receive packets

    Check your firewall settings. Usually you shouldn't access ports lower than 1025.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: UDP socket doesn't receive packets

    Oh I didn't think of that one! However, I turned off the firewall and it still doesn't work. In netstat I can see that that something is listening for UDP packets on 0.0.0.0:1001.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: UDP socket doesn't receive packets

    It doesn't mean it is going to be receiving those packets that are trying to arrive there. Why are you trying to use port 1001? You really shouldn't be doing that. Port 1001 is a standard port assigned to a service called "pmake customs server" (whatever that is).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: UDP socket doesn't receive packets

    I have to use port 1001 because the software the sends the packets is not changeable.
    The problem was that it's not a broadcast stream, but a multicast stream. Thanks to Qt 4.8.1 this change is easy to make.

  6. #6
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: UDP socket doesn't receive packets

    I have run into more trouble with assigning a network interface to a multicast group.
    My host has more than one network interface (this is very common). It has a wireless interface and a cable LAN interface.
    The interfaces have different IP addresses and since QUdpSocket::joinMulticastGroup(QHostAddress) assigns
    whichever network interface it seems fit to the desired multicast group, I want to explicitly assign the cable interface.
    So here is what I do:

    Qt Code:
    1. UDPSocket::UDPSocket()
    2. {
    3. udpSocket.bind(1001, QUdpSocket::ShareAddress);
    4.  
    5. // alternative 1:
    6. udpSocket.setMulticastInterface(getNetworkInterfaceByAddress("10.7.7.140"));
    7. udpSocket.joinMulticastGroup(QHostAddress("224.0.0.1"));
    8.  
    9. // alternative 2:
    10. //udpSocket.joinMulticastGroup(QHostAddress("224.0.0.1"), getNetworkInterfaceByAddress("10.7.7.140"));
    11.  
    12. qDebug() << "Multicast group joined on interface:";
    13. QNetworkInterface intf(udpSocket.multicastInterface());
    14. printNetworkInterfaceInfo(intf);
    15. }
    16.  
    17. QNetworkInterface UDPSocket::getNetworkInterfaceByAddress(QString adr)
    18. {
    19. QList<QNetworkInterface> il(QNetworkInterface::allInterfaces());
    20. for (int i = 0; i < il.size(); i++)
    21. {
    22. QList<QNetworkAddressEntry> ade(il[i].addressEntries());
    23. for (int j = 0; j < ade.size(); j++)
    24. {
    25. if (ade[j].ip().toString() == adr)
    26. return il[i];
    27. }
    28. }
    29.  
    30. }
    31.  
    32. void UDPSocket::printNetworkInterfaceInfo(QNetworkInterface ni)
    33. {
    34. qDebug() << ni.index() << ni.humanReadableName();
    35. QList<QNetworkAddressEntry> ade(ni.addressEntries());
    36. for (int j = 0; j < ade.size(); j++)
    37. qDebug() << ade[j].ip();
    38. }
    To copy to clipboard, switch view to plain text mode 


    The QUdpSocket provides two alternatives to assign a specific interface and both of them don't work for me.
    The output of alternative 1 looks right though:

    Multicast group joined on interface:
    10 "Local Area Connection"
    QHostAddress( "FE80::1544:8566:C8B3:A3B5" )
    QHostAddress( "10.7.7.140" )
    The output of alternative 2 is rubbish:

    Multicast group joined on interface:
    0 ""
    In both cases I don't receive the multicast packets. Only when I completely disable the wireless interface such that the OS assigns the correct interface automatically it works and I receive the packets. Any comments on this?

  7. #7
    Join Date
    Nov 2010
    Posts
    122
    Thanks
    62
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Cool Re: UDP socket doesn't receive packets

    I had a similar experience all was well, where the readyRead() signal was not emitted unless I combined the interface with the joinMulticastGroup() method similar to the following example:

    Qt Code:
    1. m_udpSocket = new(std::nothrow)QUdpSocket();
    2.  
    3. if (!m_udpSocket->bind(QHostAddress::AnyIPv4, UDP_DEST_PORT, QUdpSocket::ShareAddress))
    4. {
    5. qDebug() << "MainWindow::MainWindow:: Failed bind of multicast socket";
    6. }
    7.  
    8. QNetworkInterface iface = getNetworkInterfaceByAddress("10.100.0.200");
    9. if (!iface.isValid())
    10. {
    11. qDebug() << "MainWindow::MainWindow:: Multicast socket interface is invalid or does not exist";
    12. }
    13. else
    14. {
    15. m_udpSocket->setMulticastInterface(iface);
    16.  
    17. QHostAddress groupAddress4(QStringLiteral("224.1.2.8"));
    18.  
    19. // NOTE: must provide iface to the following join call or else readyRead() signal will not be emitted
    20. if (!m_udpSocket->joinMulticastGroup(groupAddress4, iface))
    21. {
    22. qDebug() << "MainWindow::MainWindow:: Failed to join multicast group" << groupAddress4.toString() << "on interface" << iface.humanReadableName();
    23. }
    24. else
    25. {
    26. qDebug() << "MainWindow::MainWindow:: Successfully joined multicast group" << groupAddress4.toString() << "on interface" << m_udpSocket->multicastInterface();
    27. printNetworkInterfaceInfo(iface);
    28. }
    29.  
    30. auto fd = m_udpSocket->socketDescriptor();
    31. if (fd != -1)
    32. {
    33. // Make connection for new data received
    34. if (!connect(m_udpSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams())))
    35. {
    36. qDebug() << "MainWindow::MainWindow:: Failed to connect multicast socket readyRead() to handler";
    37. }
    38. else
    39. {
    40. qDebug() << "MainWindow::MainWindow:: Connected multicast socket readyRead() to handler";
    41.  
    42. #ifdef UDP_WRITE_TEST
    43. QByteArray data("THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG'S TAIL");
    44. QNetworkDatagram datagram(data, groupAddress4, UDP_DEST_PORT);
    45. m_udpSocket->writeDatagram(datagram);
    46. #endif
    47. }
    48. }
    49. else
    50. {
    51. qDebug("MainWindow::MainWindow:: Failed to obtain UDP socket descriptor, network sensing is not possible");
    52. }
    53. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. IP packets with header 0x45
    By Pablo220372 in forum Qt Programming
    Replies: 3
    Last Post: 28th June 2011, 00:30
  2. udp broadcast filtering own packets
    By Cruz in forum Qt Programming
    Replies: 0
    Last Post: 27th June 2011, 14:11
  3. qt3 socket receive problems
    By naga1003 in forum Qt Programming
    Replies: 0
    Last Post: 15th October 2009, 05:12
  4. Reading all UDP packets
    By moron in forum Qt Programming
    Replies: 3
    Last Post: 27th May 2008, 16:51
  5. Updation of GUI truncates Packets
    By arunvv in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 11th July 2007, 20:13

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.