-
Notifications
You must be signed in to change notification settings - Fork 176
/
reverb.go
49 lines (45 loc) · 911 Bytes
/
reverb.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// ex8.4 is a reverb server that uses sync.WaitGroup to choose when to close
// connections.
package main
import (
"bufio"
"fmt"
"log"
"net"
"strings"
"sync"
"time"
)
func echo(c net.Conn, shout string, delay time.Duration, wg sync.WaitGroup) {
fmt.Fprintln(c, "\t", strings.ToUpper(shout))
time.Sleep(delay)
fmt.Fprintln(c, "\t", shout)
time.Sleep(delay)
fmt.Fprintln(c, "\t", strings.ToLower(shout))
wg.Done()
}
func handleConn(c net.Conn) {
wg := sync.WaitGroup{}
input := bufio.NewScanner(c)
for input.Scan() {
wg.Add(1)
go echo(c, input.Text(), 1*time.Second, wg)
}
wg.Wait()
// NOTE: ignoring potential errors from input.Err()
c.Close()
}
func main() {
l, err := net.Listen("tcp", "localhost:8000")
if err != nil {
log.Fatal(err)
}
for {
conn, err := l.Accept()
if err != nil {
log.Print(err) // e.g., connection aborted
continue
}
go handleConn(conn)
}
}