Created
July 24, 2016 10:03
-
-
Save alexniver/813c81d3eaf7449f96af3087e66563ac to your computer and use it in GitHub Desktop.
rpc server example. edit from https://gist.github.com/jordanorelli/2629049
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" | |
"fmt" | |
"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 length int | |
err = client.Call("Listener.GetLine", line, &length) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println("len : ", length) | |
} | |
} |
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, length *int) error { | |
fmt.Println(string(line)) | |
*length = len(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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment