Reputation: 457
I am trying to get a minimal application working using RPC calls in Go. I am heavily borrowing from the online example, as you can see from my code:
server.go:
package main
import (
[...]
)
type InfoDumper int
func (s *InfoDumper) Dump(request string, reply *string) error {
fmt.Println("Woooh imma deliverin stuff\n")
current_time := time.Now()
h:= sha1.New()
var barray []byte
copy(barray, request)
hash_rq := h.Sum(barray)
*reply = request + "\n" + current_time.Format(time.ANSIC) + "\n" + string(hash_rq) + "\n"
return nil
}
func main() {
server := new(InfoDumper)
rpc.Register(server)
rpc.HandleHTTP()
l, e := net.Listen("tcp", "127.0.0.1:40000")
if e != nil {
fmt.Println(e)
}
http.Serve(l, nil)
}
client.go:
package main
import (
[...]
)
func main() {
client, e := rpc.Dial("tcp", "127.0.0.1:40000")
if e!=nil {
fmt.Println(e)
} else {
fmt.Println("wooh server is ok")
}
in:= bufio.NewReader(os.Stdin)
for {
line, _, _ := in.ReadLine()
request := string(line)
var reply string
e = client.Call("InfoDumper.Dump", request, &reply)
if (e!=nil) {
fmt.Println("omg error!", e)
}
fmt.Println(reply)
}
}
The only difference I can see is that I wrote http.Serve(l, nil)
instead of go http.Serve(l, nil)
; this is because writing with go
makes my server terminate right away.
InfoDump is supposed to reply with a copy of whatever was sent, the time and hash of the request.
This is what's happening right now:
In either case, "Woooh imma deliverin stuff" is never displayed on the server's side...
This was done during class as a preliminary step to get acquainted with RPC in Go before going on to more serious exercises ; all the other students managed to get this step working, looked at this code and couldn't see the difference with theirs.
Does anyone see anything wrong with this code?
Upvotes: 3
Views: 3177
Reputation: 4756
As I noted in my mailing list response, you will need to use DialHTTP if you want to connect to an RPC server that you have served using HandleHTTP.
I've made some other notes about your code (including styling: use gofmt and MixedCaps, per Effective Go, and be sure to bail out on errors) on the mailing list.
Upvotes: 5