doutouman6245 2013-12-04 17:12
浏览 82
已采纳

Go中的简单RPC调用

I am trying to get a minimal application working using RPC calls in Go. I am heavily borrowing from the online example, as you can see from my code:

server.go:

package main

import (
    [...]
)

type InfoDumper int

func (s *InfoDumper) Dump(request string, reply *string) error {

    fmt.Println("Woooh imma deliverin stuff
")

    current_time := time.Now()

    h:= sha1.New()
    var barray []byte
    copy(barray, request)
    hash_rq := h.Sum(barray)

    *reply = request + "
" + current_time.Format(time.ANSIC) + "
"  + string(hash_rq) + "
"
    return nil
}


func main() {

    server := new(InfoDumper)

    rpc.Register(server)
    rpc.HandleHTTP()

    l, e := net.Listen("tcp", "127.0.0.1:40000")
    if e != nil {
        fmt.Println(e)
    }

    http.Serve(l, nil)

}

client.go:

package main

import (
    [...]
)

func main() {

    client, e := rpc.Dial("tcp", "127.0.0.1:40000")

    if e!=nil {
        fmt.Println(e)
    }   else {
        fmt.Println("wooh server is ok")
    }

    in:= bufio.NewReader(os.Stdin)

    for {

        line, _, _ := in.ReadLine()
        request := string(line)
        var reply string

        e = client.Call("InfoDumper.Dump", request, &reply)

        if (e!=nil) {
            fmt.Println("omg error!", e)
        }

        fmt.Println(reply)
    }

}

The only difference I can see is that I wrote http.Serve(l, nil) instead of go http.Serve(l, nil) ; this is because writing with go makes my server terminate right away. InfoDump is supposed to reply with a copy of whatever was sent, the time and hash of the request.

This is what's happening right now:

  • I run server.go in a terminal
  • I run client.go in another terminal, after a second or so "wooh server is ok" is printed
  • I type something and hit Enter on the client's side
  • either nothing happens, or "rpc: client protocol error: unexpected EOF" is printed on the client's side
  • if nothing happened, terminating the server (ie hitting Control-C) makes the client print the error above

In either case, "Woooh imma deliverin stuff" is never displayed on the server's side...

This was done during class as a preliminary step to get acquainted with RPC in Go before going on to more serious exercises ; all the other students managed to get this step working, looked at this code and couldn't see the difference with theirs.

Does anyone see anything wrong with this code?

  • 写回答

1条回答 默认 最新

  • dongyigua4468 2013-12-04 22:45
    关注

    As I noted in my mailing list response, you will need to use DialHTTP if you want to connect to an RPC server that you have served using HandleHTTP.

    I've made some other notes about your code (including styling: use gofmt and MixedCaps, per Effective Go, and be sure to bail out on errors) on the mailing list.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值