How to write a concurrent TCP server in Go

 Building a TCP Concurrent Server in Go: Harnessing the Power of Goroutines

In the world of networking and server development, concurrency is a fundamental aspect that enables handling multiple clients simultaneously, leading to better performance and responsiveness. Go, with its built-in concurrency features, provides an excellent platform for building efficient TCP servers. In this article, we'll explore the process of creating a TCP concurrent server in Go, taking advantage of Goroutines to handle multiple client connections concurrently.

Concurrent Server


Understanding TCP Concurrent Servers

A TCP server is a fundamental component of network programming that listens for incoming connections from clients and responds to their requests. It operates on the Transmission Control Protocol (TCP), a reliable and connection-oriented protocol that guarantees the delivery of data packets in the order they were sent. The TCP server binds to a specific IP address and port number, waiting for clients to establish connections. Once a connection is established, the server can exchange data with the client bidirectionally, allowing for real-time communication. TCP servers are widely used in various applications, such as web servers, chat applications, file transfer systems, and more, providing a robust foundation for building scalable and efficient network services.


TCP Server

A concurrent TCP server is an advanced networking application that combines the power of concurrent programming with the capabilities of a TCP server. Unlike traditional TCP servers, a concurrent TCP server can handle multiple client connections concurrently, allowing it to process multiple requests simultaneously without blocking other clients. This is achieved by utilizing concurrency primitives like Goroutines in Go or Threads in other programming languages. By employing parallelism, a concurrent TCP server can efficiently serve a large number of clients, enhancing its responsiveness and scalability. This makes it a preferred choice for high-traffic and real-time applications, such as chat servers, multiplayer games, or any system requiring simultaneous interactions with multiple clients. The use of concurrency in TCP servers significantly improves performance and resource utilization, making it a crucial aspect of modern network programming.


Concurrent TCP Server


The Power of Goroutines

Goroutines are a key feature of the Go programming language, and they are the building blocks of concurrent programming in Go. A Goroutine is a lightweight, independent execution unit that allows developers to execute functions concurrently without the complexities of traditional threads. Goroutines are highly efficient and can be created and destroyed with minimal overhead. They enable concurrent execution of tasks, allowing multiple operations to run simultaneously without the need for explicit thread management. Due to their lightweight nature, Goroutines make it easy to scale applications and handle a large number of concurrent operations efficiently. With Goroutines, Go programmers can write concurrent code with ease, enabling the creation of highly responsive and efficient applications. They are an essential feature for building highly scalable and responsive servers.


Implementing the TCP Concurrent Server

Let's create a simple TCP server that handles multiple client connections concurrently using Goroutines. It will create a goroutine for each new incoming TCP connection. That goroutine will execute an endless loop, where it just basically returns an echo of the received message to the client. Unless the message is the string "STOP", in which case, the goroutine for that connection will end, and the server will stop serving that client.


package main

import (
	"bufio"
	"log"
	"net"
	"strings"
)

func main() {
	l, err := net.Listen("tcp", ":8080")
	if err != nil {
		log.Fatal(err)
	}
	defer l.Close()

	for {
		c, err := l.Accept()
		if err != nil {
			log.Fatal(err)
		}
		go handleConnection(c)
	}
}

func handleConnection(c net.Conn) {
	defer c.Close()

	connReader := bufio.NewReader(c)

	log.Printf("Serving %s\n", c.RemoteAddr().String())
	for {
		data, err := connReader.ReadString('\n')
		if err != nil {
			log.Println(err)
			break
		}
		request := strings.TrimSpace(string(data))

		log.Printf("Request from %s: %s\n", c.RemoteAddr().String(), request)
		if request == "STOP" {
			break
		}
		c.Write([]byte(data))
	}
	log.Printf("Stop serving %s\n", c.RemoteAddr().String())
}
  


Once we have our server ready, we can test it using netcat in Linux. In order to do so, we just need to start the server in one terminal. And run the netcat command in another terminal using the address localhost and port 8080 to connect to the server. Then, we can start typing strings, and every time we press Enter we will see an echo of that message received from the server.


>nc localhost 8080
test1
test1
test2
test2

Meanwhile, in the server terminal we will see the messages received from the client. We can connect multiple clients from multiple terminal running the netcat in each one of them to check how the server is able to handle all their requests.


>./main
2023/08/02 10:13:35 Serving 127.0.0.1:58740
2023/08/02 10:13:38 Request from 127.0.0.1:58740: test1
2023/08/02 10:13:38 Request from 127.0.0.1:58740: test2
2023/08/02 10:13:51 Serving 127.0.0.1:58742
2023/08/02 10:13:57 Request from 127.0.0.1:58742: test3
2023/08/02 10:13:58 Request from 127.0.0.1:58742: test4
2023/08/02 10:14:01 Request from 127.0.0.1:58742: STOP
2023/08/02 10:14:01 Stop serving 127.0.0.1:58742
2023/08/02 10:14:04 Request from 127.0.0.1:58740: test5
2023/08/02 10:14:05 Request from 127.0.0.1:58740: test6


Conclusion

In this article, we explored the process of building a TCP concurrent server in Go, utilizing Goroutines to handle multiple client connections simultaneously. By leveraging the power of concurrency through Goroutines, our server can efficiently handle a large number of clients, leading to improved performance and responsiveness. With the knowledge gained from this example, you can further extend the server to incorporate your own business logic and create powerful network applications. Go's built-in concurrency features make it an ideal choice for developing high-performance servers, and mastering the art of concurrent programming opens up a world of possibilities for creating scalable and efficient applications. 

Popular posts from this blog

How to setup NeoVim configuration file

WebAssembly (Wasm): Fixing the Flaws of Applets

How to query Outlook from PowerShell