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
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