英文:
Go: rpc request returns empty
问题
好的,以下是翻译好的内容:
好的,我有一个rpc服务器包和一个rpc客户端包。
我想要求服务器返回所有的域名。
在server.go中,我导入server.DomainServer并通过http进行服务。
在client.go中,我导入client.DomainClient并向正在监听的服务器发送一个rpc调用。
响应是空的[]
。
期望的响应是["dom1.de","dom2.de"]
。
为什么响应是空的?我该如何调试这个问题?
我是否需要共享GetAllDomainsRequest和GetAllDomainsRequest结构体,使它们具有相同的类型,例如在一个名为common的包中?
更新:运行wireshark来捕获响应,响应是,除其他内容外,[]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
}
英文:
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
得分: 2
错误出现在 server/domain.go 文件中。
rsp = &GetAllDomainsResponse{
Domains: []string{"dom1.de", "dom2.de"},
}
在这里,你没有更新调用时提供的响应值,而是将一个新值分配给了你的局部变量。应该像这样修改:
rsp.Domains = []string{"dom1.de", "dom2.de"}
请注意,这只是一个翻译,我无法提供关于代码的进一步解释或建议。
英文:
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"}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论