From 4e9dffe5adb8527ba22afd56e9a1ebf03406f874 Mon Sep 17 00:00:00 2001 From: Evan Burkey Date: Mon, 7 Nov 2022 10:30:51 -0800 Subject: [PATCH 1/2] add readout, remove localhost --- cmd/smoketest/main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/smoketest/main.go b/cmd/smoketest/main.go index 0ec2dde..3b86a2e 100644 --- a/cmd/smoketest/main.go +++ b/cmd/smoketest/main.go @@ -7,20 +7,19 @@ import ( ) const ( - connHost = "localhost" connPort = "3030" connType = "tcp" ) func main() { - l, err := net.Listen(connType, connHost+":"+connPort) + l, err := net.Listen(connType, ":"+connPort) if err != nil { fmt.Printf("net.Listen fail: %v\n", err) os.Exit(1) } defer l.Close() - fmt.Println(fmt.Sprintf("Listening on %s:%s", connHost, connPort)) + fmt.Println(fmt.Sprintf("Listening on port %s", connPort)) for { conn, err := l.Accept() @@ -34,6 +33,7 @@ func main() { func handleRequest(conn net.Conn) { buf := make([]byte, 1024) _, err := conn.Read(buf) + fmt.Printf("read in %s.\nSending it back\n", string(buf)) if err != nil { fmt.Printf("Error reading: %v\n", err) } From 203c3b970459c84ee6c441521b852b1d27f1d612 Mon Sep 17 00:00:00 2001 From: Evan Burkey Date: Mon, 7 Nov 2022 10:43:37 -0800 Subject: [PATCH 2/2] cleanup handleRequest --- cmd/smoketest/main.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/cmd/smoketest/main.go b/cmd/smoketest/main.go index 3b86a2e..10f924b 100644 --- a/cmd/smoketest/main.go +++ b/cmd/smoketest/main.go @@ -2,19 +2,21 @@ package main import ( "fmt" + "io" + "log" "net" "os" ) const ( connPort = "3030" - connType = "tcp" + connType = "tcp4" ) func main() { l, err := net.Listen(connType, ":"+connPort) if err != nil { - fmt.Printf("net.Listen fail: %v\n", err) + log.Fatalln(err) os.Exit(1) } defer l.Close() @@ -24,19 +26,15 @@ func main() { for { conn, err := l.Accept() if err != nil { - fmt.Printf("Error accepting: %v\n", err) + log.Fatalln(err) } go handleRequest(conn) } } func handleRequest(conn net.Conn) { - buf := make([]byte, 1024) - _, err := conn.Read(buf) - fmt.Printf("read in %s.\nSending it back\n", string(buf)) - if err != nil { - fmt.Printf("Error reading: %v\n", err) + defer conn.Close() + if _, err := io.Copy(conn, conn); err != nil { + log.Fatalln(err) } - conn.Write(buf) - conn.Close() }