diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..a6a0782 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch Package", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "main.go", + "env": { + "PORT": "4269" + } + } + ] +} \ No newline at end of file diff --git a/main.go b/main.go index 4a699dc..7cb0a2c 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,14 @@ package main import ( + "errors" "fmt" - "io" "net" "os" ) +var EOF = errors.New("EOF") + func main() { ln, err := net.Listen("tcp", ":"+os.Getenv("PORT")) if err != nil { @@ -28,7 +30,22 @@ func main() { func handle(conn net.Conn) { defer conn.Close() - if _, err := io.Copy(conn, conn); err != nil { - fmt.Println("copy: ", err.Error()) + buff := make([]byte, 32*1024) + for { + messageLength, err := conn.Read(buff) + + if err != nil { + if err != EOF { + fmt.Println("Error reading data: ", err) + } + break + } + + _, err = conn.Write(buff[0:messageLength]) + + if err != nil { + fmt.Println("Error writing data: ", err) + break + } } }