Search⌘ K
AI Features

Developing a TCP Server That Uses net.ListenTCP()

Explore how to develop a TCP echo server in Go with net.ListenTCP. Understand handling client connections, processing input with space trimming, and closing connections on receiving STOP messages. Gain practical experience running the server alongside TCP clients.

We'll cover the following...

This time, this alternative version of the TCP server implements the echo service. Put simply, the TCP server sends back to the client the data that was received by the client.

Coding example

The code of otherTCPserver.go is as follows:

Go (1.19.0)
package main
import (
"fmt"
"net"
"os"
"strings"
)
func main() {
arguments := os.Args
if len(arguments) == 1 {
fmt.Println("Please provide a port number!")
return
}
SERVER := "localhost" + ":" + arguments[1]
s, err := net.ResolveTCPAddr("tcp", SERVER)
if err != nil {
fmt.Println(err)
return
}

The above code gets the TCP port number value as a command-line argument, which is used in ...