Merge pull request 'smoketest' (#1) from smoketest into master

Reviewed-on: https://git.fputs.com/fputs/protohackers/pulls/1
This commit is contained in:
Evan Burkey 2022-11-07 19:45:44 +01:00
commit 3556511a3a
1 changed files with 10 additions and 12 deletions

View File

@ -2,41 +2,39 @@ package main
import ( import (
"fmt" "fmt"
"io"
"log"
"net" "net"
"os" "os"
) )
const ( const (
connHost = "localhost"
connPort = "3030" connPort = "3030"
connType = "tcp" connType = "tcp4"
) )
func main() { func main() {
l, err := net.Listen(connType, connHost+":"+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()
fmt.Println(fmt.Sprintf("Listening on %s:%s", connHost, connPort)) fmt.Println(fmt.Sprintf("Listening on port %s", connPort))
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 {
if err != nil { log.Fatalln(err)
fmt.Printf("Error reading: %v\n", err)
} }
conn.Write(buf)
conn.Close()
} }