Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@KumarL KumarL commented Dec 17, 2014

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

@kratorado

This comment has been minimized.

Copy link

@kratorado kratorado commented Feb 15, 2016

Very helpful, thanx

@danielrangelmoreira

This comment has been minimized.

Copy link

@danielrangelmoreira danielrangelmoreira commented Feb 17, 2017

Thanks

@alexniver

This comment has been minimized.

Copy link

@alexniver alexniver commented Jun 29, 2017

thanks alot

@xgz123

This comment has been minimized.

Copy link

@xgz123 xgz123 commented May 30, 2018

very helpful

@wcc19940308

This comment has been minimized.

Copy link

@wcc19940308 wcc19940308 commented May 19, 2020

thx

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