From 72ba52557fc6c1307d38e8a743d327e686c615bd Mon Sep 17 00:00:00 2001 From: stephen mcquay Date: Wed, 4 Feb 2015 21:08:44 -0800 Subject: [PATCH] added simple client/server --- cmd/rrecv/main.go | 43 +++++++++++++++++++++++++++++++++++++++++++ cmd/rsend/main.go | 41 +++++++++++++++++++++++++++++++++++++++++ robo.go | 3 +++ 3 files changed, 87 insertions(+) create mode 100644 cmd/rrecv/main.go create mode 100644 cmd/rsend/main.go create mode 100644 robo.go diff --git a/cmd/rrecv/main.go b/cmd/rrecv/main.go new file mode 100644 index 0000000..bda8fe2 --- /dev/null +++ b/cmd/rrecv/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "bufio" + "fmt" + "net" + "os" + + "mcquay.me/robo" +) + +func main() { + ln, err := net.Listen("tcp", fmt.Sprintf(":%d", robo.Port)) + if err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + os.Exit(1) + + } + + for { + conn, err := ln.Accept() + if err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + os.Exit(1) + } + + s := bufio.NewScanner(conn) + for s.Scan() { + txt := s.Text() + fmt.Printf("sending: %s\n", txt) + _, err := conn.Write([]byte(txt + "\n")) + if err != nil { + break + } + } + if err := s.Err(); err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + os.Exit(1) + } + + fmt.Printf("disconnect: %+v\n", conn) + } +} diff --git a/cmd/rsend/main.go b/cmd/rsend/main.go new file mode 100644 index 0000000..225d905 --- /dev/null +++ b/cmd/rsend/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "bufio" + "fmt" + "net" + "os" + + "mcquay.me/robo" +) + +const usage = "rsend " + +func main() { + if len(os.Args) != 2 { + fmt.Fprintf(os.Stderr, "%s\n", usage) + os.Exit(1) + } + + conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", os.Args[1], robo.Port)) + if err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + os.Exit(1) + + } + + s := bufio.NewScanner(os.Stdin) + for s.Scan() { + txt := s.Text() + fmt.Printf("sending: %s\n", txt) + _, err := conn.Write([]byte(txt + "\n")) + if err != nil { + break + } + } + + if err := s.Err(); err != nil { + fmt.Fprintf(os.Stderr, "%v\n", err) + os.Exit(1) + } +} diff --git a/robo.go b/robo.go new file mode 100644 index 0000000..058e8f3 --- /dev/null +++ b/robo.go @@ -0,0 +1,3 @@ +package robo + +const Port = 1337