好的,我有一个 rpc 服务器包和一个 rpc 客户端包。我想要求服务器返回所有域。在 server.go 中,我导入 server.DomainServer 并通过 http 提供服务。在 client.go 中,我导入 client.DomainClient 并向侦听服务器发送 rpc 调用。响应为空[]。预期的响应是 ["dom1.de","dom2.de"]为什么响应为空?我该如何调试?我是否必须共享 GetAllDomainsRequest 和 GetAllDomainsRequest 结构,以便它们具有相同的类型,例如在名为 common 的包中?更新:运行wireshark来捕获响应,响应是,除其他外,[]stringserver.gopackage mainimport ( "log" "net/http" "net/rpc" "bitbucket.org/idalu/hostd/server")func main() { var authkey = "123" ds := &server.DomainServer{ Authkey: authkey, } rpc.Register(ds) rpc.HandleHTTP() log.Fatalln(http.ListenAndServe(":1312", nil))}服务器/域.gopackage serverimport ( "errors" "fmt")type DomainServer struct { Authkey string}type GetAllDomainsRequest struct { Authkey string}type GetAllDomainsResponse struct { Domains []string}func (s *DomainServer) GetAllDomains(req GetAllDomainsRequest, rsp *GetAllDomainsResponse) error { if req.Authkey != s.Authkey { return errors.New("forbidden") } rsp = &GetAllDomainsResponse{ Domains: []string{"dom1.de", "dom2.de"}, } fmt.Println(rsp) return nil}客户端package mainimport ( "log" "bitbucket.org/idalu/hostd/client")func main() { dc := &client.DomainClient{ Authkey: "123", ServerAddr: "127.0.0.1:1312", } if e := dc.GetAllDomains(); e != nil { log.Fatalln(e.Error()) }}客户端/域.gopackage clientimport ( "fmt" "net/rpc")type DomainClient struct { ServerAddr string Authkey string}type GetAllDomainsRequest struct { Authkey string}type GetAllDomainsResponse struct { Domains []string}func (c *DomainClient) GetAllDomains() error { client, e := rpc.DialHTTP("tcp", c.ServerAddr) if e != nil { return e } req := GetAllDomainsRequest{ Authkey: c.Authkey, } rsp := GetAllDomainsResponse{} if e := client.Call("DomainServer.GetAllDomains", req, &rsp); e != nil { return e } fmt.Println(rsp.Domains) return nil}
1 回答
- 1 回答
- 0 关注
- 135 浏览
添加回答
举报
0/150
提交
取消