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 touchsocket udp组播
  • ¥20 MAC怎么安装Silverlight 插件?以及安装了怎么启用
  • ¥15 VS2012中查询语句无法填入解析,数值传不进去
  • ¥15 gis系统开发出现命名空间“ESRI.ArcGIS”中不存在类型或命名空间名“Analyst3D”报错
  • ¥15 怎么让ai定时给我发信息 c#或者python
  • ¥15 scrapy的Error
  • ¥15 RBF-VSG姚凤军论文复现问题
  • ¥30 开发一个APP商城在制作tabbar的时候运行不了代码没有检查出错误,但是显示不出tabbar,以下为运行结果,如何解决?
  • ¥15 多网卡服务器中winform如何绑定指定网卡
  • ¥15 关于#python#pandas#的问题,想要实现:多个TXT导入Excel,进行分列,不同txt之间都从第一行开始,请各位专家解答!