douya5331 2013-07-17 18:23
浏览 142
已采纳

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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题