~craftyguy/slowping

78c2a445acaef3746cc93cadc4c8f137af7f95b9 — Clayton Craft 2 years ago eb5db09 master
cmd/*: add -h/--help options
2 files changed, 16 insertions(+), 2 deletions(-)

M cmd/slowping-client/main.go
M cmd/slowping-server/main.go
M cmd/slowping-client/main.go => cmd/slowping-client/main.go +8 -1
@@ 15,13 15,20 @@ func main() {
	var server string
	var port uint
	var interval uint
	var help bool

	flag.UintVar(&port, "p", 9000, "server's tcp port to connect to")
	flag.StringVar(&server, "s", "0.0.0.0", "server to connect to")
	flag.UintVar(&interval, "i", 1, "interval to send to server")
	flag.BoolVar(&help, "h", false, "Print help and quit.")

	flag.Parse()

	if help {
		flag.CommandLine.Usage()
		return
	}

	con, err := net.Dial("tcp", fmt.Sprintf("%s:%d", server, port))
	if err != nil {
		log.Fatalln(err)


@@ 38,7 45,7 @@ func main() {
		resp, err := sReader.ReadString('\n')
		switch err {
		case nil:
			log.Printf("got: %s\n", strings.TrimSpace(resp))
			log.Printf("%s\n", strings.TrimSpace(resp))
		case io.EOF:
			log.Println("connection closed")
			return

M cmd/slowping-server/main.go => cmd/slowping-server/main.go +8 -1
@@ 14,12 14,19 @@ import (
func main() {
	var port uint
	var delay uint
	var help bool

	flag.UintVar(&port, "p", 9000, "tcp port to listen on")
	flag.UintVar(&delay, "d", 5, "delay between receiving from client and responding back to client")
	flag.BoolVar(&help, "h", false, "Print help and quit.")

	flag.Parse()

	if help {
		flag.CommandLine.Usage()
		return
	}

	l, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", port))
	if err != nil {
		log.Fatalln(err)


@@ 47,7 54,7 @@ func handleCon(con net.Conn, delay time.Duration) {

		switch err {
		case nil:
			log.Printf("got: %s\n", strings.TrimSpace(req))
			log.Printf("%s\n", strings.TrimSpace(req))

			time.Sleep(delay * time.Second)