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
")
current_time := time.Now()
h:= sha1.New()
var barray []byte
copy(barray, request)
hash_rq := h.Sum(barray)
*reply = request + "
" + current_time.Format(time.ANSIC) + "
" + string(hash_rq) + "
"
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:
- I run server.go in a terminal
- I run client.go in another terminal, after a second or so "wooh server is ok" is printed
- I type something and hit Enter on the client's side
- either nothing happens, or "rpc: client protocol error: unexpected EOF" is printed on the client's side
- if nothing happened, terminating the server (ie hitting Control-C) makes the client print the error above
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?