Developing concurrent TCP servers
This section teaches you a pattern for developing concurrent TCP servers, which are servers that use 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, myCount int) {
fmt.Print(".")
The previous statement is not required—it just informs us that a new client has been connected.
netData, err := bufio.NewReader(c).ReadString('\n')
if err != nil {
fmt.Println(err)
return
}
for {
temp := strings.TrimSpace(string(netData))
if temp == "STOP"...