0
我跑在Ubuntu Linux 16.04這個UDP客戶端程序:golang UDP連接拒絕每隔寫
package main
import (
"fmt"
"net"
"time"
"strconv"
)
func CheckError(err error) {
if err != nil {
fmt.Println("Error: " , err)
}
}
func main() {
ServerAddr,err := net.ResolveUDPAddr("udp","127.0.0.1:10001")
CheckError(err)
LocalAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
CheckError(err)
Conn, err := net.DialUDP("udp", LocalAddr, ServerAddr)
CheckError(err)
defer Conn.Close()
i := 0
for {
msg := strconv.Itoa(i)
i++
buf := []byte(msg)
_,err := Conn.Write(buf)
if err != nil {
fmt.Println(msg, err)
}
time.Sleep(time.Second * 1)
}
}
它產生這樣的輸出:
$ go run server.go
1 write udp 127.0.0.1:58703->127.0.0.1:10001: write: connection refused
3 write udp 127.0.0.1:58703->127.0.0.1:10001: write: connection refused
5 write udp 127.0.0.1:58703->127.0.0.1:10001: write: connection refused
但我預計輸出,而不是:
$ go run server.go
1 write udp 127.0.0.1:58703->127.0.0.1:10001: write: connection refused
2 write udp 127.0.0.1:58703->127.0.0.1:10001: write: connection refused
3 write udp 127.0.0.1:58703->127.0.0.1:10001: write: connection refused
4 write udp 127.0.0.1:58703->127.0.0.1:10001: write: connection refused
5 write udp 127.0.0.1:58703->127.0.0.1:10001: write: connection refused
tcpdump說:
15:28:46.453313 IP localhost.47993 > localhost.10001: UDP, length 1
15:28:46.453338 IP localhost > localhost: ICMP localhost udp port 10001 unreachable, length 37
15:28:48.453821 IP localhost.47993 > localhost.10001: UDP, length 1
15:28:48.453852 IP localhost > localhost: ICMP localhost udp port 10001 unreachable, length 37
15:28:50.454242 IP localhost.47993 > localhost.10001: UDP, length 1
15:28:50.454271 IP localhost > localhost: ICMP localhost udp port 10001 unreachable, length 37
爲什麼每隔一段時間都會發生這種情況conn.Write寫入而不是每次?我不是在指責,我只是想知道爲什麼。
你能分享你的服務器如何處理連接?看起來像被阻止或類似的東西 –
除非你有一個要求,通常建議避免使用連接的UDP套接字。 – JimB
@JimB這是什麼「連接的UDP套接字」我認爲UDP是無連接的?他們通常建議避免它在哪裏? – selden