Problem with broadcast in C#

G

Guest

Can anybody help and explain why?
I wrote two programs just to test broadcasting in C#, which both run on the
same machine. However, the receiver program can't receive any messages from
the sender program. Key parts of the source codes are listed below:

/****** Sender.cs *****/
int remotePort = 8888;
// Init the socket
try {
Socket sendSocket = new Socket( AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp );
sendSocket.SetSocketOption( SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 1);
IPEndPoint remoteEP = new IPEndPoint( IPAddress.Broadcast, remotePort );
}
catch (Exception e ) {
Console.WriteLine( e.ToString() );
}

while ( true ) {
sendSocket.SendTo( data, remoteEP );
Thread.CurrentThread.Join( new TimeSpan( 0, 0, 0, 5, 0 ) );
}

/****** Listener.cs *****/
byte[] data = new byte[128];
localPort = 8888;
try{
IPEndPoint remoteEP = new IPEndPoint( new IPAddress( 0 ), 0 );
UdpClient udpListener = new UdpClient( localPort );
}
catch ( Exception e ) {
Console.WriteLine( e.ToString() );
}

while ( true ) {
data = udpListener.Receive( ref remoteEP );
}

Thank you ! LOL~
 
C

CometJoe

Broadcast works, we use it often in our networked medical systems.

When you create your listening remoteEP, try using the loopback IP
"127.0.0.1" and see what happens. My understanding is that Broadcast
only works within the scope of a subnet, and unless your network is
set up on "0.0.0.xxx" then you're trying to listen on an invalid
address.

For instance, if your network card IP is "192.168.0.1" then you are
broadcasting to "192.168.0.xxx" range of addresses...but trying to
listen on "0.0.0.xxx" with the code that you show won't receive it.

I'll check some working code after lunch and let you know how we do
it, but your really close.

A test would be to set the sending & receiving IP's to a known and
validate it works, then only change the sending to Broadcast to see it
still works, then try variations of your listening IP.

HTH,
Joe
 
C

CometJoe

Ok, I was wrong. I have a test app that received OK with your "new
IPAddress(0)"...don't know why, but it did.

I haven't used the UDPClient, I just open a second socket to listen,
same as the first, but I "bind" it to a remote EP in order to
listen.


iepRx = new IPEndPoint ( new IPAddress( 0 ), nDataPort
);
epRx = (EndPoint) iepRx;

sckRx = new Socket ( AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp );
sckRx.SetSocketOption ( SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 500 );
sckRx.Bind ( epRx );


I've used the IPAddress.Broadcast to send, and I've also listened on
the IPAddress.Any and they work as expected. But, as I said, I tried
address 0, and it worked too.

Here is the test app I used, maybe it will help. Note the casting of
the IPEndpoint to an EndPoint for the bind call.

http://www.cometracing.com/Loopback/Loopback.zip

Good Luck,
Joe
 
G

Guest

Thank you, Joe!

I've put the two programs to seperate computers in the same subnet, and it
works as you mentioned. The reason that the datagrams won't be received on
the same machine is that I didn't specify the loop option, also as you
pointed out.

I am new to network programming and will surely meet with all kinds of
problems that may seem too simple, but I really appreciate you and other
people's help.

Thanks again!
 

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