Help with Multicast and bind problem needed

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Ron Hall (rfhall_at_verizon.net)
Date: 11/02/04


Date: Tue, 02 Nov 2004 02:16:42 GMT

I have an application that uses multicast and I'm trying to extend it to
support machines multiple adapters allowing the user to select the adapter
to use. I added a bind statement to the socket initialization and now
communications no longer work. So I'm clearly doing something wrong or not
understanding something. I've included a brief snippet of code that I'm
trying to get working, if someone can point me at my problem I would greatly
appreciate it.

    m_rcvsock = WSASocket(AF_INET, SOCK_DGRAM, 0, NULL, 0,
        WSA_FLAG_MULTIPOINT_C_LEAF | WSA_FLAG_MULTIPOINT_D_LEAF);
    assert(m_rcvsock != INVALID_SOCKET);
    nOptVal = 0;
    irtn = WSAIoctl(m_rcvsock, SIO_MULTIPOINT_LOOPBACK, &nOptVal,
sizeof(nOptVal),
        NULL, 0, (PULONG) &irtn, NULL, NULL);
    assert(irtn == 0);
    nOptVal = 1;
    irtn = WSAIoctl(m_rcvsock, SIO_MULTICAST_SCOPE, &nOptVal,
sizeof(nOptVal),
        NULL, 0, (PULONG) &irtn, NULL, NULL);
    assert(irtn == 0);

   // bind to the specified adapter

   sa.sin_family = AF_INET;
   sa.sin_addr.S_un.S_addr = ulAdapter;
   sa.sin_port = 0;
   irtn = bind(m_rcvsock, (sockaddr *) &sa, sizeof(sockaddr_in));
   assert(irtn == 0);

   // now join this socket as a leaf in the multicast group

   WSAEventSelect(m_rcvsock, m_EventHandle, FD_CONNECT);
   WSAResetEvent(m_EventHandle);
   sa.sin_family = AF_INET;
   sa.sin_addr.s_addr = htonl(ulRcvIP);
   sa.sin_port = htons(usRcvPort);
   irtn = WSAJoinLeaf(m_rcvsock, (sockaddr *) &sa, sizeof(sa),
    NULL, NULL, NULL, NULL, JL_RECEIVER_ONLY);
   assert(irtn == m_rcvsock);
   irtn = WaitForSingleObject(m_EventHandle, 1000);
   if (irtn != WAIT_OBJECT_0)
    return IDS_NO_CONNECT;

If I comment out the bind statement and the assert that follows
communication works just fine.

TIA
Ron