Go net/rpc package is an implementation of RPC (Remote Procedure Call) system in Go language. The package offers a way for client-server communications, which allows a server to call methods on a client just like local methods.
Here are some examples of using net/rpc Server package in Go: - **Example 1: Basic RPC server setup**
This example demonstrates the basic setup of an RPC server in Go. We define a type with a few methods that we want to expose to the clients. Then, we create a server instance and register our type with it. We then use the server's ListenAndServe method to start the server and listen for incoming client requests.
package main
import ( "fmt" "net" "net/rpc" )
type Math struct{}
func (m *Math) Add(args []int, reply *int) error { sum := 0 for _, v := range args { sum += v } *reply = sum return nil }
func main() { math := new(Math) rpc.Register(math) listener, err := net.Listen("tcp", ":1234") if err != nil { fmt.Println(err) return } defer listener.Close() fmt.Println("Server started ...") rpc.Accept(listener) }
In this example, we created a Math struct that has one method Add that takes an array of integers and returns their summation. We then registered this struct with the RPC server, created a listener on port 1234 and started accepting incoming client requests.
- **Example 2: Two-way communication between client and server**
This example demonstrates how to enable two-way communication between the client and server using net/rpc Server package. We define a type with a method that we want to expose to the client. The server sends a message to the client and the client replies with a response message.
package main
import ( "fmt" "net" "net/rpc" )
type Server struct{}
func (s *Server) Ping(msg string, reply *string) error { fmt.Println("Server received message:", msg) *reply = "pong" return nil }
func main() { rpc.Register(new(Server)) listener, err := net.Listen("tcp", ":1234") if err != nil { fmt.Println(err) return } defer listener.Close() fmt.Println("Server started ...") for { conn, err := listener.Accept() if err != nil { fmt.Println(err) continue } go rpc.ServeConn(conn) fmt.Println("Server sending message ...") var response string err = rpc.Call(conn, "Server.Ping", "ping", &response) if err != nil { fmt.Println(err) continue } fmt.Println("Server received response:", response) } }
In this example, we defined a Server struct with one method Ping that takes a string message from the client and returns a string message in response. The server sends a message "ping" to the client, which responds with "pong". The server receives the response and prints it to the console.
In conclusion, net/rpc package is a go library package that provides a simple and efficient way of creating client-server architectures using Remote Procedure Call (RPC).
Golang Server - 30 examples found. These are the top rated real world Golang examples of net/rpc.Server extracted from open source projects. You can rate examples to help us improve the quality of examples.