-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslaver.go
60 lines (51 loc) · 1.02 KB
/
slaver.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
package main
import (
"bufio"
"fmt"
"net"
"os"
)
var writeStr, readStr = make([]byte, 1024), make([]byte, 1024)
func main() {
var (
host = "localhost"
port = "7077"
remote = host + ":" + port
reader = bufio.NewReader(os.Stdin)
)
con, err := net.Dial("tcp", remote)
defer con.Close()
if err != nil {
fmt.Println("Server not found.")
os.Exit(-1)
}
in, err := con.Write([]byte("slaver"))
if err != nil {
fmt.Printf("Error when send to server: %d\n", in)
os.Exit(0)
}
fmt.Println("Now begin to talk!")
read(con)
for {
writeStr, _, _ = reader.ReadLine()
if string(writeStr) == "quit" {
fmt.Println("Communication terminated.")
os.Exit(1)
}
in, err := con.Write([]byte(writeStr))
if err != nil {
fmt.Printf("Error when send to server: %d\n", in)
os.Exit(0)
}
}
}
func read(conn net.Conn) {
for {
length, err := conn.Read(readStr)
if err != nil {
fmt.Printf("Error when read from server. Error:%s\n", err)
os.Exit(0)
}
fmt.Println(string(readStr[:length]))
}
}