protohackers/pkg/conn/conn.go

34 lines
419 B
Go
Raw Normal View History

2022-11-07 19:01:00 +00:00
package conn
import (
"fmt"
"net"
)
const (
Port = ":3030"
Type = "tcp4"
)
func StartSimple(handler func(conn net.Conn)) error {
l, err := net.Listen(Type, Port)
if err != nil {
return err
}
defer l.Close()
fmt.Println(fmt.Sprintf("Listening on port %s", Port))
for {
c, err := l.Accept()
if err != nil {
return err
}
go handler(c)
err = c.Close()
if err != nil {
return err
}
}
}