Added sockets exercise
This commit is contained in:
parent
43e9f4c6c4
commit
a4a5594096
|
@ -0,0 +1,3 @@
|
|||
module sockets
|
||||
|
||||
go 1.22.1
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue