Is there some way to generate random Multicast address ?

  • I write program which will stream audio to multicast group. I need to generate random multicast ip which is not reserved yet. I need something like chooseRandomIPv4SSMAddress function in live555 library. So it there any way to do this in qt ?

  • The realization of this function in live555 library is seems like this:
    @netAddressBits chooseRandomIPv4SSMAddress(UsageEnvironment& env) {
    // First, a hack to ensure that our random number generator is seeded:
    (void) ourIPAddress(env);

    // Choose a random address in the range [,
    // i.e., [0xE8000100, 0xE8FFFFFF)
    netAddressBits const first = 0xE8000100, lastPlus1 = 0xE8FFFFFF;
    netAddressBits const range = lastPlus1 - first;

    return ntohl(first + ((netAddressBits)our_random())%range);

    @netAddressBits ourIPAddress(UsageEnvironment& env) {
    static netAddressBits ourAddress = 0;
    int sock = -1;
    struct in_addr testAddr;

    if (ourAddress == 0) {
    // We need to find our source address
    struct sockaddr_in fromAddr;
    fromAddr.sin_addr.s_addr = 0;

    // Get our address by sending a (0-TTL) multicast packet,
    // receiving it, and looking at the source address used.
    // (This is kinda bogus, but it provides the best guarantee
    // that other nodes will think our address is the same as we do.)
    do {
      loopbackWorks = 0; // until we learn otherwise
      testAddr.s_addr = our_inet_addr(""); // arbitrary
      Port testPort(15947); // ditto
      sock = setupDatagramSocket(env, testPort);
      if (sock < 0) break;
      if (!socketJoinGroup(env, sock, testAddr.s_addr)) break;
      unsigned char testString[] = "hostIdTest";
      unsigned testStringLength = sizeof testString;
      if (!writeSocket(env, sock, testAddr, testPort, 0,
         testString, testStringLength)) break;
      // Block until the socket is readable (with a 5-second timeout):
      fd_set rd_set;
      FD_SET((unsigned)sock, &rd_set);
      const unsigned numFds = sock+1;
      struct timeval timeout;
      timeout.tv_sec = 5;
      timeout.tv_usec = 0;
      int result = select(numFds, &rd_set, NULL, NULL, &timeout);
      if (result <= 0) break;
      unsigned char readBuffer[20];
      int bytesRead = readSocket(env, sock,
     readBuffer, sizeof readBuffer,
      if (bytesRead != (int)testStringLength

    || strncmp((char*)readBuffer, (char*)testString, testStringLength) != 0) {

      loopbackWorks = 1;
    } while (0);
    if (sock >= 0) {
      socketLeaveGroup(env, sock, testAddr.s_addr);
    if (!loopbackWorks) do {
      // We couldn't find our address using multicast loopback,
      // so try instead to look it up directly - by first getting our host name, and then resolving this host name
      char hostname[100];
      hostname[0] = '\0';
      int result = gethostname(hostname, sizeof hostname);
      if (result != 0 || hostname[0] == '\0') {

    env.setResultErrMsg("initial gethostname() failed");

      // Try to resolve "hostname" to an IP address:
      NetAddressList addresses(hostname);
      NetAddressList::Iterator iter(addresses);
      NetAddress const* address;
      // Take the first address that's not bad:
      netAddressBits addr = 0;
      while ((address = iter.nextAddress()) != NULL) {

    netAddressBits a = (netAddressBits)(address->data());
    if (!badAddressForUs(a)) {
    addr = a;

      // Assign the address that we found to "fromAddr" (as if the 'loopback' method had worked), to simplify the code below: 
      fromAddr.sin_addr.s_addr = addr;
    } while (0);
    // Make sure we have a good address:
    netAddressBits from = fromAddr.sin_addr.s_addr;
    if (badAddressForUs(from)) {
      char tmp[100];
      sprintf(tmp, "This computer has an invalid IP address: %s", AddressString(from).val());
      from = 0;
    ourAddress = from;
    // Use our newly-discovered IP address, and the current time,
    // to initialize the random number generator's seed:
    struct timeval timeNow;
    gettimeofday(&timeNow, NULL);
    unsigned seed = ourAddress^timeNow.tv_sec^timeNow.tv_usec;

    return ourAddress;

  • So is there any solution on Qt ?

  • How can I check if multicast group exist or not ? If I have it's ip ?

  • I need to solve similar issue, some one know how to check is multicast group exists?

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.