duangong0690 2015-08-26 13:35
浏览 103
已采纳

执行:rpc请求返回空

Ok I have a rpc server package and a rpc client package. I'd like to ask the server to return all domains.

In server.go I import server.DomainServer and serve it over http.

In client.go I import client.DomainClient and send a rpc call to the listening server.

The response is empty [].

The expected response is ["dom1.de","dom2.de"]

Why is the response empty? How can I debug this?

Do I have to share the GetAllDomainsRequest and GetAllDomainsRequest struct so they have the same type, e.g. in a package called common?

Update: Ran wireshark to capture the response, and the response was, amongst other stuff, []string

server.go

package main

import (
    "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))
}

server/domain.go

package server

import (
    "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
}

client.go

package main

import (
    "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())
    }
}

client/domain.go

package client

import (
    "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条回答 默认 最新

  • duanchi3109 2015-08-26 13:58
    关注

    The bug is in server/domain.go

    rsp = &GetAllDomainsResponse{
        Domains: []string{"dom1.de", "dom2.de"},
    }
    

    where you are not updating the response value provided during the call but assign a new one to your local variable. It should be something like

    rsp.Domains = []string{"dom1.de", "dom2.de"}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗