473,394 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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
Nov 17 '05 #1
2 3094
No replies? I'm hoping that means nobody has seen this post as opposed
to no suggestions. :-)
Terry wrote:
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

Nov 17 '05 #2
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

Terry wrote:
No replies? I'm hoping that means nobody has seen this post as opposed
to no suggestions. :-)
Terry wrote:
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

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Jim H | last post by:
I have a UDP socket that sends out a request on a multicast socket and waits for a response. This client is not listening on a multicast IP but the local IP. The server (UNIX) responds to the...
6
by: Tom Kent | last post by:
I have a computer with two network cards (attached to seperate networks) and I am having trouble getting my C# application to send its multicast packets on a specific one. It receives just fine...
2
by: Naveen Mukkelli | last post by:
Hi, I'm sending multicast messages UDP socket and C#. But the receiving clients are not receiving all the messages. Receiving clients are missing out some messages. When I debug the server...
1
by: Eric St-Onge | last post by:
Hi, I have eleven C# applications running on eleven different computers sending packets to a MSMQ MultiCast configured queue (same address for all). Those eleven senders also act as receivers. ...
0
by: Nuno Magalhaes | last post by:
I've build an application that sends UDP packets to a multicast address 224.1.2.3 but in my other application I can't receive that packets using the code below. It blocks on receive. Any...
7
by: Nuno Magalhaes | last post by:
I'm streaming quicktime H264 movies to 224.1.2.3:554. Why, in my MulticastReceiver class of another application, have I have to bind my socket locally on the same remote port (554) in order to...
7
by: pietro.cerutti | last post by:
Hi guys, I have a daemon running on Debian and listening for multicast packets sent to 224.0.0.251:5353 (ZeroConf Multicast DNS queries). The server is plugged into a VLAN trunk with eth0 and...
2
by: Maggie LU | last post by:
Hi everyone, I am trying to send out multicast packets from my program. I am using Microsoft Visual Studio VC++ for coding. Any ideas?? Can anyone show me how to do it? Thanks. Best...
0
by: george585 | last post by:
Hello! I am new to network programming, and understand just basics. Using some sample code, and having read documentation, I managed to create a simple app in C# and VB.NET. The application is...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.