Created
May 7, 2012 17:16
-
-
Save jordanorelli/2629049 to your computer and use it in GitHub Desktop.
rpc server example in go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Very helpful, thanx
Thanks
thanks alot
very helpful
thx
Very helpful, thanx
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for a simple, yet effective, example of rpc!