Problem receiving multicast packets



I've got a strange problem receiving multicast packets in a C# application. What's strange is that it works *sometimes* but not always.

I create a socket, call bind(), set the multicast socket option and then fire off a thread that calls "receiveFrom()" in a loop. This works sometimes, but other times it'll get into a funk where the "receiveFrom()" call doesn't return even though a packet trace capture (Ethereal) shows that the multicast packet was received.

Here's where I'm creating the socket and setting up the thread:

private void createListener()
{
	createListenerSocket();

	_listenerThread = new Thread(new ThreadStart(listenerProc));
	_listenerThread.Name = "Multicast Listener";
        _listenerThread.IsBackground = true;
	_fRunning = true;
	_listenerThread.Start();
}

private void createListenerSocket()
{
IPEndPoint iep = new IPEndPoint(IPAddress.Any, _nGroupPort);
int nTTL = AppSettings.Instance.LAN.MulticastTTL;
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
_socket.Bind(iep);
_socket.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.AddMembership,
new MulticastOption(_groupAddress));
_socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, nTTL);
}



And here's the loop where I'm receiving and processing the packets.

private void listenerProc()
{
	IPEndPoint iep = new IPEndPoint(IPAddress.Any, 0);
	EndPoint ep = iep;
	byte[] recvBuff = new byte[8192];
	int nBytes;
	while (_fRunning)
	{
		nBytes = _socket.ReceiveFrom(recvBuff, ref ep);
							
		if (isFromMyself((IPEndPoint)ep))
		{
			logMessage("Packet was sent by me.  Ignoring.", TraceLevel.Verbose);
			continue;
		}

logMessage("listenerProc - Received " + nBytes + " bytes from " + ep.ToString(), TraceLevel.Verbose);
IPEndPoint iepRemote = (IPEndPoint)ep;
processPacket(getMessageBytes(recvBuff, nBytes), iepRemote);
}
}


So, to reiterate, I've verified that the packet is being recieved by my NIC and is being sent to the proper multicast address. In my case the address is "230.2.1.75" with a TTL of 2. But, most of the time, the "_socket.ReceiveFrom()" never returns. Sometimes this works just fine.

Does anyone see what I'm doing wrong?  (I'm hoping Rich Blum sees this :-)

Thanks,
Terry
.



Relevant Pages

  • Re: socket question: how to use sendto and recvfrom based on the same multicast address
    ... Why don't you just use the same socket for both send and receive? ... multicast address and receive other data from others on the samd ... closesocket; ... if (setsockopt (sockrcv, ...
    (microsoft.public.windowsce.platbuilder)
  • AF_IPN: Inter Process Networking, try these...
    ... If you suspect we would be better using IP multicast, ... Exercise #1. ... I Create a IPN socket, with protocol IPN_VDESWITCH and all the VM can ... or a binary stream. ...
    (Linux-Kernel)
  • Re: Too much multicasting in Linux
    ... > that didn't join the multicast address where the data is being sent. ... > the interface 0.0.0.0 causes them to receive all data. ... if I have a socket that joins the SAME ... > multicast group but is tied to a different interface it will not receive ...
    (comp.os.linux.networking)
  • Re: divert and deadlock issues
    ... multicast socket options to a divert socket, ... socket that originally sent the multicast packet itself. ... They shouldn't be necessary, however I can foresee situations where someone might well want to redirect multicast datagrams traversing an IPPROTO_DIVERT socket, by using these socket options. ... a divert injection should occur at teh position of the firewall ...
    (freebsd-net)
  • Re: WSASocket FAILS for multicast options..
    ... CE supports multicasting using Berkeley calls. ... // Set up the socket. ... We aren't associated with a multicast ... Paul T. ...
    (microsoft.public.windowsce.app.development)