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?
CodePudding user response:
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.