Go语言学习之TCP RPC
来源:互联网 发布:编译c 的软件 编辑:程序博客网 时间:2024/06/16 11:00
server
package mainimport ( "errors" "fmt" "net" "net/rpc" "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("divede 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") if err != nil { fmt.Println("Fatal error:", err) os.Exit(1) } listener, err := net.ListenTCP("tcp", tcpAddr) if err != nil { fmt.Println("Fatal error:", err) os.Exit(1) } for { conn, err := listener.Accept() if err != nil { continue } rpc.ServeConn(conn) }}
client
package mainimport ( "fmt" "log" "net/rpc")type Args struct { A, B int}type Quotient struct { Quo, Rem int}func main() { service := "127.0.0.1:1234" client, err := rpc.Dial("tcp", service) if err != nil { log.Fatal("dialing:", err) } 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, ") 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)}
rpc原理
http://blog.csdn.net/libinbin_1014/article/details/73302757
阅读全文
1 0
- Go语言学习之TCP RPC
- Go语言学习之JSON RPC
- go语言RPC
- go语言网络编程之tcp
- GO语言网络编程之TCP (v1)
- GO语言网络编程之TCP (v2)
- Go语言学习之数据类型
- GO语言学习之goroutine
- Go语言学习之语言特性
- Go语言_RPC_Go语言的RPC
- GO语言学习--tcp服务器、客户机的通信
- 八、Go语言包之strings--go语言学习笔记
- Go学习之Go语言开发环境配置
- Go语言学习之Hello World(The way to go)
- Go语言学习之变量(The way to go)
- Go语言学习之常量(The way to go)
- Go语言学习之运算符(The way to go)
- Go语言学习之流程控制(The way to go)
- 信号灯的应用
- HDU 2642 Stars(二维树状数组 模板题)
- Visual Studio VS 2017 应用程序打包
- 4_代理模式
- hdu 1102 Constructing Roads
- Go语言学习之TCP RPC
- Codeforces 240F : TorCoder
- 知识点
- 对比groovy和java不同点。
- 学习笔记——JAVA设计模式<18>策略模式
- Python利用itchat库向好友或者公众号发消息
- 1.3 Android工程师的角色方向与定位
- ros kinetic +hokuyo 源码安装
- HDOJ 1115 Lifting the Stone (计算几何+多边形重心)