首页 技术 正文
技术 2022年11月21日
0 收藏 535 点赞 4,493 浏览 2159 个字

参考:https://studygolang.com/pkgdoc

导入方式:

import "net/rpc/jsonrpc"

jsonrpc包实现了JSON-RPC的ClientCodec和ServerCodec接口,可用于rpc包。

func Dial

func Dial(network, address string) (*rpc.Client, error)

Dial在指定的网络和地址连接一个JSON-RPC服务端

func ServeConn

func ServeConn(conn io.ReadWriteCloser)

ServeConn在单个连接上执行DefaultServer。ServeConn会阻塞,服务该连接直到客户端挂起。调用者一般应另开线程调用本函数:”go serveConn(conn)”。ServeConn在该连接使用JSON编解码格式。

举例:

JSON RPC

服务端:

package mainimport (
"fmt"
"net"
"net/rpc"
"net/rpc/jsonrpc"
"errors"
"os"
)type Args struct{
A, B int
}type Quotient struct{
Quo, Rem int
}type Arith intfunc (t *Arith) Multiply(args *Args, reply *int) error{
*reply = args.A * args.B
return nil
}func (t *Arith) Divide(args *Args, quo *Quotient) error{
if args.B == 0{
return errors.New("divide by zero")
}
quo.Quo = args.A / args.B
quo.Rem = args.A % args.B
return nil
}func main() {
arith := new(Arith)
rpc.Register(arith) tcpAddr, err := net.ResolveTCPAddr("tcp", ":1234")//jsonrpc是基于TCP协议的,现在他还不支持http协议
if err != nil{
fmt.Println(err.Error())
os.Exit(1)
} listener, err := net.ListenTCP("tcp", tcpAddr)
if err != nil{
fmt.Println(err.Error())
os.Exit(1)
}
for{
conn, err := listener.Accept()
if err != nil{
continue
}
jsonrpc.ServeConn(conn)
}
}

客户端:

package mainimport (
"fmt"
"net/rpc/jsonrpc"
"log"
"os"
)
type Args struct{
A, B int
}type Quotient struct{
Quo, Rem int
}func main() {
if len(os.Args) != 2{
fmt.Println("Usage: ", os.Args[0], "server:port")
os.Exit(1)
}
service := os.Args[1] client, err := jsonrpc.Dial("tcp", service)
if err != nil{
log.Fatal("dialing : ", err)
} //Synchronous call
args := Args{17, 8}
var reply int
err = client.Call("Arith.Multiply", args, &reply)
if err != nil{
log.Fatal("arith error : ", err)
}
fmt.Printf("Arith: %d*%d = %d \n", args.A, args.B, reply) var quot Quotient
err = client.Call("Arith.Divide", args, &quot)
if err != nil{
log.Fatal("arith error : ", err)
}
fmt.Printf("Arith: %d/%d = %d remainder %d\n", args.A, args.B, quot.Quo, quot.Rem)
}

客户端返回:

userdeMBP:go-learning user$ go run test.go 127.0.0.1:1234
Arith: 17*8 = 136
Arith: 17/8 = 2 remainder

func NewClient

func NewClient(conn io.ReadWriteCloser) *rpc.Client

NewClient返回一个新的rpc.Client,以管理对连接另一端的服务的请求。

func NewClientCodec

func NewClientCodec(conn io.ReadWriteCloser) rpc.ClientCodec

NewClientCodec返回一个在连接上使用JSON-RPC的rpc.ClientCodec。

func NewServerCodec

func NewServerCodec(conn io.ReadWriteCloser) rpc.ServerCodec

NewServerCodec返回一个在连接上使用JSON-RPC的rpc. ServerCodec。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,907
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,432
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727