user2592637
user2592637

Reputation:

JSONRPC server returns empty result

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

Upvotes: 2

Views: 1000

Answers (1)

Volker
Volker

Reputation: 42431

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

Upvotes: 4

Related Questions