cleanup handleRequest

This commit is contained in:
Evan Burkey 2022-11-07 10:43:37 -08:00
parent 4e9dffe5ad
commit 203c3b9704
1 changed files with 8 additions and 10 deletions

View File

@ -2,19 +2,21 @@ package main
import ( import (
"fmt" "fmt"
"io"
"log"
"net" "net"
"os" "os"
) )
const ( const (
connPort = "3030" connPort = "3030"
connType = "tcp" connType = "tcp4"
) )
func main() { func main() {
l, err := net.Listen(connType, ":"+connPort) l, err := net.Listen(connType, ":"+connPort)
if err != nil { if err != nil {
fmt.Printf("net.Listen fail: %v\n", err) log.Fatalln(err)
os.Exit(1) os.Exit(1)
} }
defer l.Close() defer l.Close()
@ -24,19 +26,15 @@ func main() {
for { for {
conn, err := l.Accept() conn, err := l.Accept()
if err != nil { if err != nil {
fmt.Printf("Error accepting: %v\n", err) log.Fatalln(err)
} }
go handleRequest(conn) go handleRequest(conn)
} }
} }
func handleRequest(conn net.Conn) { func handleRequest(conn net.Conn) {
buf := make([]byte, 1024) defer conn.Close()
_, err := conn.Read(buf) if _, err := io.Copy(conn, conn); err != nil {
fmt.Printf("read in %s.\nSending it back\n", string(buf)) log.Fatalln(err)
if err != nil {
fmt.Printf("Error reading: %v\n", err)
} }
conn.Write(buf)
conn.Close()
} }