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 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?