-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
43 lines (38 loc) · 799 Bytes
/
main.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 (
"encoding/json"
"fmt"
"learn_GeeRPC/codec"
"log"
"net"
"time"
)
func startServer(addr chan string) {
l, err := net.Listen("tcp", ": 0")
if err != nil {
log.Fatalf("network:", err)
}
log.Println("start rpc server on", l.Addr())
addr <- l.Addr().String()
Accept(l)
}
func main() {
addr := make(chan string)
go startServer(addr)
conn, _ := net.Dial("tcp", <-addr)
defer conn.Close()
time.Sleep(time.Second)
_ = json.NewEncoder(conn).Encode(DefaultOption)
cc := codec.NewGobCodec(conn)
for i := 0; i < 5; i++ {
h := &codec.Header{
ServiceMethod: "Foo.Sum",
Seq: uint64(i),
}
_ = cc.Write(h, fmt.Sprintf("geerpc req %d", h.Seq))
_ = cc.ReadHeader(h)
var reply string
_ = cc.ReadBody(&reply)
log.Println("reply:", reply)
}
}