mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 04:48:29 +08:00
add chapter 15.9 (#686)
This commit is contained in:
39
eBook/examples/chapter_15/rpc/rpc_client.go
Normal file
39
eBook/examples/chapter_15/rpc/rpc_client.go
Normal file
@@ -0,0 +1,39 @@
|
||||
// rpc_client.go
|
||||
// if the server is not started:
|
||||
// can't get the server to start, so client stops immediately with error:
|
||||
// 2011/08/01 16:08:05 Error dialing:dial tcp :1234:
|
||||
// The requested address is not valid in its context.
|
||||
// with serverAddress = localhost:
|
||||
// 2011/08/01 16:09:23 Error dialing:dial tcp 127.0.0.1:1234:
|
||||
// No connection could be made because the target machine actively refused it.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/rpc"
|
||||
"./rpc_objects"
|
||||
)
|
||||
|
||||
const serverAddress = "localhost"
|
||||
|
||||
func main() {
|
||||
client, err := rpc.DialHTTP("tcp", serverAddress + ":1234")
|
||||
if err != nil {
|
||||
log.Fatal("Error dialing:", err)
|
||||
}
|
||||
// Synchronous call
|
||||
args := &rpc_objects.Args{7, 8}
|
||||
var reply int
|
||||
err = client.Call("Args.Multiply", args, &reply)
|
||||
if err != nil {
|
||||
log.Fatal("Args error:", err)
|
||||
}
|
||||
fmt.Printf("Args: %d * %d = %d", args.N, args.M, reply)
|
||||
}
|
||||
/* Output:
|
||||
Starting Process E:/Go/GoBoek/code_examples/chapter_14/rpc_client.exe ...
|
||||
|
||||
Args: 7 * 8 = 56
|
||||
End Process exit status 0
|
||||
*/
|
13
eBook/examples/chapter_15/rpc/rpc_objects.go
Normal file
13
eBook/examples/chapter_15/rpc/rpc_objects.go
Normal file
@@ -0,0 +1,13 @@
|
||||
// rpc_objects.go
|
||||
package rpc_objects
|
||||
|
||||
import "net"
|
||||
|
||||
type Args struct {
|
||||
N, M int
|
||||
}
|
||||
|
||||
func (t *Args) Multiply(args *Args, reply *int) net.Error {
|
||||
*reply = args.N * args.M
|
||||
return nil
|
||||
}
|
32
eBook/examples/chapter_15/rpc/rpc_server.go
Normal file
32
eBook/examples/chapter_15/rpc/rpc_server.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// rpc_server.go
|
||||
// after client-exits the server shows the message:
|
||||
// 1:1234: The specified network name is no longer available.
|
||||
// 2011/08/01 16:19:04 rpc: rpc: server cannot decode request: WSARecv tcp 127.0.0.
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"log"
|
||||
"net"
|
||||
"net/rpc"
|
||||
"time"
|
||||
"./rpc_objects"
|
||||
)
|
||||
|
||||
func main() {
|
||||
calc := new(rpc_objects.Args)
|
||||
rpc.Register(calc)
|
||||
rpc.HandleHTTP()
|
||||
listener, e := net.Listen("tcp", "localhost:1234")
|
||||
if e != nil {
|
||||
log.Fatal("Starting RPC-server -listen error:", e)
|
||||
}
|
||||
go http.Serve(listener, nil)
|
||||
time.Sleep(1000e9)
|
||||
}
|
||||
/* Output:
|
||||
Starting Process E:/Go/GoBoek/code_examples/chapter_14/rpc_server.exe ...
|
||||
|
||||
** after 5 s: **
|
||||
End Process exit status 0
|
||||
*/
|
39
eBook/examples/chapter_15/rpc_updated/rpc_client.go
Normal file
39
eBook/examples/chapter_15/rpc_updated/rpc_client.go
Normal file
@@ -0,0 +1,39 @@
|
||||
// rpc_client.go
|
||||
// if the server is not started:
|
||||
// can't get the server to start, so client stops immediately with error:
|
||||
// 2011/08/01 16:08:05 Error dialing:dial tcp :1234:
|
||||
// The requested address is not valid in its context.
|
||||
// with serverAddress = localhost:
|
||||
// 2011/08/01 16:09:23 Error dialing:dial tcp 127.0.0.1:1234:
|
||||
// No connection could be made because the target machine actively refused it.
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/rpc"
|
||||
"./rpc_objects"
|
||||
)
|
||||
|
||||
const serverAddress = "localhost"
|
||||
|
||||
func main() {
|
||||
client, err := rpc.DialHTTP("tcp", serverAddress + ":1234")
|
||||
if err != nil {
|
||||
log.Fatal("Error dialing:", err)
|
||||
}
|
||||
// Synchronous call
|
||||
args := &rpc_objects.Args{7, 8}
|
||||
var reply int
|
||||
err = client.Call("Args.Multiply", args, &reply)
|
||||
if err != nil {
|
||||
log.Fatal("Args error:", err)
|
||||
}
|
||||
fmt.Printf("Args: %d * %d = %d", args.N, args.M, reply)
|
||||
}
|
||||
/* Output:
|
||||
Starting Process E:/Go/GoBoek/code_examples/chapter_14/rpc_client.exe ...
|
||||
|
||||
Args: 7 * 8 = 56
|
||||
End Process exit status 0
|
||||
*/
|
@@ -0,0 +1,11 @@
|
||||
// rpc_objects.go
|
||||
package rpc_objects
|
||||
|
||||
type Args struct {
|
||||
N, M int
|
||||
}
|
||||
|
||||
func (t *Args) Multiply(args *Args, reply *int) error {
|
||||
*reply = args.N * args.M
|
||||
return nil
|
||||
}
|
32
eBook/examples/chapter_15/rpc_updated/rpc_server.go
Normal file
32
eBook/examples/chapter_15/rpc_updated/rpc_server.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// rpc_server.go
|
||||
// after client-exits the server shows the message:
|
||||
// 1:1234: The specified network name is no longer available.
|
||||
// 2011/08/01 16:19:04 rpc: rpc: server cannot decode request: WSARecv tcp 127.0.0.
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"log"
|
||||
"net"
|
||||
"net/rpc"
|
||||
"time"
|
||||
"./rpc_objects"
|
||||
)
|
||||
|
||||
func main() {
|
||||
calc := new(rpc_objects.Args)
|
||||
rpc.Register(calc)
|
||||
rpc.HandleHTTP()
|
||||
listener, e := net.Listen("tcp", "localhost:1234")
|
||||
if e != nil {
|
||||
log.Fatal("Starting RPC-server -listen error:", e)
|
||||
}
|
||||
go http.Serve(listener, nil)
|
||||
time.Sleep(1000e9)
|
||||
}
|
||||
/* Output:
|
||||
Starting Process E:/Go/GoBoek/code_examples/chapter_14/rpc_server.exe ...
|
||||
|
||||
** after 5 s: **
|
||||
End Process exit status 0
|
||||
*/
|
Reference in New Issue
Block a user