Results 1 to 8 of 8

Thread: MULTICAST with QT 4.1 and above.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: MULTICAST with QT 4.1 and above.

    Qt uses BSD sockets to do its job. You can even set a "pure" BSD socket (using QAbstractSocket::setSocketDescriptor), so you can create a bsd socket, set the options you need and make a Q*Socket out of it. I have never used multicasting but if it only needs setting a proper multicast address, then Qt should handle it out of the box.

  2. #2
    Join Date
    Feb 2006
    Posts
    12

    Default Re: MULTICAST with QT 4.1 and above.

    Hi Wysota,

    Well, I have been programming those small Multicast handlers both under Linux and Windows. Actually Winsock is based also on the BSD "Like" socket lib ( Not MFC, just Winsock ), and they use the same system calls.

    Well I tried using QUdpSocket but it doesn't catch any Multicasted packet. Here a simple example of code provided by Trolltech:

    Qt Code:
    1. QUdpSocket udpSocket = new QUdpSocket(this);
    2. udpSocket->bind(QHostAddress::LocalHost, 24001);
    3.  
    4. while (udpSocket->hasPendingDatagrams())
    5. {
    6. QByteArray datagram;
    7. datagram.resize(udpSocket->pendingDatagramSize());
    8. QHostAddress sender;
    9. quint16 senderPort;
    10.  
    11. udpSocket->readDatagram(datagram.data(), datagram.size(),
    12. &sender, &senderPort);
    13.  
    14. processTheDatagram(datagram);
    15. }
    To copy to clipboard, switch view to plain text mode 

    The problem here is pretty obvious : where do you join the multicast group ????

    So I tried to put in the bind() function a QHostAddress containing the multicast address, but the bind function fails each time.

    So, I don't know I do believe that Qt doesn't handle multicast by itself ?? Sad isn't it

  3. #3
    Join Date
    Aug 2006
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: MULTICAST with QT 4.1 and above.

    I had the same issue a while ago and used setsockopt to set multicast option. The code looks like this:

    udpSendingSocket = new QUdpSocket;
    if (!udpSendingSocket->bind(sendPort, QUdpSocket::ShareAddress))
    std::cout <<"send bind failed\n";
    sendingFd = udpSendingSocket->socketDescriptor();

    if (sendingFd != -1)
    {
    /* set up destination address */
    // struct sockaddr_in sendAddr;
    memset(&sendAddr,0,sizeof(sendAddr));
    sendAddr.sin_family=AF_INET;
    sendAddr.sin_addr.s_addr=inet_addr(HELLO_GROUP);
    sendAddr.sin_port=htons(sendPort);

    // set TTL
    unsigned int ttl = 1; // restricted to the same subnet
    if (setsockopt(sendingFd, IPPROTO_IP, IP_MULTICAST_TTL, (unsigned int*)&ttl, sizeof(ttl) ) < 0)
    {
    std::cout << "TTL failed\n";
    }
    }

    Can Qt add a multicast option in QUdpSocket class?

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
  •  
Qt is a trademark of The Qt Company.