//server.go
package main
import (
"errors"
"fmt"
"net"
"net/rpc"
"net/rpc/jsonrpc"
"os"
)
type Args struct {
A, B int
}
func checkError(err error) {
if err != nil {
fmt.Fprint(os.Stderr, "Usage: %s", err.Error())
os.Exit(1)
}
}
type Quotient struct {
Quo, Rem int
}
type Arith int
func (t *Arith) Muliply(args *Args, reply *int) error {
*reply = args.A * args.B
return nil
}
func (t *Arith) Divide(args *Args, quo *Quotient) error {
if args.B == 0 {
return errors.New("divide by zero")
}
quo.Quo = args.A * args.B
quo.Rem = args.A / args.B
return nil
}
func main() {
arith := new(Arith)
rpc.Register(arith)
tcpAddr, err := net.ResolveTCPAddr("tcp", ":1234")
checkError(err)
Listener, err := net.ListenTCP("tcp", tcpAddr)
checkError(err)
for {
conn, err := Listener.Accept()
if err != nil {
fmt.Fprint(os.Stderr, "accept err: %s", err.Error())
continue
}
jsonrpc.ServeConn(conn)
}
}
//client.go
package main
import (
"fmt"
"net/rpc/jsonrpc"
"os"
)
type Args struct {
A, B int
}
type quo struct {
Quo, Rem int
}
func main() {
service := "127.0.0.1:1234"
client, err := jsonrpc.Dial("tcp", service)
if err != nil {
fmt.Println("dial error:", err)
os.Exit(1)
}
args := Args{1, 2}
var reply int
err = client.Call("Arith.Muliply", args, &reply)
if err != nil {
fmt.Println("Arith.Muliply call error:", err)
os.Exit(1)
}
fmt.Println("the arith.mutiply is :", reply)
var quto quo
err = client.Call("Arith.Divide", args, &quto)
if err != nil {
fmt.Println("Arith.Divide call error:", err)
os.Exit(1)
}
fmt.Println("the arith.devide is :", quto.Quo, quto.Rem)
}