Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Receive Broadcast Datagrams

Status
Not open for further replies.

djinn01

Programmer
Dec 16, 2003
7
CA
I have used TcpClient a few times with good success. In this new application, I would like to use a connectionless protocol for each instance of a program to broadcast its status to all other instances on other machines.

I need to use actual broadcasts (not multicasts) because the program is likely to be run in an environment where there is no router.

My code to send the broadcasts is working fine (I see that using a network sniffer). However, my testing is limited to one machine currently and I have another thread listening to broadcasts. That thread seems to receive nothing. So I suspect my code to receive is not good.

I have tried a dozen or so code configurations (using Socket and using UdpClient) and none worked. I would appreciate an example of code to listen to broadcasts. Failing which, I believe the answers to the following questions would help me a lot:

- Will a socket receive broadcasts that are sent from the same host?

- To receive broadcast dgrams, I use
UdpClient udp =
new UdpClient(Config.ControlBroadcastPort,
AddressFamily.InterNetwork);
abBuf = udp.Receive(ref ep);
(where ep is constructed with the broadcast address and the same port number as the UdpClient)
-> Is anything wrong with this code?

- To receive broadcasts on a socket...
- do I need to activate the broadcast socket option?
- do I need to bind the socket first?
- do I need to connect to a broadcast endpoint first?
- should I use Receive or ReceiveFrom?
 
I have finally cut through the fog.

My problem was I was broadcasting to an invalid network address 192.168.255.255 while I should be using 192.168.0.255. I have only recently learned the difference between Class A, B and C adresses (being a programmer more than a network person) and I had not realized that broadcasting to a class B network when I am on a class C network would not work.

If you should be interested, here's the receiving code that works. I have not tried UdpClient.Receive since I fixed my broadcast address.

Socket sktListen = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp );
sktListen.Bind(epLocal);
// epLocal is local IP with Config.ControlBroadcastPort
sktListen.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveBuffer, 2000);
sktListen.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 100);

EndPoint ep = new IPEndPoint(IPAddress.Any,
Config.ControlBroadcastPort);
int n = sktListen.ReceiveFrom(abBuf, SocketFlags.None,
ref ep);
MessageBox.Show(Encoding.UTF8.GetString(abBuf));

This works fine when broadcasting and listening from the same host.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top