第15章 15.9-go rpc

package rpc_objects

type Args struct {
    M, N int
}

func (this *Args) Muitlply(args *Args, reply *int) error {
    *reply = args.M * args.N
    return nil
}
package main

import (
    "net/http"
    "log"
    "net"
    "net/rpc"
    "time"
    "./rpc_objects"
)

func main() {
    calc := new(rpc_objects.Args)
    rpc.Register(calc)
    rpc.HandleHTTP()
    listener, e := net.Listen("tcp", "localhost:1234")
    if e != nil {
        log.Fatal("Starting RPC-server -listen error:", e)
    }
    go http.Serve(listener, nil)
    time.Sleep(1000e9)
}
package main

import (
    "fmt"
    "log"
    "net/rpc"
    "./rpc_objects"
)

func main() {
    rpcClient, err := rpc.DialHTTP("tcp", "localhost:1234")
    if err != nil {
        log.Fatal("Error dialing:", err)
    }

    args := &rpc_objects.Args{2,3}

    var reply int

    err = rpcClient.Call("Args.Muitlply", args, &reply)

    if err != nil {
        log.Fatal("Args error:", err)
    }

    fmt.Printf("Args: %d * %d = %d\n", args.N, args.M, reply)
}

你可能感兴趣的:(第15章 15.9-go rpc)