Reputation: 5413
I have seen some say UDPSocket Broadcast to 127.255.255.255.
1)What so special about this
127.*.*.*` network?
2)If I have client systems that are not in this 127.*.*.*
network and I don't know what network they are on then what broadcast address I should use so the client can receive the message.
3)During the UDP Broadcast, I guess the Server address is Not important because it's a connectionless transmission?
Upvotes: 5
Views: 12682
Reputation: 20818
Looking at the output of ip address
I get the following:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether ac:1f:6b:78:27:42 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.1/24 brd 192.168.1.255 scope global eno1
valid_lft forever preferred_lft forever
As we can see, lo
doesn't include the BROADCAST flag, so you can't broadcast on that address (network).
As to how to determine the address, I would suggest you offer a configuration file where the admin can enter the IP he wants to use to broadcast. Much cleaner, I think. However, if you want to programmatically find the list of interfaces then you can use the if_...()
functions get the IP addresses attached to each interface. Just skip lo
(well, skip any interface which does not have the BROADCAST
flag set).
For how that's done, I wrote the libaddr, a C++ library which shows how you list the interfaces. From there you can use the info in the interface. It's not too complicated with the docs & structs that you find in the /usr/include/...
folder. Many tools use that feature in order to listen on all networks your computer has available. This could be a security issue, though.
For broadcast, you have several possibilities, though.
Send to an interface which supports BROADCAST
and use the broadcast IP address of the interface. Usually an IP such as 192.168.1.255.
Send to 255.255.255.255, which is really ugly and dangerous. It's going to go absolutely everywhere.
Send to a 224.x.x.x IP address. However, such IPs can cause problems because they act in a way similar to the 255.255.255.255 (i.e. the messages get sent everywhere). That being said, smart switches can check which computer is currently listening on those IP addresses and thus control which computer is sent the UDP packets. If you can set it up that way, that is... To me smart switches are still just theory.
I have the eventdispatcher C++ library that handles all of those cases. The code for UDP is found in a few files which name start with udp_...
so it should be easy enough to copy & paste. If you want to use the library, it's a bit more complicated since it has dependencies (all found in the Snap! C++ project, though, so you can git clone recursively... the info on how to do all of that is in the main README.md).
Upvotes: 0
Reputation: 782683
127.0.0.0/8
is a special address block that's reserved for loopback to the same machine, it should never be the address of a real network interface. In particular, 127.0.0.1
is the default localhost
address (look at the lo0
interface on a Unix machine).
You should never have client systems in the 127/8 network. I don't think 127.255.255.255
could be intended to send to the network, it should only be for sending to the local machine.
If necessary, the client can get the server address when it receives the response. It can then use this to keep communicating with that server.
Upvotes: 4