-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
43 lines (37 loc) · 869 Bytes
/
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
package main
import (
"bufio"
"fmt"
"net"
"os"
"strings"
)
var (
server = "127.0.0.1:5000"
)
func main() {
//open connection
conn, err := net.Dial("tcp", server)
if err != nil {
fmt.Println("Error dialing", err.Error())
return
}
inputReader := bufio.NewReader(os.Stdin)
fmt.Println("First, what's your name?")
clientName, _ := inputReader.ReadString('\n')
fmt.Printf("clientName is %s", clientName)
trimmedClient := strings.Trim(clientName, "\r\n") //windows, if linux use "\n" instead
for {
fmt.Println("what to send to the server? Type q to quit")
input, _ := inputReader.ReadString('\n')
trimmedInput := strings.Trim(input, "\r\n")
if trimmedInput == "q" {
return
}
_, err := conn.Write([]byte(trimmedClient + " says:" + trimmedInput + "\n"))
if err != nil {
fmt.Println("Error writing", err.Error())
return
}
}
}