为什么我的UDP拨号器没有监听功能?

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

Why isn't my UDP dialer also listening?

问题

我有两个程序,一个是监听器(Listener),一个是拨号器(Dialer)。我想在同一个端口上实现双向的UDP通信
我的监听器按预期从拨号器接收数据报,并发送自己的5个数据报。唯一的问题是,我的拨号器没有读取到它发送的数据。

我尝试使用net.DialUDP,但使用它时,拨号器没有发送任何数据报。

监听器 - 工作正常

func main() {
    addr := net.UDPAddr{
        Port: 7000,
        IP:   net.ParseIP("127.0.0.1"),
    }
    conn, err := net.ListenUDP("udp", &addr)

    defer conn.Close()
    if err != nil {
        panic(err)
    }

    i := 0

    b := make([]byte, 10)
    conn.ReadFromUDP(b)
    fmt.Println(string(b[:]))
    for i < 5 {
        conn.WriteToUDP([]byte("sending back"), &addr)
        i++
    }
}

拨号器发送数据报但无法读取它们

func main() {
    sock, _ := net.Dial("udp", "127.0.0.1:7000")
    buf := make([]byte, 100)

    _, werr := sock.Write([]byte("first send"))
    if werr != nil {
        fmt.Println(werr)
    }
    sock.Read(buf)

    fmt.Println(string(buf[:]))
}

拨号器没有发送任何数据报

func main() {
    remote, _ := net.ResolveUDPAddr("udp", "127.0.0.1:7000")
    sock, _ := net.DialUDP("udp", nil, remote)

    buf := make([]byte, 10)

    for {
        sock.WriteToUDP([]byte("first send"), remote)
        sock.ReadFromUDP(buf)
        fmt.Println(string(buf[:]))
    }
}

以上是你提供的代码的翻译。

英文:

I have two programs, a listener and a dialer. I want a two way street of UDP communication on the same port.
My Listener as expected reads the datagram sent from the dialer, then sends back 5 datagrams of its own. Only trouble is, my dialer isn't reading it.

I tried using net.DialUDP but when I use that, 0 datagrams get sent from the dialer.

listener - Works great

func main() {

addr := net.UDPAddr{
	Port: 7000,
	IP:   net.ParseIP(&quot;127.0.0.1&quot;),
}
conn, err := net.ListenUDP(&quot;udp&quot;, &amp;addr)

defer conn.Close()
if err != nil {
	panic(err)
}

i := 0

b := make([]byte, 10)
conn.ReadFromUDP(b)
fmt.Println(string(b[:]))
for i &lt; 5 {

	conn.WriteToUDP([]byte(&quot;sending back&quot;), &amp;addr)
	i++
}
}

Dialer that sends datagram but cannot read them

func main() {
sock, _ := net.Dial(&quot;udp&quot;, &quot;127.0.0.1:7000&quot;)
buf := make([]byte, 100)

_, werr := sock.Write([]byte(&quot;first send&quot;))
if werr != nil {
	fmt.Println(werr)
}
sock.Read(buf)

fmt.Println(string(buf[:]))
}

Dialer that doesn't send any datagrams

func main() {

remote, _ := net.ResolveUDPAddr(&quot;udp&quot;, &quot;127.0.0.1:7000&quot;)
sock, _ := net.DialUDP(&quot;udp&quot;, nil, remote)

buf := make([]byte, 10)

for {
	sock.WriteToUDP([]byte(&quot;first send&quot;), remote)
	sock.ReadFromUDP(buf)
	fmt.Println(string(buf[:]))

}
}

答案1

得分: 2

当你不确定时,只需使用ListenUDP。它既可以发送也可以接收数据报。

sock, _ := net.Dial("udp", "127.0.0.1:7000")

这将创建一个net.Conn,它只是一个基本的连接接口。你必须将其断言为*net.UDPConn才能使用实际的UDP方法。

sock, _ := net.DialUDP("udp", nil, remote)

这将创建一个“已连接”的UDP套接字,并使用裸露的Write方法向单个远程地址发送数据。

英文:

When in doubt, just use ListenUDP. It will both send and receive datagrams.

sock, _ := net.Dial(&quot;udp&quot;, &quot;127.0.0.1:7000&quot;)

This creates a net.Conn, which is only a basic connection interface. You have to assert it as a *net.UDPConn to get the actual UDP methods to work.

sock, _ := net.DialUDP(&quot;udp&quot;, nil, remote)

This creates a "connected" UDP socket, and uses the bare Write method to send to a single remote address.

huangapple
  • 本文由 发表于 2014年11月28日 08:15:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/27180293.html
匿名

发表评论

匿名网友

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

确定