Ok, here are all my source files.
I want to get a simple rpc example running, so I mostly copied the
example in the rpc package documentation. When I run the server and
then the client on the same machine, I get the following error on the
terminal running the client:
2009/11/15 14:20:29 rpc: client protocol error: gob: internal error:
field numbers out of bounds
2009/11/15 14:20:29 error: gob: internal error: field numbers out of
bounds
I get the following message on the terminal running the server:
2009/11/15 14:20:29 rpc: EOF
//---------------------------------------------------------------
// commands.go
package commands
import "fmt"
import "os"
type Args struct
{
Cmd, Arg int;
}
type Result struct
{
Out int;
}
type Server int
func (t *Server) Execute(args *Args, res *Result) os.Error
{
fmt.Println("params = ", args.Cmd, " ", args.Arg );
res.Out = 2;
return nil
}
//---------------------------------------------------------------
// client.go
package main
import "log"
import "rpc"
import "fmt"
import "./commands"
func main()
{
client, err := rpc.DialHTTP("tcp", "
127.0.0.1:2000");
if err != nil
{
log.Exit("dialing:", err);
}
// Synchronous call
args := &commands.Args{1,2};
reply := new(commands.Result);
fmt.Println("calling");
err = client.Call("Server.Execute", args, reply);
if err != nil
{
log.Exit("error: ", err);
}
fmt.Println(reply.Out);
}
//---------------------------------------------------------------
// server.go
package main
import "log"
import "http"
import "net"
import "rpc"
import "./commands"
func main()
{
cmdsvr := new(commands.Server);
rpc.Register(cmdsvr);
rpc.HandleHTTP();
l, e := net.Listen("tcp", ":2000");
if e != nil
{
log.Exit("listen error: ", e);
}
http.Serve(l, nil);
}
I build with these commands:
6g commands.go
6g server.go
6l -o server server.6
6g client.go
6l -o client client.6