Skip to content

Instantly share code, notes, and snippets.

@physacco
Created March 8, 2013 03:01
Show Gist options
  • Save physacco/5113907 to your computer and use it in GitHub Desktop.
Save physacco/5113907 to your computer and use it in GitHub Desktop.
A TCP echo client demo.
package main
import (
"os"
"fmt"
"net"
"time"
"bufio"
)
func main() {
conn, err := net.Dial("tcp", "localhost:4567")
if err != nil {
fmt.Printf("Dial error: %s\n", err)
os.Exit(1)
}
reader := bufio.NewReader(conn)
writer := bufio.NewWriter(conn)
for {
now := time.Now().Format(time.RFC3339)
writer.WriteString(now + "\n")
writer.Flush()
line, err := reader.ReadString('\n')
if err != nil {
fmt.Printf("ReadString error: %s\n", err)
conn.Close()
break
}
fmt.Printf(line)
time.Sleep(1000 * time.Millisecond)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment