-
Notifications
You must be signed in to change notification settings - Fork 2
/
go_client.go
72 lines (59 loc) · 1.12 KB
/
go_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
65
66
67
68
69
70
71
72
package main
import (
"fmt"
"flag"
"net"
)
type MsgHeader struct {
msgtype byte
from, to int32
}
type MsgRequest struct {
header MsgHeader
ctype byte
from int
value int
}
var (
cmd = flag.String("c", "d", "command")
port = flag.String("p", "11110", "port number connect to")
value = flag.Int("v", 999, "request value")
)
func main() {
flag.Parse()
var msg MsgRequest
buf := make([]byte, 24)
var i int = 0
/* header */
msg.header.msgtype = 'r'
msg.header.from = -1
msg.header.to = -1
/* body */
msg.ctype = ([]byte(*cmd))[0]
msg.from = -1
msg.value = *value
fmt.Printf("port %s\n", *port)
fmt.Printf("cmd %s\n", *cmd)
fmt.Printf("value %d\n", *value)
fmt.Println(msg)
i = 0
/* header is 12 byte */
buf[i] = 'R' /* type */
i += 4
buf[i] = byte(100) /* from */
i += 4
buf[i] = byte(100) /* to */
i += 4 /* add padding */
buf[i] = ([]byte(*cmd))[0] /* command */
i += 4
buf[i] = 0 /* to */
i += 4
buf[i] = byte(*value) /* value */
conn, err := net.Dial("tcp", "127.0.0.1" + ":" + *port)
if err != nil {
fmt.Printf("Dial error : %s\n", err)
return
}
defer conn.Close()
conn.Write(buf)
}