TCP client/server

This commit is contained in:
Meutel 2017-07-09 15:04:44 +02:00
parent fbb7bebd3b
commit a308cd85e0
3 changed files with 50 additions and 0 deletions

2
.gitignore vendored
View File

@ -44,3 +44,5 @@ example-json/example-json
financialjson/financialjson
statecsv2json/statecsv2json
converter-main/converter-main
tcpclient/tcpclient
tcpserver/tcpserver

25
tcpclient/main.go Normal file
View File

@ -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))
}

23
tcpserver/main.go Normal file
View File

@ -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()
}
}