Menu Close

Go – ServeConn Function in net/rpc package in Go

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

Introduction:

Go, also known as Golang, is a powerful programming language designed 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 ServeConn function, an important feature of the ‘net/rpc’ package, and discuss its purpose, usage, and a practical example to better understand its functionality.

What is ServeConn Function ?

The ServeConn function is part of the ‘net/rpc’ package in Go, which primarily deals with creating RPC clients and servers. This function serves a single connection, using the default ‘gob’ codec to decode requests and encode responses. ServeConn enables developers to serve individual connections without needing to implement a listener or serve a codec.

Function Signature

The function signature for ServeConn is as follows:

func (server *Server) ServeConn(conn io.ReadWriteCloser)

The ServeConn function is a method of the ‘Server’ type in the ‘net/rpc’ package. It takes an ‘io.ReadWriteCloser’ as input, which is an interface that combines the ‘io.Reader’, ‘io.Writer’, and ‘io.Closer’ interfaces. It represents a connection that can read, write, and close.

Example

Let’s take a look at an example to understand how the ServeConn function works in conjunction with the ‘net/rpc’ package:

package main

import (
	"fmt"
	"net"
	"net/rpc"
)

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.DefaultServer.ServeConn(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 ‘ServeConn()’ function on the ‘rpc’ default server instance.

Note that this example demonstrates the server-side implementation of the RPC system using the default ‘gob’ codec. To fully test the functionality, you would need to create a separate client-side implementation to connect to this server and call the ‘Greet’ method.

Conclusion

The ServeConn function in Go’s ‘net/rpc’ package is a valuable tool for building RPC servers that serve individual connections using the default ‘gob’ codec. This function simplifies the process of serving connections and provides developers with an easy way to handle incoming RPC requests. The example above demonstrates the simplicity of using the ServeConn function along with the ‘net/rpc’ package

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

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

Posted in golang, net, packages

Leave a Reply

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