-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcp_client.go
More file actions
50 lines (41 loc) · 832 Bytes
/
tcp_client.go
File metadata and controls
50 lines (41 loc) · 832 Bytes
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
package main
import (
"encoding/json"
"fmt"
"net"
"os"
"time"
)
type state struct {
MouseX float64 `json:"mouseX"`
MouseY float64 `json:"mouseY"`
}
func (s state) String() string {
return fmt.Sprintf("%d, %d", s.MouseX, s.MouseY)
}
var clientState state = state{MouseX: 17, MouseY: 17}
func main() {
if len(os.Args) == 1 {
panic("host:port not provided")
}
CONNECT := os.Args[1]
conn, err := net.Dial("tcp4", CONNECT)
if err != nil {
panic(err)
}
defer conn.Close()
endcoder, decoder := json.NewEncoder(conn), json.NewDecoder(conn)
for {
fmt.Println("Client >> ", clientState)
err = endcoder.Encode(&clientState)
if err != nil {
panic(err)
}
err = decoder.Decode(&clientState)
if err != nil {
panic(err)
}
fmt.Println("Server -> ", clientState)
time.Sleep(time.Second * 3)
}
}