好的,我有一個 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 回答
- 0 關注
- 156 瀏覽
添加回答
舉報
0/150
提交
取消