在 macOS 上的 Go 应用程序中无法接收 UDP 数据包

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

Unable to receive UDP packets in Go app on macOS

问题

我目前遇到了一个问题,我的Go应用程序在macOS上无法接收UDP数据包。我使用net包实现了一个基本的UDP监听器,但是尽管应用程序报告它正在监听指定的UDP端口,但当我将数据包发送到我的IP地址时,它却没有接收到任何数据包。

以下是部分代码(没有错误检查):

addr, err := net.ResolveUDPAddr("udp", ":1234")
conn, err := net.ListenUDP("udp", addr)
defer conn.Close()

buffer := make([]byte, 1024)
n, _, err := conn.ReadFromUDP(buffer)
fmt.Println("Packet Received!") //永远不会执行到这一行

我已经完成了以下操作:

  • 验证了我的macOS机器上的防火墙已关闭。
  • 使用nc命令行工具测试接收UDP数据包,在从运行Go应用程序的同一台机器发送数据包到本地主机(127.0.0.1)时成功。
  • 但是,当我使用nc连接到计算机的自身IP地址时,Go应用程序没有接收到任何数据包。
  • 此外,我尝试在同一台机器上实现一个Python UDP监听器,当发送到我的IP地址时,它成功接收到UDP数据包。
  • 我还在其他计算机上进行了测试,一切正常。

现在我只需要它显示文本。
我认为它应该接收到UDP数据包,并在之后打印出文本。

英文:

I am currently facing an issue with my Go application on macOS where I am unable to receive UDP packets. I have implemented a basic UDP listener using the net package, but despite the application reporting that it is listening on the specified UDP port, it does not receive any packets when sending them to my IP address.

Here is part of the code (without error checking):

addr, err := net.ResolveUDPAddr("udp", ":1234")
conn, err := net.ListenUDP("udp", addr)
defer conn.Close()

buffer := make([]byte, 1024)
n, _, err := conn.ReadFromUDP(buffer)
fmt.Println("Packet Received!") //never reaches this line

I already did this:

  • Verified that the firewall on my macOS machine is turned off.
  • Tested receiving UDP packets using the nc command-line tool, which was successful when sending packets to localhost (127.0.0.1) from the same machine where the Go application is running.
  • However, when I used nc to connect to the computer's own IP address from the same machine, the Go application did not receive any packets.
  • Additionally, I tried implementing a Python UDP listener on the same machine, and it successfully received UDP packets when sent to my IP address.
  • I also tested it on other computers and it worked fine.

Right now I just need it to show the text.
I think it should receive the udp packet, and print the text after.

答案1

得分: 0

问题与Podman相关。卸载Podman后,应用程序成功接收UDP数据包。

值得注意的是,我不确定Docker或其他容器化工具是否会出现相同的问题。然而,在我的特定情况下,移除Podman解决了这个问题。

英文:

The issue was related to Podman. Upon uninstalling it,the application started receiving UDP packets successfully.
It's worth noting that I'm unsure if the same problem occurs with Docker or other containerization tools. However, in my specific case, removing Podman resolved the issue.

huangapple
  • 本文由 发表于 2023年6月8日 20:02:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431670.html
匿名

发表评论

匿名网友

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

确定