Added sockets exercise

This commit is contained in:
raul 2024-03-09 19:35:26 +01:00
parent 43e9f4c6c4
commit a4a5594096
2 changed files with 37 additions and 0 deletions

3
sockets/go.mod Normal file
View File

@ -0,0 +1,3 @@
module sockets
go 1.22.1

34
sockets/main.go Normal file
View File

@ -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
}