[Daily] Go Language Bible--Example: Concurrent Clock Service Exercises

Exercise 8.1: Modify clock2 to support incoming parameters as port numbers, then write a clockwall program that communicates with multiple clock servers at the same time, reads the time from multiple servers, and displays all service transfers in a table at once The result is similar to the clock wall you see in some offices. If you have geographically distributed servers available, have those servers run on different machines; or run multiple different instances on the same machine that listen on different ports and pretend to be in different time zones . Like the following:

$ 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) //Create new goroutines to handle the 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)
        }   
}

  

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324906631&siteId=291194637