This commit is contained in:
2018-04-11 10:46:14 +08:00
parent 33e72857bf
commit 6c5797819e
11 changed files with 499 additions and 0 deletions

BIN
src/rpc/client/client Executable file

Binary file not shown.

48
src/rpc/client/client.go Normal file
View File

@@ -0,0 +1,48 @@
package main
import (
"errors"
"net/rpc"
"log"
"fmt"
)
type Args struct {
A, B int
}
type Quotient struct {
Quo, Rem int
}
type Arith int
func (t *Arith) Multiply(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() {
serverAddress := "127.0.0.1"
client, err := rpc.DialHTTP("udp", serverAddress + ":1234")
if err != nil {
log.Fatal("Fatal error:", err)
}
//args := &server.Args{7, 8}
var args Args = Args{7, 8}
var reply int
err = client.Call("Arith.Multiply", args, &reply)
if err != nil {
log.Fatal("arith error:", err)
}
fmt.Printf("Arith: %d * %d = %d", args.A, args.B, reply)
}

BIN
src/rpc/server/server Executable file

Binary file not shown.

44
src/rpc/server/server.go Normal file
View File

@@ -0,0 +1,44 @@
package main
import (
"errors"
"net/rpc"
"net"
"log"
"net/http"
)
type Args struct {
A, B int
}
type Quotient struct {
Quo, Rem int
}
type Arith int
func (t *Arith) Multiply(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)
rpc.HandleHTTP()
l, e := net.Listen("udp", ":1234")
if e != nil {
log.Fatal("Fatal error:", e)
}
http.Serve(l, nil)
}