douya5331
2013-07-17 18:23
浏览 130

JSONRPC服务器返回空结果

I've coded a simple JSONRPC server to test Go's capabilities, but I'm stuck, always getting an empty result, without error and correct id. I've got the following Go code: package main

import (
    "log"
    "net"
    "net/rpc"
    "net/rpc/jsonrpc"
)

type Experiment int

func (e *Experiment) Test(i *string, reply *string) error {
    s := "Hello, " + *i
    reply = &s
    log.Println(s, reply)
    return nil
}

func main() {
    exp := new(Experiment)
    server := rpc.NewServer()
    server.Register(exp)
    l, err := net.Listen("tcp", ":1234")
    if err != nil {
        log.Fatal("listen error:", err)
    }
    for {
        conn, err := l.Accept()
        if err != nil {
            log.Fatal(err)
        }
        server.ServeCodec(jsonrpc.NewServerCodec(conn))
    }
}

No matter what I tried, I've always got the following response:

{"id":1,"result":"","error":null}

Logging shows everything is working as it should in the server.

2013/07/17 15:17:13 Hello, Paulo 0xc200090ac0

Any ideas on what's going on? I'm using the latest stable version of Go, v1.1.1

Thanks

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongshi2141 2013-07-17 18:47
    已采纳

    Your reply is of type pointer to string. That is okay and even required as the second argument is used to return the answer. But then you do:

    s := "Hello, " + *i
    reply = &s
    

    Which translates to:

    • Construct a new string with a new value.
    • Let reply point to this new string

    This lets the string which is returned completely unaffected. Try

    *reply = s
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题