Skip to content

Instantly share code, notes, and snippets.

@jordanorelli
Created May 7, 2012 17:16
Show Gist options
  • Select an option

  • Save jordanorelli/2629049 to your computer and use it in GitHub Desktop.

Select an option

Save jordanorelli/2629049 to your computer and use it in GitHub Desktop.
rpc server example in go
package main
import (
"bufio"
"log"
"net/rpc"
"os"
)
func main() {
client, err := rpc.Dial("tcp", "localhost:42586")
if err != nil {
log.Fatal(err)
}
in := bufio.NewReader(os.Stdin)
for {
line, _, err := in.ReadLine()
if err != nil {
log.Fatal(err)
}
var reply bool
err = client.Call("Listener.GetLine", line, &reply)
if err != nil {
log.Fatal(err)
}
}
}
package main
import (
"fmt"
"log"
"net"
"net/rpc"
)
type Listener int
func (l *Listener) GetLine(line []byte, ack *bool) error {
fmt.Println(string(line))
return nil
}
func main() {
addy, err := net.ResolveTCPAddr("tcp", "0.0.0.0:42586")
if err != nil {
log.Fatal(err)
}
inbound, err := net.ListenTCP("tcp", addy)
if err != nil {
log.Fatal(err)
}
listener := new(Listener)
rpc.Register(listener)
rpc.Accept(inbound)
}
@KumarL
Copy link
Copy Markdown

KumarL commented Dec 17, 2014

Thanks for a simple, yet effective, example of rpc!

@kratorado
Copy link
Copy Markdown

Very helpful, thanx

@danielrangelmoreira
Copy link
Copy Markdown

Thanks

@alexniver
Copy link
Copy Markdown

thanks alot

@xgz123
Copy link
Copy Markdown

xgz123 commented May 30, 2018

very helpful

@wcc19940308
Copy link
Copy Markdown

thx

@Helshr
Copy link
Copy Markdown

Helshr commented Aug 4, 2021

Very helpful, thanx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment