diff --git a/sockets/go.mod b/sockets/go.mod new file mode 100644 index 0000000..4570719 --- /dev/null +++ b/sockets/go.mod @@ -0,0 +1,3 @@ +module sockets + +go 1.22.1 diff --git a/sockets/main.go b/sockets/main.go new file mode 100644 index 0000000..163d189 --- /dev/null +++ b/sockets/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "bufio" + "fmt" + "net" + "strings" +) + +func main() { + ln, err := net.Listen("tcp", ":1302") + catchErr(err) + fmt.Println("Listening on port 1302") + + conn, err := ln.Accept() + catchErr(err) + + for { + message, err := bufio.NewReader(conn).ReadString('\n') + catchErr(err) + fmt.Printf("Message received: %v", string(message)) + newMsg := strings.ToUpper(message) + conn.Write([]byte(newMsg + "\n")) + } +} + +func catchErr(err error) (errHappened bool) { + errHappened = false + if err != nil { + errHappened = true + fmt.Println(err) + } + return errHappened +}