Skip to content

Commit

Permalink
Merge pull request #37 from themoonbear/13.7.2
Browse files Browse the repository at this point in the history
13.7.2
  • Loading branch information
hantmac committed Jan 31, 2019
2 parents c3b8560 + c5a5692 commit 1bc4aed
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
- [13.6.1 简洁的并发TCP服务器](https://github.com/hantmac/Mastering_Go_ZH_CN/tree/master/eBook/chapter13/13.6.1.md)
- [13.7 远程调用(RPC)](https://github.com/hantmac/Mastering_Go_ZH_CN/tree/master/eBook/chapter13/13.7.md)
- [13.7.1 RPC 客户端](https://github.com/hantmac/Mastering_Go_ZH_CN/tree/master/eBook/chapter13/13.7.1.md)
- [13.7.2 RPC 服务器](https://github.com/hantmac/Mastering_Go_ZH_CN/tree/master/eBook/chapter13/13.7.2.md)


-------
Expand Down
101 changes: 101 additions & 0 deletions eBook/chapter13/13.7.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# ** RPC 服务器**

RPC 服务器被保存为 `RPCserver.go`, 并分为五个部分来介绍。

`RPCserver.go`的第一部分如下:

```go
package main

import(
"fmt"
"math"
"net"
"net/rpc"
"os"
"sharedRPC"
)
```

`RPCserver.go` 的第二部分代码如下:

```go
type MyInterface struct {}

func Power(x, y float64) float64 {
return math.Pow(x, y)
}

func (t *MyInterface) Multiply(arguments *sharedRPC.MyFloats, reply * float64) error {
*reply = arguments.A1 * argumentsA2
return nil
}

func (t *MyInterface) Power(arguments *sharedRPC.MyFloats, reply *float64) error {
*reply = Power(arguments.A1, arguments.A2)
return nil
}
```

上面这段代码,RPC 服务器实现了所需接口以及名为 `Power()` 的辅助函数。

`RPCserver.go` 的第三段如下:

```go
func main() {
PORT := ":1234"
arguments := os.Args
if len(arguments) != 1 {
PORT = ":" + arguments[1]
}
```
`RPCserver.go` 的第四部分代码如下:
```go
myInterface := new(MyInterface)
rpc.Register(myInterface)
t, err := net.ResolveTCPAddr("tcp4", PORT)
if err != nil {
fmt.Println(err)
return
}
l, err := net.ListenTCP("tcp4", t)
if err != nil {
fmt.Println(err)
return
}
```
`rpc.Register()` 函数的调用使这个程序成为 RPC 服务器。但是,由于 RPC 服务器使用 TCP 协议,它仍需要调用 `net.ResolveTCPAddr()``net.ListenTCP()`
`RPCserver.go`的其余代码如下:
```go
for {
c, err := l.Accept()
if err != nil {
continue
}
fmt.Printf("%s\n", c.RemoteAddr())
rpc.ServerConn(c)
}
}
```

`RemoteAddr()` 函数返回接入的 RPC 客户端 IP 地址和端口。`rpc.ServerConn()` 函数为 RPC 客户端提供服务。

执行 `RPCserver.go` 并等待 `RPCclient.go` 连接将产生如下输出:

```shell
$ go run RPCserver.go
127.0.0.1:52289
```

执行 `RPCclient.go` 将产生如下输出:

```shell
$ go run RPCclient.go localhost:1234
Reply (Multiply): -8.000000
Reply (Power): 0.250000
```
55 changes: 55 additions & 0 deletions eBook/examples/chapter13/RPCserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"fmt"
"math"
"net"
"net/rpc"
"os"
"sharedRPC"
)

type MyInterface struct{}

func Power(x, y float64) float64 {
return math.Pow(x, y)
}

func (t *MyInterface) Multiply(arguments *sharedRPC.MyFloats, reply *float64) error {
*reply = arguments.A1 * argumentsA2
return nil
}

func (t *MyInterface) Power(arguments *sharedRPC.MyFloats, reply *float64) error {
*reply = Power(arguments.A1, arguments.A2)
return nil
}

func main() {
PORT := ":1234"
arguments := os.Args
if len(arguments) != 1 {
PORT = ":" + arguments[1]
}

myInterface := new(MyInterface)
rpc.Register(myInterface)
t, err := net.ResolveTCPAddr("tcp4", PORT)
if err != nil {
fmt.Println(err)
return
}
l, err := net.ListenTCP("tcp4", t)
if err != nil {
fmt.Println(err)
return
}
for {
c, err := l.Accept()
if err != nil {
continue
}
fmt.Printf("%s\n", c.RemoteAddr())
rpc.ServerConn(c)
}
}

0 comments on commit 1bc4aed

Please sign in to comment.