Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
553 views
in Technique[技术] by (71.8m points)

go - Golang: ReadFromUDP behavior when source port differs in reponse from dial

i have written the following simple udp server/client in golang. The app sends the current time to a specified ipv6 link-local addrress. The receiver sends back a little reply. This works ONLY if the sender port of the reply is the same as the destination port of the request.

Wireshark Comparison: https://www.dropbox.com/s/ypaepz62sa4xtnh/go_simple_udp3.png?dl=0

Why is this the case?

package main

import (
"net"
"log"
"fmt"
"time"
)

func main() {

//Remote Addr
BoxAddr, err := net.ResolveUDPAddr("udp6", "[fe80:0000:0000:0000:0211:7d00:0030:8e3f]:5684")
if(err != nil) {
    log.Fatal(err)
    return
}

/*
LocalAddr, err := net.ResolveUDPAddr("udp6", "[fe80::299f:4146:2e32:72fd%4]:50361")
if(err != nil) {
    log.Fatal(err)
    return
}
*/
c, err := net.DialUDP("udp6", nil, BoxAddr)
if(err != nil) {
    log.Fatal(err)
    return
}

defer c.Close()

fmt.Print(c.LocalAddr())

//read loop
go func(c *net.UDPConn) {

    for {
        rxMsg := make([]byte, 100);
        n,_, err := c.ReadFromUDP(rxMsg)
        if(err != nil) {
            log.Fatal(err)
            return
        } else {
            fmt.Print("Got from remote: ",n, " bytes
")
        }
    }
}(c)

//write every second current time
for {
    b := []byte(time.Now().String())
    n, err := c.Write(b)

    if(err != nil) {
        log.Fatal(err)
        return
    } else {
        fmt.Print("Wrote to destination socket: ",n, " bytes
")
    }

    time.Sleep(1000 * time.Millisecond)
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...