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 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R