SoFunction
Updated on 2025-03-04

Example of remote call of go language net package rpc

The rpc package provides an external method to enter an object through a network or other I/O connection. A server registers an object and marks it as a service with the visible object type name. After registration, the external method of the object can be called remotely. A server can register multiple objects of different types, but it cannot register multiple objects of the same type.

Only methods that meet these criteria will be considered visible by remote calls; other methods will be ignored:

- The method is externally visible.
- The method has two parameters, and the parameters' types are both visible externally.
- The second parameter of the method is a pointer.
- The method has a return type error

1. RPC based on http

Server:

package main;

 

import (

  "net/rpc"

  "net/http"

  "log"

)

 

//Go supports RPC, supports three levels: TCP, HTTP, JSONRPC
//Go's RPC only supports the interaction between the server and the client developed by GO, because it uses gob encoding
 

//Note that the field must be exported
type Params struct {

  Width, Height int;

}

 

type Rect struct{}

 

//The function must be exported
// There must be two export type parameters
//The first parameter is the receiving parameter
//The second parameter is returned to the client parameter, which must be a pointer type
//The function must also have a return value error
func (r *Rect) Area(p Params, ret *int) error {

  *ret =  * ;

  return nil;

}

 

func (r *Rect) Perimeter(p Params, ret *int) error {

  *ret = ( + ) * 2;

  return nil;

}

 

func main() {

  rect := new(Rect);

  //Register a rect service
  (rect);

  //Bind service processing to http protocol
  ();

  err := (":8080", nil);

  if err != nil {

    (err);

  }

} 

Client:

package main;

 

import (

  "net/rpc"

  "log"

  "fmt"

)

 

type Params struct {

  Width, Height int;

}

 

func main() {

  //Connect remote rpc service
  rpc, err := ("tcp", "127.0.0.1:8080");

  if err != nil {

    (err);

  }

  ret := 0;

  //Calling remote method
  //Note that the third parameter is the pointer type
  err2 := ("", Params{50, 100}, &ret);

  if err2 != nil {

    (err2);

  }

  (ret);

  err3 := ("", Params{50, 100}, &ret);

  if err3 != nil {

    (err3);

  }

  (ret);

} 

2. RPC based on tcp

Server:

package main;

 

import (

  "net"

  "log"

  "net/rpc"

)

 

//Note that the field must be exported
type Params struct {

  Width, Height int;

}

 

type Rect struct{}

 

func (r *Rect) Area(p Params, ret *int) error {

  *ret =  * ;

  return nil;

}

 

func (r *Rect) Perimeter(p Params, ret *int) error {

  *ret = ( + ) * 2;

  return nil;

}

 

func chkError(err error) {

  if err != nil {

    (err);

  }

}

 

func main() {

  rect := new(Rect);

  //Register rpc service
  (rect);

  //Get tcpaddr
  tcpaddr, err := ("tcp4", "127.0.0.1:8080");

  chkError(err);

  // Listen to the port
  tcplisten, err2 := ("tcp", tcpaddr);

  chkError(err2);

  //The dead loop handles the connection request
  for {

    conn, err3 := ();

    if err3 != nil {

      continue;

    }

    //Use goroutine to handle rpc connection requests separately
    go (conn);

  }

} 

Client:

package main;

 

import (

  "net/rpc"

  "fmt"

  "log"

)

 

type Params struct {

  Width, Height int;

}

 

func main() {

  //Connect remote rpc service
  //Dial is used here, and DialHTTP is used when http, and other codes are the same.
  rpc, err := ("tcp", "127.0.0.1:8080");

  if err != nil {

    (err);

  }

  ret := 0;

  //Calling remote method
  //Note that the third parameter is the pointer type
  err2 := ("", Params{50, 100}, &ret);

  if err2 != nil {

    (err2);

  }

  (ret);

  err3 := ("", Params{50, 100}, &ret);

  if err3 != nil {

    (err3);

  }

  (ret);

} 

3. JSON RPC method

The jsonrpc method is that data encoding uses json, not gob encoding.

Server:

package main;

 

import (

  "net"

  "log"

  "net/rpc"

  "net/rpc/jsonrpc"

)

 

//Note that the field must be exported
type Params struct {

  Width, Height int;

}

 

type Rect struct{}

 

func (r *Rect) Area(p Params, ret *int) error {

  *ret =  * ;

  return nil;

}

 

func (r *Rect) Perimeter(p Params, ret *int) error {

  *ret = ( + ) * 2;

  return nil;

}

 

func chkError(err error) {

  if err != nil {

    (err);

  }

}

 

func main() {

  rect := new(Rect);

  //Register rpc service
  (rect);

  //Get tcpaddr
  tcpaddr, err := ("tcp4", "127.0.0.1:8080");

  chkError(err);

  // Listen to the port
  tcplisten, err2 := ("tcp", tcpaddr);

  chkError(err2);

  for {

    conn, err3 := ();

    if err3 != nil {

      continue;

    }

    //Use goroutine to handle rpc connection requests separately
    //Jsonrpc is used for processing here
    go (conn);

  }

} 

Client:

 package main;

 

import (

  "fmt"

  "log"

  "net/rpc/jsonrpc"

)

 

type Params struct {

  Width, Height int;

}

 

func main() {

  //Connect remote rpc service
  // Used here
  rpc, err := ("tcp", "127.0.0.1:8080");

  if err != nil {

    (err);

  }

  ret := 0;

  //Calling remote method
  //Note that the third parameter is the pointer type
  err2 := ("", Params{50, 100}, &ret);

  if err2 != nil {

    (err2);

  }

  (ret);

  err3 := ("", Params{50, 100}, &ret);

  if err3 != nil {

    (err3);

  }

  (ret);

} 

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.