echo server
All checks were successful
Build and Publish / BuildAndDeployAmd64 (push) Successful in 28s
Build and Publish / BuildAndDeploy (push) Successful in 2m35s

This commit is contained in:
matst80
2024-11-21 20:16:41 +01:00
parent bc8ffd1878
commit 895502824f
2 changed files with 42 additions and 0 deletions

28
main.go
View File

@@ -3,6 +3,7 @@ package main
import (
"fmt"
"log"
"net"
"net/http"
"net/http/pprof"
"os"
@@ -186,6 +187,33 @@ func main() {
w.Write([]byte("1.0.0"))
})
l, err := net.Listen("tcp", ":1234")
if err == nil {
go func(listener net.Listener) {
conn, err := l.Accept()
if err != nil {
log.Fatalf("Error accepting echo connection: %v\n", err)
}
go func(c net.Conn) {
defer c.Close()
buf := make([]byte, 1024)
for {
n, err := c.Read(buf)
if err != nil {
log.Fatalf("Error reading from connection: %v\n", err)
}
_, err = c.Write(buf[:n])
if err != nil {
log.Fatalf("Error writing to connection: %v\n", err)
}
}
}(conn)
}(l)
} else {
log.Println("Error creating echo server: %v\n", err)
}
sigs := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(sigs, syscall.SIGTERM)