From a308cd85e0a7033776a4fbdeed34de1600c172b8 Mon Sep 17 00:00:00 2001 From: Meutel Date: Sun, 9 Jul 2017 15:04:44 +0200 Subject: [PATCH] TCP client/server --- .gitignore | 2 ++ tcpclient/main.go | 25 +++++++++++++++++++++++++ tcpserver/main.go | 23 +++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 tcpclient/main.go create mode 100644 tcpserver/main.go diff --git a/.gitignore b/.gitignore index 499febb..ac7b209 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,5 @@ example-json/example-json financialjson/financialjson statecsv2json/statecsv2json converter-main/converter-main +tcpclient/tcpclient +tcpserver/tcpserver diff --git a/tcpclient/main.go b/tcpclient/main.go new file mode 100644 index 0000000..f247893 --- /dev/null +++ b/tcpclient/main.go @@ -0,0 +1,25 @@ +package main + +import "fmt" +import "io/ioutil" +import "net" +import "os" + +func main() { + addr := "localhost:9000" + if len(os.Args) == 2 { + addr = os.Args[1] + } + d, err := net.Dial("tcp", addr) + if err != nil { + panic(err) + } + defer d.Close() + + bs, err := ioutil.ReadAll(d) + if err != nil { + panic(err) + } + + fmt.Println(string(bs)) +} diff --git a/tcpserver/main.go b/tcpserver/main.go new file mode 100644 index 0000000..81cdd2a --- /dev/null +++ b/tcpserver/main.go @@ -0,0 +1,23 @@ +package main + +import "fmt" +import "io" +import "net" +import "time" + +func main() { + ln, err := net.Listen("tcp", ":9000") + if err != nil { + panic(err) + } + defer ln.Close() + + for { + conn, err := ln.Accept() + if err != nil { + panic(err) + } + io.WriteString(conn, fmt.Sprint(time.Now())) + conn.Close() + } +}