我正在通過(guò) netcat 收聽(tīng) nc -lkp 1902每當(dāng)我建立 tcp 連接并嘗試發(fā)送日志時(shí),它都會(huì)工作 timeout := 30 * time.Second conn, err := net.DialTimeout("tcp", "localhost:1902", timeout) if err != nil { panic("Failed to connect to localhost:1902") } defer conn.Close() f := log.Ldate | log.Lshortfile logger := log.New(conn, "example-", f) logger.Println("This is a regular message1") logger.Println("This is a regular message2") logger.Println("This is a regular message3") logger.Println("This is a regular message4") logger.Println("This is a regular message5") logger.Println("This is a regular message6")輸出example-2022/11/18 technique24.go:21: This is a regular message1example-2022/11/18 technique24.go:22: This is a regular message2example-2022/11/18 technique24.go:23: This is a regular message3example-2022/11/18 technique24.go:24: This is a regular message4example-2022/11/18 technique24.go:25: This is a regular message5example-2022/11/18 technique24.go:26: This is a regular message6但是每當(dāng)我嘗試建立 udp 連接時(shí)它都不起作用,誰(shuí)能解釋為什么我的記錄器上什么也沒(méi)有? timeout := 30 * time.Second conn, err := net.DialTimeout("udp", "localhost:1902", timeout) if err != nil { panic("Failed to connect to localhost:1902") } defer conn.Close() f := log.Ldate | log.Lshortfile logger := log.New(conn, "example-", f) logger.Println("This is a regular message1") logger.Println("This is a regular message2") logger.Println("This is a regular message3") logger.Println("This is a regular message4") logger.Println("This is a regular message5") logger.Println("This is a regular message6")想要通過(guò) udp 發(fā)送日志以減少積壓,嘗試先建立一個(gè) tcp 連接并且它工作正常但 udp 不起作用,任何人都可以解釋我必須做些什么才能讓它工作嗎?
Golang TCP 連接有效,但 UDP 無(wú)效
婷婷同學(xué)_
2023-03-15 15:27:34