为了执行UDP广播,要使用什么IP?
我正在通过LAN编写一个非常简单的服务发现协议。这个想法是通过UDP传播服务器的连接数据到整个LAN。
我能够将数据从广播员传递给听众,但前提是我使用地址“ 0.0.0.0”。如果我使用本地LAN地址 + .255(LAN广播地址),则无法发送任何消息。
的广播的代码。
import asyncio
import asyncudp
from socket import gethostbyname, gethostname
def getBroadcastIp():
ip = gethostbyname(gethostname())
broadcast_ip = '.'.join(ip.split('.')[:-1]+["255"])
print(broadcast_ip)
# return broadcast_ip
return '0.0.0.0'
async def broadcastUdp():
bip = getBroadcastIp()
while True:
sock = await asyncudp.create_socket(remote_addr=(bip, 50000))
print("Sending MESSAGE")
sock.sendto(b'Message', addr=(bip, 50000))
await asyncio.sleep(3)
sock.close()
async def main():
await asyncio.gather(*[
# I have more stuff here
asyncio.create_task(broadcastUdp())
])
asyncio.run(main())
是我
import asyncio
import asyncudp
from socket import gethostbyname, gethostname
def getBroadcastIp():
ip = gethostbyname(gethostname())
broadcast_ip = '.'.join(ip.split('.')[:-1]+["255"])
print(broadcast_ip)
# return broadcast_ip
return '0.0.0.0'
async def listenUdp():
bip = getBroadcastIp()
sock = await asyncudp.create_socket(local_addr=(bip, 50000))
try:
while True:
data, addr = await asyncio.wait_for(sock.recvfrom(), 10)
print("RECEIVING: ", data)
except asyncio.TimeoutError:
print('No data received in 10 seconds.')
sock.close()
async def main():
await asyncio.gather(*[
# I have more stuff here
asyncio.create_task(listenUdp())
])
asyncio.run(main())
这
I'm writing a very simple service discovery protocol over LAN. The idea is to broadcast to the entire LAN via UDP the connection data of the server.
I am able to pass data from my broadcaster to my listener, but only if I use the address '0.0.0.0'. If I use the local LAN address + .255 (the LAN broadcast address) I cannot send any message.
This is the code of my broadcast.py:
import asyncio
import asyncudp
from socket import gethostbyname, gethostname
def getBroadcastIp():
ip = gethostbyname(gethostname())
broadcast_ip = '.'.join(ip.split('.')[:-1]+["255"])
print(broadcast_ip)
# return broadcast_ip
return '0.0.0.0'
async def broadcastUdp():
bip = getBroadcastIp()
while True:
sock = await asyncudp.create_socket(remote_addr=(bip, 50000))
print("Sending MESSAGE")
sock.sendto(b'Message', addr=(bip, 50000))
await asyncio.sleep(3)
sock.close()
async def main():
await asyncio.gather(*[
# I have more stuff here
asyncio.create_task(broadcastUdp())
])
asyncio.run(main())
and this is my listener.py:
import asyncio
import asyncudp
from socket import gethostbyname, gethostname
def getBroadcastIp():
ip = gethostbyname(gethostname())
broadcast_ip = '.'.join(ip.split('.')[:-1]+["255"])
print(broadcast_ip)
# return broadcast_ip
return '0.0.0.0'
async def listenUdp():
bip = getBroadcastIp()
sock = await asyncudp.create_socket(local_addr=(bip, 50000))
try:
while True:
data, addr = await asyncio.wait_for(sock.recvfrom(), 10)
print("RECEIVING: ", data)
except asyncio.TimeoutError:
print('No data received in 10 seconds.')
sock.close()
async def main():
await asyncio.gather(*[
# I have more stuff here
asyncio.create_task(listenUdp())
])
asyncio.run(main())
Is 0.0.0.0 the right IP for broadcasting or am I missing something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
广播基本上被贬低了,因为它会中断局域网上的每个主机,即使是不感兴趣的人,例如路由器,打印机等。出于安全原因,使用广播拒绝应用程序。这样做的现代方式使用了多播,并且有几种注册的多播发现协议。多播仅中断感兴趣的设备。
要构建您的应用程序,您希望有兴趣的设备订阅预定义的多播组(在IANA中注册一个,或在组织本地范围中选择一个,
239.0.0.0.0/8
)。当主机正确地订阅多播组时,它将发送IGMP请求,并为多播路由器和IGMP-SNOOM-SNOOKING STECTIONS提供通知,以将该组的任何多播流量发送给请求主机。多播源将发送到多播组地址的地址,所有订阅的主机都将收到它,但不会中断未订阅多播组的任何主机。然后,接收主机可以使用自己的单媒体地址直接响应源单媒体地址。
Broadcast is basically deprecated because it interrupts every host on the LAN, even those not interested, e.g. routers, printers, etc. Broadcast was eliminated from IPv6, so the application could not be ported, and many companies will reject applications using broadcast for security reasons. The modern way of doing such things uses multicast, and there are several registered multicast discovery protocols. Multicast only interrupts interested devices.
To build your application, you want the interested devices to subscribe to your predefined multicast group (either register one with IANA, or choose one in the Organization-Local scope,
239.0.0.0/8
). When a host properly subscribes to a multicast group, it will send an IGMP request, and that will inform multicast routers and IGMP-snooping switches to send any multicast traffic for that group to the requesting host.The multicast source will send to the address of the multicast group address, and all subscribed hosts will receive it, but not interrupt any host not subscribed to the multicast group. The receiving hosts can then respond directly to the source unicast address using their own unicast addresses.
正如罗恩·莫普(Ron Maupin)恰当地指出的那样,广播已被弃用,多播是这样做的方法。如果您正在考虑使用多播,则应意识到地址范围。
通常,在网络中,有一个源地址和目标地址。
作为源地址,您需要选择绑定到网络接口的本地地址之一,或者
0.0.0.0
未指定的地址实际上是指任何本地地址。您广播的目标地址是
255.255.255.255
,对于有限的广播,仅是链接,或所有主机位设置为1
的子网前缀。如果您直言不讳地假设该地址是通过简单地将最后一个八位位设置为255 < /code>创建的,那么对于 /24以外的所有子网前缀,您都错了。例如,
172.16.0.0/26
具有广播地址172.16.0.63
,或172.16.0.128/27
使用172.16.0.159
。您不能将广播或多播地址用作源地址。
As Ron Maupin has aptly pointed out, broadcasting is deprecated and multicasting is the way to do it. If you're considering multicast you should be aware of the address scopes.
Generally in networking, there's a source address and a destination address.
As source address you need to pick one of your local addresses bound to a network interface, or the
0.0.0.0
unspecified address that actually refers to any local address.The destination address of your broadcast is either
255.255.255.255
for a limited broadcast that is on-link only, or the directed subnet broadcast = the subnet prefix with all host bits set to1
. If you're bluntly assuming that that address is created by simply setting the last octet to255
then you're wrong for all subnet prefixes other than /24. For example,172.16.0.0/26
has the broadcast address172.16.0.63
, or172.16.0.128/27
uses172.16.0.159
.You cannot use a broadcast or multicast address as source address.