forked from unknwon/the-way-to-go_ZH_CN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server1.go
executable file
·75 lines (66 loc) · 1.74 KB
/
server1.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"net"
"os"
"strings"
)
// Map of the clients: contains: clientname - 1 (active) / 0 - (inactive)
var mapUsers map[string]int
func main() {
var listener net.Listener
var error error
var conn net.Conn
mapUsers = make(map[string]int)
fmt.Println("Starting the server ...")
// create listener:
listener, error = net.Listen("tcp", "localhost:50000")
checkError(error)
// listen and accept connections from clients:
for {
conn, error = listener.Accept()
checkError(error)
go doServerStuff(conn)
}
}
func doServerStuff(conn net.Conn) {
var buf []byte
var error error
for {
buf = make([]byte, 512)
_, error = conn.Read(buf)
checkError(error)
input := string(buf)
if strings.Contains(input, ": SH") {
fmt.Println("Server shutting down.")
os.Exit(0)
}
// op commando WHO: write out mapUsers
if strings.Contains(input, ": WHO") {
DisplayList()
}
// extract clientname:
ix := strings.Index(input, "says")
clName := input[0 : ix-1]
//fmt.Printf("The clientname is ---%s---\n", string(clName))
// set clientname active in mapUsers:
mapUsers[string(clName)] = 1
fmt.Printf("Received data: --%v--", string(buf))
}
}
// advantage: code is cleaner,
// disadvantage: the server process has to stop at any error:
// a simple return continues in the function where we came from!
func checkError(error error) {
if error != nil {
panic("Error: " + error.Error()) // terminate program
}
}
func DisplayList() {
fmt.Println("--------------------------------------------")
fmt.Println("This is the client list: 1=active, 0=inactive")
for key, value := range mapUsers {
fmt.Printf("User %s is %d\n", key, value)
}
fmt.Println("--------------------------------------------")
}