Created
September 3, 2014 06:56
-
-
Save mogeta/5fc697663cfeba962778 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"errors" | |
"fmt" | |
"log" | |
"net" | |
"net/http" | |
"net/rpc" | |
"time" | |
) | |
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 startServer() { | |
arith := new(Arith) | |
rpc.Register(arith) | |
rpc.HandleHTTP() | |
l, e := net.Listen("tcp", ":1234") | |
if e != nil { | |
log.Fatal("listen error:", e) | |
} | |
go http.Serve(l, nil) | |
} | |
func main() { | |
startServer() | |
client, err := rpc.DialHTTP("tcp", "localhost"+":1234") | |
if err != nil { | |
log.Fatal("dialing:", err) | |
} | |
// Synchronous call | |
args := &Args{7, 8} | |
var reply int | |
err = client.Call("Arith.Multiply", args, &reply) | |
if err != nil { | |
log.Fatal("arith error:", err) | |
} | |
fmt.Println("Arith: %d*%d=%d", args.A, args.B, reply) | |
for { | |
time.Sleep(1 * time.Second) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment