473,566 Members | 2,924 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 "receiveFro m()" in a loop. This works
sometimes, but other times it'll get into a funk where the
"receiveFro m()" 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( )
{
createListenerS ocket();

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

private void createListenerS ocket()
{
IPEndPoint iep = new IPEndPoint(IPAd dress.Any, _nGroupPort);
int nTTL = AppSettings.Ins tance.LAN.Multi castTTL;
_socket = new Socket(AddressF amily.InterNetw ork, SocketType.Dgra m,
ProtocolType.Ud p);
_socket.Bind(ie p);
_socket.SetSock etOption(Socket OptionLevel.IP,
SocketOptionNam e.AddMembership ,
new MulticastOption (_groupAddress) );
_socket.SetSock etOption(Socket OptionLevel.IP,
SocketOptionNam e.MulticastTime ToLive, nTTL);
}
And here's the loop where I'm receiving and processing the packets.

private void listenerProc()
{
IPEndPoint iep = new IPEndPoint(IPAd dress.Any, 0);
EndPoint ep = iep;
byte[] recvBuff = new byte[8192];
int nBytes;
while (_fRunning)
{
nBytes = _socket.Receive From(recvBuff, ref ep);

if (isFromMyself(( IPEndPoint)ep))
{
logMessage("Pac ket was sent by me. Ignoring.", TraceLevel.Verb ose);
continue;
}

logMessage("lis tenerProc - Received " + nBytes + " bytes from " +
ep.ToString(), TraceLevel.Verb ose);
IPEndPoint iepRemote = (IPEndPoint)ep;
processPacket(g etMessageBytes( 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.Receiv eFrom()" 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 3114
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 "receiveFro m()" in a loop. This works
sometimes, but other times it'll get into a funk where the
"receiveFro m()" 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( )
{
createListenerS ocket();

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

private void createListenerS ocket()
{
IPEndPoint iep = new IPEndPoint(IPAd dress.Any, _nGroupPort);
int nTTL = AppSettings.Ins tance.LAN.Multi castTTL;
_socket = new Socket(AddressF amily.InterNetw ork, SocketType.Dgra m,
ProtocolType.Ud p);
_socket.Bind(ie p);
_socket.SetSock etOption(Socket OptionLevel.IP,
SocketOptionNam e.AddMembership ,
new MulticastOption (_groupAddress) );
_socket.SetSock etOption(Socket OptionLevel.IP,
SocketOptionNam e.MulticastTime ToLive, nTTL);
}
And here's the loop where I'm receiving and processing the packets.

private void listenerProc()
{
IPEndPoint iep = new IPEndPoint(IPAd dress.Any, 0);
EndPoint ep = iep;
byte[] recvBuff = new byte[8192];
int nBytes;
while (_fRunning)
{
nBytes = _socket.Receive From(recvBuff, ref ep);

if (isFromMyself(( IPEndPoint)ep))
{
logMessage("Pac ket was sent by me. Ignoring.",
TraceLevel.Verb ose);
continue;
}

logMessage("lis tenerProc - Received " + nBytes + " bytes from "
+ ep.ToString(), TraceLevel.Verb ose);
IPEndPoint iepRemote = (IPEndPoint)ep;
processPacket(g etMessageBytes( 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.Receiv eFrom()" 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 "receiveFro m()" in a loop. This
works sometimes, but other times it'll get into a funk where the
"receiveFro m()" 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( )
{
createListenerS ocket();

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

private void createListenerS ocket()
{
IPEndPoint iep = new IPEndPoint(IPAd dress.Any, _nGroupPort);
int nTTL = AppSettings.Ins tance.LAN.Multi castTTL;
_socket = new Socket(AddressF amily.InterNetw ork,
SocketType.Dgra m, ProtocolType.Ud p);
_socket.Bind(ie p);
_socket.SetSock etOption(Socket OptionLevel.IP,
SocketOptionNam e.AddMembership ,
new MulticastOption (_groupAddress) );
_socket.SetSock etOption(Socket OptionLevel.IP,
SocketOptionNam e.MulticastTime ToLive, nTTL);
}
And here's the loop where I'm receiving and processing the packets.

private void listenerProc()
{
IPEndPoint iep = new IPEndPoint(IPAd dress.Any, 0);
EndPoint ep = iep;
byte[] recvBuff = new byte[8192];
int nBytes;
while (_fRunning)
{
nBytes = _socket.Receive From(recvBuff, ref ep);
if (isFromMyself(( IPEndPoint)ep))
{
logMessage("Pac ket was sent by me. Ignoring.",
TraceLevel.Verb ose);
continue;
}

logMessage("lis tenerProc - Received " + nBytes + " bytes from
" + ep.ToString(), TraceLevel.Verb ose);
IPEndPoint iepRemote = (IPEndPoint)ep;
processPacket(g etMessageBytes( 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.Receiv eFrom()" 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
12179
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 sender's IP. Using debug files on the server I see the server receive the request from my application on my machine and I see the server send to the...
6
5629
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 (I'm assuming because its listening to both networks), but it seems like it just picks the default to send over. I have implemented the...
2
2096
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 side, the messages are being sent correctly, but receiving side side is missingout some messages. What could be the problem and how can we solve this...
1
4812
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. Do all receivers receive the sent packets in the same order? Thanks Eric
0
1346
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 suggestions? (I can see the packets arriving at 224.1.2.3:554 on the client machine since I'm running the server and the client on the same machine) ...
7
4870
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 receive the multicast packets (with the primitive receive)? Why can't I bind to another port and still receive the packets? Below is the source code...
7
8439
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 joins several VLANs using virtual interfaces (i.e. eth0.xxx). It should be able to capture the UDP packets on any interfaces, so it spawns a thread for...
2
3025
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 regards, Maggie Lu
0
3559
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 supposed to do the following: monitor ALL INCOMING TCP traffic on the local computer, and save certain parts of it as files - not log files though, but...
0
7673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7953
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5485
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2085
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.