Developing concurrent TCP servers
This section teaches a pattern for developing concurrent TCP servers, which are servers that are using separate goroutines to serve their clients following a successful Accept()
call. Therefore, such servers can serve multiple TCP clients at the same time. This is how real-world production servers and services are implemented.
The code of concTCP.go
is as follows:
package main
import (
"bufio"
"fmt"
"net"
"os"
"strconv"
"strings"
)
var count = 0
func handleConnection(c net.Conn) {
fmt.Print(".")
The previous statement is not required—it just informs us that a new client has been connected.
for {
netData, err := bufio.NewReader(c).ReadString('\n')
if err != nil {
fmt.Println(err)
return
}
temp := strings.TrimSpace(string(netData))
if temp == "STOP...