forked from unknwon/the-way-to-go_ZH_CN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client1.go
executable file
·46 lines (40 loc) · 1.06 KB
/
client1.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
package main
import (
"bufio"
"fmt"
"net"
"os"
"strings"
)
func main() {
var conn net.Conn
var error error
var inputReader *bufio.Reader
var input string
var clientName string
// maak connectie met de server:
conn, error = net.Dial("tcp", "localhost:50000")
checkError(error)
inputReader = bufio.NewReader(os.Stdin)
fmt.Println("First, what is your name?")
clientName, _ = inputReader.ReadString('\n')
// fmt.Printf("CLIENTNAME %s",clientName)
trimmedClient := strings.Trim(clientName, "\r\n") // "\r\n" voor Windows, "\n" voor Linux
for {
fmt.Println("What to send to the server? Type Q to quit. Type SH to shutdown server.")
input, _ = inputReader.ReadString('\n')
trimmedInput := strings.Trim(input, "\r\n")
// fmt.Printf("input:--%s--",input)
// fmt.Printf("trimmedInput:--%s--",trimmedInput)
if trimmedInput == "Q" {
return
}
_, error = conn.Write([]byte(trimmedClient + " says: " + trimmedInput))
checkError(error)
}
}
func checkError(error error) {
if error != nil {
panic("Error: " + error.Error()) // terminate program
}
}