Golang--RPC

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Built-in GOB

Rpcserver

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 ("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")     checkerror (Err)     listener,  err := net. LISTENTCP ("TCP",  TCPADDR)     checkerror (err)     /*         rpc. Accept (Listener)     */    for {         conn, err := listener. Accept ()         if err != nil {             continue         }        rpc. SERVECONN (conn)     }}func checkerror (Err error) &NBSp {    if err != nil {         Fmt. Println ("fatal error ",  err. Error ())         os. Exit (1)     }}

Rpcclient

package mainimport  (     "FMT"      "Log"       "NET/RPC"      "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 := rpc. Dial ("TCP",  service)     if err != nil {         log. Fatal ("Dialing:",  err)     }    // synchronous call     args := args{17,&nBsp;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)}

Test

Run./server run./client 127.0.0.1:1234


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.