initial commit
This commit is contained in:
4
server/README.MD
Normal file
4
server/README.MD
Normal file
@ -0,0 +1,4 @@
|
||||
# leaky-pool server
|
||||
|
||||
Listens on port 8080 for new connections, accepts them and then closes them after one minute.
|
||||
|
3
server/go.mod
Normal file
3
server/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module deadbeef.codes/steven/leaky-pool/server
|
||||
|
||||
go 1.20
|
53
server/main.go
Normal file
53
server/main.go
Normal file
@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
listenerAddr, err := net.ResolveTCPAddr("tcp4", ":6699")
|
||||
if err != nil {
|
||||
log.Fatalf("could not resolve listenAddr: %v", err)
|
||||
}
|
||||
|
||||
listener, err := net.ListenTCP("tcp", listenerAddr)
|
||||
if err != nil {
|
||||
log.Fatalf("could not listen on address '%s': %v", listenerAddr.String(), err)
|
||||
}
|
||||
|
||||
for {
|
||||
conn, err := listener.Accept()
|
||||
if err != nil {
|
||||
log.Printf("failed to accept new incoming connection: %v", err) // if logging facility has latency this will block
|
||||
continue
|
||||
}
|
||||
go connHandler(conn) // branch off into goroutine so we're not blocking
|
||||
}
|
||||
}
|
||||
|
||||
func connHandler(conn net.Conn) {
|
||||
log.Printf("accepted new connection from: %s", conn.RemoteAddr().String())
|
||||
|
||||
conn.SetReadDeadline(time.Now().Add(time.Second))
|
||||
|
||||
for {
|
||||
|
||||
buf := make([]byte, 128)
|
||||
bytesRead, err := conn.Read(buf)
|
||||
if err == nil && bytesRead > 0 {
|
||||
log.Printf("Incoming message over socket '%s': %s", conn.RemoteAddr().String(), string(buf))
|
||||
}
|
||||
|
||||
_, err = conn.Write([]byte("1"))
|
||||
if err != nil {
|
||||
log.Printf("failed writing to connection '%s': %v", conn.RemoteAddr().String(), err)
|
||||
conn.Close()
|
||||
break
|
||||
}
|
||||
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user