[daily] Go language Bible -- example: concurrent Clock service exercises

Keywords: Go

Exercise 8.1: modify clock2 to support the incoming parameter as the port number, and then write a clockwall program. This program can communicate with multiple clock servers at the same time, read the time from multiple servers, and display the results of all services in one table, similar to the clock wall you see in some offices. If you have geographically distributed servers to use, let them run on different machines; or run multiple instances on the same machine, which listen to different ports and pretend to be in different time zones. Like this:

$ TZ=US/Eastern    ./clock2 -port 8010 &
$ TZ=Asia/Tokyo    ./clock2 -port 8020 &
$ TZ=Europe/London ./clock2 -port 8030 &
$ clockwall NewYork=localhost:8010 Tokyo=localhost:8020 London=localhost:8030

clock2.go

package main

import (
        "flag"
        "io"
        "log"
        "net"
        "time"
)

//Support incoming parameters as port numbers
var port = flag.String("port", "8000", "Please enter the port")

func main() {
        flag.Parse()
        listener, err := net.Listen("tcp", "localhost:"+*port)
        if err != nil {
                log.Fatal(err)
        }   

        for {
                conn, err := listener.Accept()
                if err != nil {
                        log.Print(err) // e.g., connection aborted
                        continue
                }   
                go handleConn(conn) //New goroutines processing connection
        }   
}

func handleConn(c net.Conn) {
        defer c.Close()
        for {
                _, err := io.WriteString(c, time.Now().Format("15:04:05\n"))
                if err != nil {
                        return // e.g., client disconnected
                }   
                time.Sleep(1 * time.Second)
        }   
}

clockwall.go

// Netcat1 is a read-only TCP client.
package main

import (
        "io"
        "log"
        "net"
        "os"
        "strings"
        "time"
)

func main() {
        for _, v := range os.Args[1:] {
                keyValue := strings.Split(v, "=")
                go connTcp(keyValue[1])
        }   
        for {
                time.Sleep(1 * time.Second)
        }   
}

func connTcp(uri string) {
        conn, err := net.Dial("tcp", uri)
        if err != nil {
                log.Fatal(err)
        }   
        defer conn.Close()
        mustCopy(os.Stdout, conn)

}

func mustCopy(dst io.Writer, src io.Reader) {
        if _, err := io.Copy(dst, src); err != nil {
                log.Fatal(err)
        }   
}

  

Posted by jb60606 on Mon, 30 Mar 2020 11:47:23 -0700