英文:
Is a UDP broadcast actually a unicast?
问题
如果需要为广播指定特定端口,与单播有何不同呢?
广播的理念不是可以在事先不指定目的地的情况下发送数据包吗?
Python中的广播示例代码:
def send_udp_broadcast(message, port):
# 创建UDP套接字
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# 启用广播模式
udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
# 设置广播地址
broadcast_address = '<broadcast>'
server_address = (broadcast_address, port)
udp_socket.sendto(message.encode(), server_address)
我尝试进行广播而不指定端口,但似乎不可行。
英文:
If one has to specify a certain port for sending a broadcast, how is it different to a unicast?
Isn't the idea of a broadcast, that you can send a packet without specifying its destination in advance?
Example code for a broadcast in python:
def send_udp_broadcast(message, port):
# Create a UDP socket
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Enable broadcasting mode
udp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
# Set the broadcast address
broadcast_address = '<broadcast>'
server_address = (broadcast_address, port)
udp_socket.sendto(message.encode(), server_address)
I tried performing a broadcast without specifying a port, but it seems not possible.
答案1
得分: 2
TL;DR 地址确定哪台机器应该接收数据包。端口确定哪个服务在一台机器上应该接收数据包。
当数据包被传递到包含目的地的网络时,可能会发生以下两种情况:
- 路由器可以直接将数据包发送到目的地地址指示的接口。
- 数据包对该网络上的任何人都可用,但接口应忽略目的地地址与其自身地址不匹配的任何数据包。
在任一情况下,使用广播地址会导致:
- 路由器将数据包传递到网络上的每个接口。
- 每个接口将数据包识别为针对它自己的。
一旦接口拥有了一个数据包,端口被用来确定在机器上谁应该看到这个数据包。
英文:
TL;DR Addresses determine which machine should see the packet. Ports determine which service on a machine should see the packet.
When a packet is delivered to a network containing the destination, one of two things can happen:
- A router can send the packet directly to the interface indicated by the destination address.
- The packet is available to anyone on that network, but an interface is supposed to disregard any packet whose destination address does not match its own address.
In either case, using a broadcast address causes
- The router to deliver the packet to every interface on the network
- Each interface to recognize the packet as intended for it.
Once an interface has a packet, the port is used to determine who on the machine should see the packet.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论