Menu Close

Go – ServeCodec Function in net/rpc package in Go

In this article, we will explore the ServeCodec Function in net/rpc package in Go in detail, along with examples.

Introduction:

Go, also known as Golang, is a powerful programming language suitable for building concurrent and networked applications. One of the standard libraries provided by Go is the ‘net/rpc’ package, which enables developers to create and use Remote Procedure Calls (RPC) for communication between different application components. In this article, we will explore the ServeCodec function, a key feature of the ‘net/rpc’ package, and discuss its purpose, usage, and a practical example to better understand its functionality.

What is ServeCodec Function ?

The ServeCodec function is part of the ‘net/rpc’ package in Go, which primarily focuses on creating RPC clients and servers. This function serves a single connection, using the provided codec to decode requests and encode responses. The ServeCodec function allows developers to use custom codecs for encoding and decoding, offering more flexibility and control over the communication process.

Function Signature

The function signature for ServeCodec is as follows:

func (server *Server) ServeCodec(codec ServerCodec)

The ServeCodec function is a method of the ‘Server’ type in the ‘net/rpc’ package. It takes a ‘ServerCodec’ as input, which is an interface used for reading requests and writing responses.

ServerCodec Interface:

The ServerCodec interface defines methods required for implementing a custom codec for reading requests and writing responses. The interface consists of three methods:

type ServerCodec interface {
	ReadRequestHeader(*Request) error
	ReadRequestBody(interface{}) error
	WriteResponse(*Response, interface{}) error
	Close() error
}

Example

In this example, we will demonstrate the usage of the ServeCodec function with a custom codec, the JSON-RPC codec. To achieve this, we will use the ‘github.com/gorilla/rpc/v2/json2’ package.

package main

import (
	"fmt"
	"net"
	"net/rpc"
	"github.com/gorilla/rpc/v2/json2"
)

type Greeter struct{}

func (g *Greeter) Greet(name *string, reply *string) error {

	*reply = fmt.Sprintf("Hello, %s!", *name)

	return nil
}

func main() {
	addr := "localhost:12345"

	greeter := new(Greeter)
	rpc.Register(greeter)

	listener, err := net.Listen("tcp", addr)

	if err != nil {
		fmt.Println("Error listening:", err)
		return
	}
	defer listener.Close()

	fmt.Printf("RPC server listening on %s\n", addr)

	for {
		conn, err := listener.Accept()

		if err != nil {
			fmt.Println("Error accepting connection:", err)
			continue
		}

		go rpc.ServeCodec(json2.NewCodec(conn))
	}
}

In this example, we create a simple RPC server that responds to Greet function calls. We define a ‘Greeter’ type and a ‘Greet’ method for that type. The ‘Greet’ method takes a name as input and returns a greeting message.

We then create an instance of ‘Greeter’, register it with the ‘net/rpc’ package using ‘rpc.Register()’, and create a listener using ‘net.Listen()’ on ‘localhost:12345’. Inside a for loop, we accept incoming connections and serve them using the ‘ServeCodec()’ function with a JSON-RPC codec provided by the ‘github.com/gorilla/rpc/v2/json2’ package.

To check more Go related articles. Pls click given below link:

https://www.techieindoor.com/category/leetcode/

Posted in golang, net, packages

Leave a Reply

Your email address will not be published. Required fields are marked *