Created
March 8, 2013 03:01
-
-
Save physacco/5113907 to your computer and use it in GitHub Desktop.
A TCP echo client demo.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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