Skip to content

Instantly share code, notes, and snippets.

@csabahenk
Created January 17, 2011 02:04
Show Gist options
  • Save csabahenk/782387 to your computer and use it in GitHub Desktop.
Save csabahenk/782387 to your computer and use it in GitHub Desktop.
the buggy example of http://golang.org/pkg/rpc/ with two fixes
package main
import (
"os"
"rpc"
"log"
"strconv"
"fmt"
"./arith"
)
func main() {
client, err := rpc.DialHTTP("tcp", os.Args[1])
if err != nil {
log.Exit("dialing:", err)
}
// Synchronous call
n1,_ := strconv.Atoi(os.Args[3])
n2,_ := strconv.Atoi(os.Args[4])
args := &arith.Args{n1, n2}
// XXX works only with Multiply
var reply int
err = client.Call("Arith." + os.Args[2], args, &reply)
if err != nil {
log.Exit("arith error:", err)
}
fmt.Printf("Arith: %d*%d=%d\n", args.A, args.B, *reply)
}
package arith
import "os"
type Args struct {
A, B int
}
type Quotient struct {
Quo, Rem int
}
type Arith int
func (t *Arith) Multiply(args *Args, reply *int) os.Error {
*reply = args.A * args.B
return nil
}
func (t *Arith) Divide(args *Args, quo *Quotient) os.Error {
if args.B == 0 {
return os.ErrorString("divide by zero")
}
quo.Quo = args.A / args.B
quo.Rem = args.A % args.B
return nil
}
package main
import (
"rpc"
"net"
"log"
"http"
"os"
"./arith"
)
func main() {
arith := new(arith.Arith)
rpc.Register(arith)
rpc.HandleHTTP()
l, e := net.Listen("tcp", ":" + os.Args[1])
if e != nil {
log.Exit("listen error:", e)
}
http.Serve(l, nil)
}
--- a/aclient.go
+++ b/aclient.go
@@ -27,5 +27,5 @@ func main() {
if err != nil {
log.Exit("arith error:", err)
}
- fmt.Printf("Arith: %d*%d=%d\n", args.A, args.B, *reply)
+ fmt.Printf("Arith: %d*%d=%d\n", args.A, args.B, reply)
}
--- a/aclient.go
+++ b/aclient.go
@@ -22,7 +22,7 @@ func main() {
n2,_ := strconv.Atoi(os.Args[4])
args := &arith.Args{n1, n2}
// XXX works only with Multiply
- var reply int
+ var reply *int
err = client.Call("Arith." + os.Args[2], args, &reply)
if err != nil {
log.Exit("arith error:", err)
include $(GOROOT)/src/Make.inc
TARG=aserver\
aclient
SHARED=arith
all: $(SHARED:%=%.$O) $(TARG)
$(TARG): %: %.$O
$(LD) -o $@ $<
%.$O: %.go Makefile
$(GC) -o $@ $<
clean:
rm -f *.[$(OS)] $(TARG) $(CLEANFILES)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment