亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Go:rpc 請求返回空

Go:rpc 請求返回空

Go
精慕HU 2021-11-01 16:47:00
好的,我有一個 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 關注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號