UDP广播实际上是单播吗?

huangapple go评论64阅读模式
英文:

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 = &#39;&lt;broadcast&gt;&#39;
 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 地址确定哪台机器应该接收数据包。端口确定哪个服务在一台机器上应该接收数据包。


当数据包被传递到包含目的地的网络时,可能会发生以下两种情况:

  1. 路由器可以直接将数据包发送到目的地地址指示的接口。
  2. 数据包对该网络上的任何人都可用,但接口应忽略目的地地址与其自身地址不匹配的任何数据包。

在任一情况下,使用广播地址会导致:

  1. 路由器将数据包传递到网络上的每个接口。
  2. 每个接口将数据包识别为针对它自己的。

一旦接口拥有了一个数据包,端口被用来确定在机器上谁应该看到这个数据包。

英文:

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:

  1. A router can send the packet directly to the interface indicated by the destination address.
  2. 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

  1. The router to deliver the packet to every interface on the network
  2. 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.

huangapple
  • 本文由 发表于 2023年6月15日 19:45:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定