-
Notifications
You must be signed in to change notification settings - Fork 400
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from themoonbear/13.7.2
13.7.2
- Loading branch information
Showing
3 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
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
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
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 | ||
``` |
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
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) | ||
} | ||
} |