1
0
mirror of https://github.com/chai2010/advanced-go-programming-book.git synced 2025-05-24 04:22:22 +00:00
2018-12-16 12:40:54 +08:00

37 lines
815 B
Go

package main
import (
"flag"
"fmt"
"log"
"net"
"net/rpc"
"net/rpc/jsonrpc"
)
var flagAddr = flag.String("addr", "localhost:1234", "server address")
func main() {
flag.Parse()
// nc localhost 1234
// {"method":"HelloService.Hello","params":["hello"],"id":0}
// echo -e '{"method":"HelloService.Hello","params":["hello2222"],"id":3}' | nc localhost 1234
// echo -e '{"method":"HelloService.Hello","params":["hello2222"],"id":3}{"method":"HelloService.Hello","params":["hello33"],"id":4}' | nc localhost 1234
conn, err := net.Dial("tcp", *flagAddr)
if err != nil {
log.Fatal("net.Dial:", err)
}
client := rpc.NewClientWithCodec(jsonrpc.NewClientCodec(conn))
var reply string
err = client.Call("HelloService.Hello", "hello", &reply)
if err != nil {
log.Fatal(err)
}
fmt.Println(reply)
}