Golang的TCP连接正常工作,但UDP连接不正常。

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

Golang TCP connection works, but UDP does not

问题

我正在通过netcat监听,命令如下:
nc -lkp 1902

每当我建立一个TCP连接并尝试发送日志时,它都能正常工作。

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 message1
example-2022/11/18 technique24.go:22: This is a regular message2
example-2022/11/18 technique24.go:23: This is a regular message3
example-2022/11/18 technique24.go:24: This is a regular message4
example-2022/11/18 technique24.go:25: This is a regular message5
example-2022/11/18 technique24.go:26: This is a regular message6

但是,当我尝试建立一个UDP连接时,它不起作用,为什么我的日志记录器上没有任何输出呢?

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")

我想创建一个小的POC来通过UDP发送日志以减少积压,我尝试先建立一个TCP连接,它可以正常工作,但UDP不起作用,有人能解释一下我需要做什么才能使其工作吗?

英文:

I'm listening via netcat as such
nc -lkp 1902

Whenever I make a tcp connection and try to send logs it works

        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")

Output

example-2022/11/18 technique24.go:21: This is a regular message1
example-2022/11/18 technique24.go:22: This is a regular message2
example-2022/11/18 technique24.go:23: This is a regular message3
example-2022/11/18 technique24.go:24: This is a regular message4
example-2022/11/18 technique24.go:25: This is a regular message5
example-2022/11/18 technique24.go:26: This is a regular message6

But whenever I try to make a udp connection it does not work, could anyone explain why I get nothing on my logger?

        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")

Want to make a small poc for sending logs over udp to reduce backlog, tried to make a tcp connection first and it works fine but udp does not work, could anyone explain what I have to do to make it work?

答案1

得分: 0

Netcat默认创建TCP连接,除非另有指定。对于UDP连接,您需要使用netcat的-u标志。从netcat的man页面中可以看到:

> -u 使用UDP而不是默认的TCP选项。

因此,将您的监听器更改为nc -luk 1902应该可以解决UDP连接的问题。

英文:

Netcat by default creates a TCP connection unless specified otherwise. For UDP connections, you need to use the -u flag of the netcat. From man page of netcat

> -u Use UDP instead of the default option of TCP.

So changing your listener to nc -luk 1902 should fix the issue for UDP connections.

huangapple
  • 本文由 发表于 2022年11月18日 18:04:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/74487955.html
匿名

发表评论

匿名网友

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

确定