Receiving broadcast messages
I'm trying to receive broadcast messages using C# code in an ISDN network with BRI interface at my end.
I see the packets sent to the broadcast ip address (239.255.255.255) on some ports using Comm View tool.
But when I try to listen to this IP address, it says the address is not in a valid context.
But when I send broadcast messages to 255.255.255.255 on a port, I can receive those messages with the below code..
What could be the problem with this ip address - 239.255.25开发者_如何学运维5.255 ?
The code I use to listen to broadcast messages is..
UdpClient udp = new UdpClient();
IPEndPoint receiveEndPoint = new IPEndPoint(IPAddress.Any, 8013);
// If I use IPAddress.Parse("239.255.255.255") to listen to,
// it says "the address is not in a valid // context."
udp.Client.Bind(receiveEndPoint);
udp.BeginReceive(_Callback, udp);
static private void _Callback(IAsyncResult iar)
{
try
{
UdpClient client = (UdpClient)iar.AsyncState;
client.BeginReceive(_Callback, client);
IPEndPoint ipRemote = new IPEndPoint(IPAddress.Any, 8013);
byte[] rgb = client.EndReceive(iar, ref ipRemote);
Console.WriteLine("Received {0} bytes: \"{1}\"",
rgb.Length.ToString(), Encoding.UTF8.GetString(rgb));
}
catch (ObjectDisposedException)
{
Console.WriteLine("closing listening socket");
}
catch (Exception exc)
{
Console.WriteLine("Listening socket error: \"" +
exc.Message + "\"");
}
}
There are packets sent to the broadcast ipaddress (239.255.255.255) which I can see in Commview tool, but can't receive them from the code...
Can anybody help me out please?
Thanking you in advance,
Prasad Kancharla.I haven't done much with multicasting, but I believe that preparing to receive multicast packets is a two-step process. First, you bind to a local IP address, which is what you've done with IPAddress.Any
. Then, you need to specify which multicast group you wish to join using a MulticastOption
object with the Socket.SetSocketOption
method.
The MSDN Library has an example for your reference.
It sounds like you're assuming that the address is a directed broadcast (subnet-local broadcast) when it's actually in the IP address range reserved for multicasting, which is something else entirely.
精彩评论