package main
import (
"fmt"
"net"
"os"
"time"
)
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "Usage: %s host:port\n", os.Args[0])
os.Exit(1)
}
service := os.Args[1]
conn, err := net.Dial("tcp", service)
if err != nil {
fmt.Fprintf(os.Stderr, "Error dialing: %s\n", err)
os.Exit(1)
}
defer conn.Close()
go func() {
t := time.NewTicker(time.Second)
defer t.Stop()
for {
<-t.C
conn.Write([]byte("Hello, server!\n"))
}
}()
buf := make([]byte, 512)
for {
n, err := conn.Read(buf)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading: %s\n", err)
os.Exit(1)
}
fmt.Print(string(buf[:n]))
}
}