-
-
Save xgz123/47285d413194ee64bd828f594f45f670 to your computer and use it in GitHub Desktop.
rpc server example in go
This file contains hidden or 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 hidden or 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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment