可以把第一次发送 datagram (对于另一方来说是第一次接收)视作双方建立了「连接」,一方不再和另一方通讯视作「连接」断开。这样强行定义的连接的好处是很多时候我们关心的是双方之间的通讯,至于这个通讯是 TCP 还是 UDP 其实是实现细节,UDP 也定义连接就可以和 TCP 用差不多的方式处理,某些时候这样比较方便。
拿 Go 来说,UDP 也可以 Dial,也会返回 Conn,这个 Conn 同样也需要 close
> func Dial(network, address string) (Conn, error)
> Dial connects to the address on the named network.
> Known networks are "tcp", "tcp4" (IPv4-only), "tcp6" (IPv6-only), "udp", "udp4" (IPv4-only), "udp6" (IPv6-only), "ip", "ip4" (IPv4-only), "ip6" (IPv6-only), "unix", "unixgram" and "unixpacket".
https://pkg.go.dev/net#Dial