Results 1 to 2 of 2

Thread: Qt multicast works from Windows to Mac but not another around

  1. #1
    Join Date
    Jan 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt multicast works from Windows to Mac but not another around

    I use an experimental project to test Qt multicast. It's acting like a simple chat program within a local network. I'm able to send messages from Windows to Mac, but not from Mac to Windows. Please see the comment in Multicast.cpp.

    Both of them join a predefined group. I used some hard coded IP to find the corresponding interface to bind to. I have also checked the multicast groups on both platforms using netstat and netsh and it show the predefined group on both sides.

    It's not a firewall issue, since I turned it off and the issue persisted.

    Qt version:5.5

    Any suggestion what I should do next to diagnose this issue?

    netsh output:

    Qt Code:
    1. C:\Users\Jerry>netsh interface ip show joins
    2.  
    3. Interface 1: Loopback Pseudo-Interface 1
    4.  
    5. Scope References Last Address
    6. ---------- ---------- ---- ---------------------------------
    7. 0 1 Yes 224.0.0.251
    8. 0 4 Yes 239.255.255.250
    9.  
    10. Interface 4: Ethernet
    11.  
    12. Scope References Last Address
    13. ---------- ---------- ---- ---------------------------------
    14. 0 0 Yes 224.0.0.1
    15. 0 2 Yes 224.0.0.251
    16. 0 1 Yes 224.0.0.252
    17. 0 0 Yes 239.255.43.21
    18. 0 4 Yes 239.255.255.250
    19.  
    20. Interface 8: Local Area Connection
    21.  
    22. Scope References Last Address
    23. ---------- ---------- ---- ---------------------------------
    24. 0 0 Yes 224.0.0.1
    25. 0 2 Yes 224.0.0.251
    26. 0 1 Yes 224.0.0.252
    27. 0 4 Yes 239.255.255.250
    To copy to clipboard, switch view to plain text mode 

    netstat output

    Qt Code:
    1. eve:~ Jerry$ netstat -g
    2. Link-layer Multicast Group Memberships
    3. Group Link-layer Address Netif
    4. 1:0:5e:7f:2b:15 <none> en0
    5. 1:0:5e:0:0:fb <none> en0
    6. 1:0:5e:0:0:1 <none> en0
    7. 33:33:ff:c9:32:14 <none> en0
    8. 33:33:0:0:0:fb <none> en0
    9. 33:33:ff:41:27:e <none> en0
    10. 33:33:0:0:0:1 <none> en0
    11. 33:33:ff:33:5b:7a <none> en0
    12. 1:80:c2:0:0:3 <none> en0
    13. 33:33:0:0:0:fb <none> en1
    14. 1:3:93:df:b:92 <none> en1
    15. 33:33:0:0:0:fb <none> awdl0
    16. 33:33:80:0:0:fb <none> awdl0
    17.  
    18. IPv4 Multicast Group Memberships
    19. Group Link-layer Address Netif
    20. 224.0.0.251 <none> lo0
    21. 224.0.0.1 <none> lo0
    22. 239.255.43.21 1:0:5e:7f:2b:15 en0
    23. 224.0.0.251 1:0:5e:0:0:fb en0
    24. 224.0.0.1 1:0:5e:0:0:1 en0
    25.  
    26. IPv6 Multicast Group Memberships
    27. Group Link-layer Address Netif
    28. ff02::fb%lo0 <none> lo0
    29. ff02::2:ff33:9cc0%lo0 <none> lo0
    30. ff01::1%lo0 <none> lo0
    31. ff02::1%lo0 <none> lo0
    32. ff02::1:ff00:1%lo0 <none> lo0
    33. ff02::1:ffc9:3214%en0 33:33:ff:c9:32:14 en0
    34. ff02::fb%en0 33:33:0:0:0:fb en0
    35. ff01::1%en0 33:33:0:0:0:1 en0
    36. ff02::2:ff41:270e%en0 33:33:ff:41:27:e en0
    37. ff02::1%en0 33:33:0:0:0:1 en0
    38. ff02::1:ff33:5b7a%en0 33:33:ff:33:5b:7a en0
    39. ff02::fb%en1 33:33:0:0:0:fb en1
    40. ff02::fb%awdl0 33:33:0:0:0:fb awdl0
    To copy to clipboard, switch view to plain text mode 

    Here is the source code

    Multicast.h

    Qt Code:
    1. #ifndef MULTICAST_H
    2. #define MULTICAST_H
    3.  
    4. #include <QQuickItem>
    5. #include <QHostAddress>
    6. #include <QNetworkInterface>
    7.  
    8. class QUdpSocket;
    9.  
    10. class Multicast : public QQuickItem
    11. {
    12. Q_OBJECT
    13. public:
    14. Multicast();
    15.  
    16. Q_INVOKABLE void multicast(QString s);
    17.  
    18. signals:
    19. void messageAdded(const QString msg);
    20.  
    21. private slots:
    22. void processPendingDatagrams();
    23.  
    24. private:
    25. QNetworkInterface getNetworkInterfaceByAddress(QString adr);
    26. void printNetworkInterfaceInfo(QNetworkInterface ni);
    27. private:
    28. QHostAddress groupAddress;
    29. QUdpSocket *udpSocket;
    30. };
    31.  
    32. #endif // MULTICAST_H
    To copy to clipboard, switch view to plain text mode 

    Multicast.cpp

    Qt Code:
    1. #include "multicast.h"
    2.  
    3. #include <QtNetwork>
    4.  
    5. Multicast::Multicast()
    6. {
    7. // hard coded group
    8. groupAddress = QHostAddress("239.255.43.21");
    9. udpSocket = new QUdpSocket(this);
    10.  
    11. udpSocket->bind(QHostAddress::AnyIPv4, 45454, QUdpSocket::ShareAddress);
    12. // hard coded IP to find network interface. 10.0.1.40 for the other devices
    13. // if I don't manually set this interface, and set loopback to 1,
    14. // this can receive the message from itself. Otherwise, it receives
    15. // nothing.
    16. udpSocket->setMulticastInterface(getNetworkInterfaceByAddress("10.0.1.39"));
    17. bool r = udpSocket->joinMulticastGroup(groupAddress);
    18. udpSocket->setSocketOption(QAbstractSocket::MulticastLoopbackOption, QVariant(1));
    19. QNetworkInterface intf(udpSocket->multicastInterface());
    20. printNetworkInterfaceInfo(intf);
    21.  
    22. connect(udpSocket, SIGNAL(readyRead()),
    23. this, SLOT(processPendingDatagrams()));
    24. }
    25.  
    26. QNetworkInterface Multicast::getNetworkInterfaceByAddress(QString adr)
    27. {
    28. QList<QNetworkInterface> il(QNetworkInterface::allInterfaces());
    29. for (int i = 0; i < il.size(); i++)
    30. {
    31. QList<QNetworkAddressEntry> ade(il[i].addressEntries());
    32. for (int j = 0; j < ade.size(); j++)
    33. {
    34. if (ade[j].ip().toString() == adr)
    35. return il[i];
    36. }
    37. }
    38.  
    39. }
    40.  
    41. void Multicast::printNetworkInterfaceInfo(QNetworkInterface ni)
    42. {
    43. qDebug() << ni.index() << ni.humanReadableName();
    44. QList<QNetworkAddressEntry> ade(ni.addressEntries());
    45. for (int j = 0; j < ade.size(); j++)
    46. qDebug() << ade[j].ip();
    47. }
    48.  
    49. void Multicast::multicast(QString msg)
    50. {
    51. QByteArray datagram = msg.toUtf8();
    52. udpSocket->writeDatagram(datagram.data(), datagram.size(),
    53. groupAddress, 45454);
    54. }
    55.  
    56. void Multicast::processPendingDatagrams()
    57. {
    58. while (udpSocket->hasPendingDatagrams()) {
    59. QByteArray datagram;
    60. datagram.resize(udpSocket->pendingDatagramSize());
    61. udpSocket->readDatagram(datagram.data(), datagram.size());
    62. QString data(datagram);
    63. emit messageAdded(data);
    64. }
    65. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp

    Qt Code:
    1. #include <QApplication>
    2. #include <QQmlApplicationEngine>
    3.  
    4. #include "multicast.h"
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication app(argc, argv);
    9.  
    10. qmlRegisterType<Multicast>("com.multicast.test", 1, 0, "Multicast");
    11.  
    12. QQmlApplicationEngine engine;
    13. engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    14.  
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 

    main.qml

    Qt Code:
    1. import QtQuick 2.3
    2. import QtQuick.Controls 1.2
    3.  
    4.  
    5. import com.multicast.test 1.0
    6.  
    7. ApplicationWindow {
    8. id: applicationWindow1
    9. visible: true
    10. width: 640
    11. height: 480
    12. title: qsTr("Hello World")
    13.  
    14. Multicast {
    15. id : multicast
    16. }
    17.  
    18. menuBar: MenuBar {
    19. Menu {
    20. title: qsTr("File")
    21. MenuItem {
    22. text: qsTr("&Open")
    23. onTriggered: console.log("Open action triggered");
    24. }
    25. MenuItem {
    26. text: qsTr("Exit")
    27. onTriggered: Qt.quit();
    28. }
    29. }
    30. }
    31.  
    32. TextInput {
    33. id: textInput1
    34. y: 57
    35. width: 450
    36. height: 20
    37. text: qsTr("Text Input")
    38. anchors.left: parent.left
    39. anchors.leftMargin: 50
    40. font.pixelSize: 12
    41. }
    42.  
    43. Button {
    44. id: button1
    45. y: 57
    46. text: qsTr("Button")
    47. anchors.left: textInput1.right
    48. anchors.leftMargin: 20
    49.  
    50. onClicked: {
    51. textEdit1.append(textInput1.text)
    52. multicast.multicast(textInput1.text)
    53. textInput1.text = ""
    54. }
    55. }
    56.  
    57. TextEdit {
    58. id: textEdit1
    59. y: 106
    60. height: 327
    61. readOnly: true
    62. anchors.left: parent.left
    63. anchors.leftMargin: 50
    64. anchors.right: parent.right
    65. anchors.rightMargin: 50
    66. font.pixelSize: 12
    67.  
    68. Connections {
    69. target: multicast
    70. onMessageAdded: {
    71. textEdit1.append(msg)
    72. }
    73. }
    74. }
    75. }
    To copy to clipboard, switch view to plain text mode 


    Added after 1 8 minutes:


    Using wireshark to monitor the traffic, the message from Mac dose arrive on Windows.
    Last edited by Jerry Hou; 14th January 2016 at 20:43.

  2. #2
    Join Date
    Jan 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt multicast works from Windows to Mac but not another around

    Just found on Windows the multicast group in bound to interface 4 Ethernet. But my program multicastInterface output 8 Local Area Connection.

    Do I need 2 separate sockets for receiving and sendin individually?


    Added after 1 12 minutes:


    The cause is the VirtualBox network card steals multicasting.This is a known issue in VirtualBox.

    Disable the network interface or set a high Metic in settings solves the problem.

    Now I need to find out if it's feasible to bind a socket on a specific interface.
    Last edited by Jerry Hou; 14th January 2016 at 23:42.

Similar Threads

  1. Does anyone have works with qoauth in windows?
    By MorrisLiang in forum Qt Programming
    Replies: 2
    Last Post: 5th April 2020, 23:37
  2. Replies: 4
    Last Post: 15th April 2015, 11:24
  3. Some works Qt 5.1.1 on Windows XP ?
    By giorgik in forum Qt Programming
    Replies: 2
    Last Post: 20th November 2013, 09:29
  4. Replies: 5
    Last Post: 8th June 2012, 13:14
  5. Works well on Windows but fails on Linux
    By utkuaydin in forum Qt Programming
    Replies: 3
    Last Post: 11th January 2010, 13:22

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.