Client-Server model implementation in Golang

Nikhil Vaidyar
4 min readSep 8, 2022

In this blog, I will try my best to explain the client and server model and how it is so important and how we use it daily in our assigned tasks.

Photo by Lucian Alex on Unsplash

Let’s start this topic with some real-world scenarios, right? So, let’s say we need to explain the client-server model in a simple daily life way. Suppose, you called a pizza delivery company called “YourPizzaBuddy” and then tell an executive on the other side asked you about the details like which pizza would you like to have and then you say I need margarita pizza with bell pepper toppings on it and some cold drinks too and then after some time this order got confirmed and after 30 minutes, you got your pizza delivered by pizza delivery guy. Well, that’s a system which is also a bit similar to the client-server model. Like, we as a client requested a response called pizza and from the server side, it was processing our order and we got pizza in response, the same thing we requested. Well, I will now explain this topic in a more technical way.

What is the client-server model?

It is a model in which the client is requesting data or some service which gets processed by the server and the server process the desired request which is coming from the client.

Client-server model representation

So, let's get a bit deep into this model, let’s say what happens when you type a URL on your internet browser like google chrome, safari etc. in response you get the desired webpage response. So, how does this happens actually?

When you type any URL on the search bar of the browser, that URL is actually mapped to a unique IP address and that IP is then mapped to a DNS, which describes the entities and protocols from DNS to IP address. Suppose, if we search for a URL that is not cached in our browser, what happens? Actually, in that case, if the URL is not in our browser and OS cache, then the resolver will ping to root and then root will provide the possible TLDs(Top-Level Domains) after that it will provide the name-server, hence the name-server is created when you register a domain, so domain name-servers provide stores with the IP address and hence you will get the desired result. Well, I got to learn all this from this website: https://howdns.works. They have described the whole journey of how DNS works in an interactive and fun way.

Implementation using terminal

Using the Netcat command, you can also create your client-server model in your OS terminal. It’s a command-line utility to do Read-Write operations on computer networks.

To start the server on your local machine, just open your terminal and type this command

nc 127.0.0.1 8080

This command starts the server on your terminal and with that, you can also open another terminal and type the command, it will listen on port 80 and show the response on the terminal which is listening from the system default host.

nc -l 8080

NC — means Netcat command
127.0.0.1 — It is a localhost address of a device
8080 — port

Here’s the representation for the above:

Implementation using terminal

Implementation in golang

Photo by Possessed Photography on Unsplash

We can also implement the same in golang using goroutines. Goroutines are lightweight threads that work concurrently to achieve the output. Well, there’s more to goroutines, which I will explain in my upcoming series. Here’s the working of the client-server alike model using golang. We will create one client and server which will concurrently show the message coming from the server.

Code implementation

In the implementation of the server.go, we will create two functions one to handle the outgoing messages which are concurrently sending the “Hi from server” message to the client which is listening on port 8080. Whereas, with the implementation of the client.go it reads and write the I/O to the terminal.

Server

server

Client

client

From the above approach, you will get the desired output and get to know the importance of goroutines.

Implementation using golang

I have also created a repository for the various upcoming system design fundamentals and we try to replicate the system design using golang. You can go out and check out this link: https://github.com/nikzayn/golang-system-design. Later on, I will explain the goroutines in a bit more detail.

--

--

Nikhil Vaidyar

Maintaining consistency | CNCF Contributor | Golang | Docker | Kubernetes