-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
64 lines (54 loc) · 1.33 KB
/
client.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
package main
import (
"bufio"
"fmt"
"net"
"os"
"strings"
)
func main() {
fmt.Print("Client for simple remote shell...\n")
readIP := bufio.NewReader(os.Stdin)
fmt.Print("Enter Server IP address: ")
strIPAddress, _ := readIP.ReadString('\n')
strIPAddress = strings.TrimSpace(strIPAddress)
conn, err := net.Dial("tcp", strIPAddress+":8087")
if err != nil {
fmt.Fprintf(os.Stderr, "error dialing tcp: %v\nServer needs to be runing before client.\n", err)
return
}
defer conn.Close()
console := bufio.NewReader(os.Stdin)
connReader := bufio.NewReader(conn)
for {
fmt.Print("For close connection clien use command: client close\n")
fmt.Print("For stop server use command: server close\n")
fmt.Print("Command to send: ")
text, err := console.ReadString('\n')
if err != nil {
fmt.Fprintf(os.Stderr, "error reading string: %v", err)
return
}
text = strings.TrimSpace(text)
fmt.Fprintf(conn, text+"\n")
if text == "client close" {
fmt.Println("Close connection")
return
}
if text == "server close" {
fmt.Println("Close connection")
return
}
for {
message, err := connReader.ReadString('\n')
if err != nil {
fmt.Fprintf(os.Stderr, "error reading from conn: %v", err)
return
}
if strings.Index(message, "<<<endMessage>>") > 0 {
break
}
fmt.Print(message)
}
}
}