protohackers/cmd/smoketest/main.go

41 lines
533 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"
2022-11-07 18:43:37 +00:00
"io"
"log"
2022-11-07 16:56:16 +00:00
"net"
"os"
)
const (
connPort = "3030"
2022-11-07 18:43:37 +00:00
connType = "tcp4"
2022-11-07 16:56:16 +00:00
)
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 {
2022-11-07 18:43:37 +00:00
log.Fatalln(err)
2022-11-07 16:56:16 +00:00
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 {
2022-11-07 18:43:37 +00:00
log.Fatalln(err)
2022-11-07 16:56:16 +00:00
}
go handleRequest(conn)
}
}
func handleRequest(conn net.Conn) {
2022-11-07 18:43:37 +00:00
defer conn.Close()
if _, err := io.Copy(conn, conn); err != nil {
log.Fatalln(err)
2022-11-07 16:56:16 +00:00
}
}