Developing a TCP server
This section presents two ways of developing TCP servers that can interact with TCP clients, just as we did with the TCP client.
Developing a TCP server with net.Listen()
The TCP server presented in this section, which uses net.Listen()
, returns the current date and time to the client in a single network packet. In practice, this means that after accepting a client connection, the server gets the time and date from the operating system and sends that data back to the client. The net.Listen()
function listens for connections, whereas the net.Accept()
method waits for the next connection and returns a generic net.Conn
variable with the client information. The code of tcpS.go
is as follows:
package main
import (
"bufio"
"fmt"
"net"
"os"
"strings"
"time"
)
func main() {
arguments := os.Args
if len(arguments) == 1 {
fmt.Println("Please provide port...