echo logged server errors back to client as well

This commit is contained in:
Gokcehan 2021-05-21 16:54:01 +03:00
parent 7cdb7495ea
commit 7acb69c40a

View File

@ -53,6 +53,15 @@ func listen(l net.Listener) {
} }
} }
func echoerr(c net.Conn, msg string) {
fmt.Fprintln(c, msg)
log.Printf(msg)
}
func echoerrf(c net.Conn, format string, a ...interface{}) {
echoerr(c, fmt.Sprintf(format, a...))
}
func handleConn(c net.Conn) { func handleConn(c net.Conn) {
s := bufio.NewScanner(c) s := bufio.NewScanner(c)
@ -66,24 +75,24 @@ Loop:
word2, _ := splitWord(rest) word2, _ := splitWord(rest)
id, err := strconv.Atoi(word2) id, err := strconv.Atoi(word2)
if err != nil { if err != nil {
log.Print("listen: conn: client id should be a number") echoerr(c, "listen: conn: client id should be a number")
} else { } else {
gConnList[id] = c gConnList[id] = c
} }
} else { } else {
log.Print("listen: conn: requires a client id") echoerr(c, "listen: conn: requires a client id")
} }
case "drop": case "drop":
if rest != "" { if rest != "" {
word2, _ := splitWord(rest) word2, _ := splitWord(rest)
id, err := strconv.Atoi(word2) id, err := strconv.Atoi(word2)
if err != nil { if err != nil {
log.Print("listen: drop: client id should be a number") echoerr(c, "listen: drop: client id should be a number")
} else { } else {
delete(gConnList, id) delete(gConnList, id)
} }
} else { } else {
log.Print("listen: drop: requires a client id") echoerr(c, "listen: drop: requires a client id")
} }
case "send": case "send":
if rest != "" { if rest != "" {
@ -114,12 +123,12 @@ Loop:
gListener.Close() gListener.Close()
break Loop break Loop
default: default:
log.Printf("listen: unexpected command: %s", word) echoerrf(c, "listen: unexpected command: %s", word)
} }
} }
if s.Err() != nil { if s.Err() != nil {
log.Printf("listening: %s", s.Err()) echoerrf(c, "listening: %s", s.Err())
} }
c.Close() c.Close()