protohackers/cmd/smoketest/main.go

43 lines
681 B
Go
Raw Normal View History

2022-11-07 18:23:09 +00:00
package main
2022-11-07 16:56:16 +00:00
import (
"fmt"
"net"
"os"
)
const (
connPort = "3030"
connType = "tcp"
)
func main() {
2022-11-07 18:30:51 +00:00
l, err := net.Listen(connType, ":"+connPort)
2022-11-07 16:56:16 +00:00
if err != nil {
fmt.Printf("net.Listen fail: %v\n", err)
os.Exit(1)
}
defer l.Close()
2022-11-07 18:30:51 +00:00
fmt.Println(fmt.Sprintf("Listening on port %s", connPort))
2022-11-07 16:56:16 +00:00
for {
conn, err := l.Accept()
if err != nil {
fmt.Printf("Error accepting: %v\n", err)
}
go handleRequest(conn)
}
}
func handleRequest(conn net.Conn) {
buf := make([]byte, 1024)
_, err := conn.Read(buf)
2022-11-07 18:30:51 +00:00
fmt.Printf("read in %s.\nSending it back\n", string(buf))
2022-11-07 16:56:16 +00:00
if err != nil {
fmt.Printf("Error reading: %v\n", err)
}
conn.Write(buf)
conn.Close()
}