Problem receiving multicast packets

T

Terry

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
 
T

Terry

No replies? I'm hoping that means nobody has seen this post as opposed
to no suggestions. :)
 
T

Terry

Ok. Does anyone have any suggestions at all?

When joining a multicast group, does the application need to "refresh"
the group membership periodically?

With the way it's working now, multicast just isn't usable. With all
the MVP's in this group, there must be someone that has something I
could test for.

Thanks,
Terry
No replies? I'm hoping that means nobody has seen this post as opposed
to no suggestions. :)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top