Not Receiving UDP Broadcast Packets

K

Kevin S.

I'm trying to write code to receive broadcast UDP messages. These
broadcast messages are being sent from another computer on the local
network. My computer has more than one network connection.

This code works if I put the IP address of my own network connection
in the Bind() call.. If I try to use IPAddress.Any to bind, then
Available always returns 0, and if I call Receive anyway, it blocks
forever. Why won't IPAddress.Any work?

m_udpClient = new UdpClient();
m_udpClient.Client.SetSocketOption
(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
m_udpClient.EnableBroadcast = true;
m_udpClient.Client.Bind(new IPEndPoint(IPAddress.Any,
12012));
// m_udpClient.Client.Bind(new IPEndPoint(new IPAddress(new
byte[] { 192, 168, 0, 102 }), 12012));

(....)

while(whatever)
{
if(0!= m_udpClient.Available)
{
byte[] newBytes = m_udpClient.Receive(ref
m_udpIpEndpoint);
(... blah blah )
}
}

-Kevin
 
C

CY

P

Peter Duniho

I'm trying to write code to receive broadcast UDP messages. These
broadcast messages are being sent from another computer on the local
network. My computer has more than one network connection.

This code works if I put the IP address of my own network connection
in the Bind() call.. If I try to use IPAddress.Any to bind, then
Available always returns 0, and if I call Receive anyway, it blocks
forever. Why won't IPAddress.Any work?

I don't know. It's probably a problem in the code you didn't post, or a
network configuration issue.

I don't normally have more than one network adapter active at once, but I
added one just to test your scenario. I wrote a simple broadcast-based
send & receive test application (see code below). I was unable to
reproduce your problem. I was able to run the program on two different
computers, sending a broadcast from one, and receiving it on the other
computer with two network adapters configured.

I can tell you that you should not be using UdpClient.Available, nor
setting the ReuseAddress option. But I would be surprised if either of
those problems in the code you posted were preventing you from receiving
the datagram(s) you expect to receive (but note that in my test code, I
did not include those mistakes, so I can't say for sure). Other than
that, all I can tell you is that you need to post a concise-but-complete
code example that reliably demonstrates the problem. If using that code
example, others are still unable to reproduce the problem, you probably
have a network configuration problem and should post your question in a
more appropriate forum for that kind of question.

You might want to try my example code to see if it has the same problem.
If it does, you have a network configuration problem. If it doesn't, you
have a bug in your code.

Pete



using System;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace TestUdpClientBroadcast
{
class Program
{
static void Main(string[] args)
{
string strPort;
int port = 0;

Console.Write("Enter port to use (blank for automatic): ");
while ((strPort = Console.ReadLine()) != "" &&
!int.TryParse(strPort, out port))
{
Console.WriteLine("invalid port number \"{0}\"; try
again", strPort.ToString());
}

UdpClient client = new UdpClient();

client.Client.EnableBroadcast = true;
client.Client.Bind(new IPEndPoint(IPAddress.Any, port));
client.BeginReceive(_Callback, client);

Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);

sock.EnableBroadcast = true;

string strMessage;
IPEndPoint epSend = new IPEndPoint(IPAddress.Broadcast,
((IPEndPoint)client.Client.LocalEndPoint).Port);

Console.WriteLine("Enter messages, one message per line (enter
blank line to exit):");
while ((strMessage = Console.ReadLine()) != "")
{
byte[] rgb = Encoding.UTF8.GetBytes(strMessage);

Console.WriteLine("Sending: \"{0}\"", strMessage);
sock.SendTo(rgb, epSend);
}

client.Close();
sock.Close();

Console.WriteLine("Press any key to exit");
Console.ReadKey();
}

static private void _Callback(IAsyncResult iar)
{
try
{
UdpClient client = (UdpClient)iar.AsyncState;

client.BeginReceive(_Callback, client);

IPEndPoint ipRemote = new IPEndPoint(IPAddress.Any, 0);

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 + "\"");
}
}
}
}
 

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