views:

3371

answers:

3

I'm using .NET 2.0 and have created a fairly simple udp broadcast app and udp listener.

Listener code:

Socket listener = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp );
IPEndPoint localEndPoint = new IPEndPoint( IPAddress.Any, 11000 );
listener.Bind( localEndPoint );
EndPoint ep = (EndPoint)iep;
Console.WriteLine("Ready to receive…");
byte[] data = new byte[1024];
int recv = listener.ReceiveFrom(data, ref ep);
string stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine("received: {0} from: {1}", stringData, ep.ToString());
listener.Close();

Server code:

int groupPort = 11000;
IPEndPoint groupEP = new IPEndPoint( IPAddress.Parse( "255.255.255.255" ), groupPort );

if ( radioButton2.Checked )
{
    groupEP = new IPEndPoint( IPAddress.Broadcast, groupPort );
}
    else if ( radioButton3.Checked )
{
    groupEP = new IPEndPoint( IPAddress.Parse( "172.16.75.15" ), groupPort );
}

Socket socket = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp );
socket.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1 );
socket.SendTo( System.Text.ASCIIEncoding.ASCII.GetBytes( testTextBox.Text ), groupEP );

The server is just a simple windows app with 3 radio buttons, button and a textbox.

When I run the server on a separate computer and choose radioButton3 I receive the message just fine on my client listener (which is running on ip address 172.16.75.15). However if I choose the first or second radiobutton (which creates Broadcast or 255.255.255.255 as the ip address) I get nothing. Now if I run the client on the same pc as the server I can receive the message using those two choices.

I'm not sure what I'm doing wrong or if there could be some kind of firewall preventing UDP messages on the LAN. Any ideas?

Thanks,

Craig

+2  A: 

Try a broadcast on the local subnet only. IE if your subnet is 255.255.255.0 try a broadcast of 172.16.75.255. It may be that windows, a router or even network card automatically block universal broadcasts as a preventative measure.

Spencer Ruport
most routers / switches / etc. block the propagation of broadcasts by default.
Josh E
I've tried the 172.16.75.255 broadcast and it failed too. Josh you could very well be right.
Craig
A: 

Just a note, but if you are writing a new app from the ground up then you should really be using multicast instead of broadcast.

Robert S. Barnes
A: 

Is the client on the same physical network as the server? If not, you won't be able to do a local broadcast (255.255.255.255) and will need to do a directed subnet broadcast. You will have to enable your router to allow a directed subnet broadcast (172.16.75.255) before that will work.

tvanfosson