I have been in DevOps related jobs for past 6 years dealing mainly with Kubernetes in AWS and on-premise as well. I spent quite a lot …
:date_long | 1 min Read
Go simple TCP server
package main
import (
"fmt"
"time"
"log"
"net"
"bufio"
)
func main() {
li, err := net.Listen("tcp", ":8080")
if err != nil {
log.Panic(err)
}
defer li.Close()
for {
conn, err := li.Accept()
if err != nil {
log.Println(err)
}
go handle(conn)
}
}
func handle(conn net.Conn) {
err := conn.SetDeadline(time.Now().Add(10 * time.Second))
if err != nil {
log.Println("Connection timeout!")
}
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
ln := scanner.Text()
fmt.Println(ln)
fmt.Fprintf(conn, "I heard you say: %s\n", ln)
}
defer conn.Close()
fmt.Println("Code got here.")
}