title: Simple RPC Call in Go
body: <p>I am trying to get a minimal application working using RPC calls in Go. I am heavily borrowing from the <a href="http://golang.org/pkg/net/rpc/#pkg-overview" rel="nofollow">online example</a>, as you can see from my code:</p>
  
  <p>server.go:</p>
  
  <pre class="lang-go prettyprint-override"><code>package main
  
  import (
      [...]
  )
  
  type InfoDumper int
  
  func (s *InfoDumper) Dump(request string, reply *string) error {
  
      fmt.Println("Woooh imma deliverin stuff\n")
  
      current_time := time.Now()
  
      h:= sha1.New()
      var barray []byte
      copy(barray, request)
      hash_rq := h.Sum(barray)
  
      *reply = request + "\n" + current_time.Format(time.ANSIC) + "\n"  + string(hash_rq) + "\n"
      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)
  
  }
  </code></pre>
  
  <p>client.go:</p>
  
  <pre class="lang-go prettyprint-override"><code>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, &amp;reply)
  
          if (e!=nil) {
              fmt.Println("omg error!", e)
          }
  
          fmt.Println(reply)
      }
  
  }
  </code></pre>
  
  <p>The only difference I can see is that I wrote <code>http.Serve(l, nil)</code> instead of <code>go http.Serve(l, nil)</code> ; this is because writing with <code>go</code> 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.</p>
  
  <p>This is what's happening right now:</p>
  
  <ul>
  <li>I run server.go in a terminal</li>
  <li>I run client.go in another terminal, after a second or so "wooh server is ok" is printed</li>
  <li>I type something and hit Enter on the client's side</li>
  <li>either nothing happens, or "rpc: client protocol error: unexpected EOF" is printed on the client's side</li>
  <li>if nothing happened, terminating the server (ie hitting Control-C) makes the client print the error above</li>
  </ul>
  
  <p>In either case, "Woooh imma deliverin stuff" is never displayed on the server's side...</p>
  
  <p>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.</p>
  
  <p>Does anyone see anything wrong with this code?</p>