added simple client/server

This commit is contained in:
Stephen McQuay 2015-02-04 21:08:44 -08:00
commit 72ba52557f
3 changed files with 87 additions and 0 deletions

43
cmd/rrecv/main.go Normal file
View File

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

41
cmd/rsend/main.go Normal file
View File

@ -0,0 +1,41 @@
package main
import (
"bufio"
"fmt"
"net"
"os"
"mcquay.me/robo"
)
const usage = "rsend <hostname>"
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)
}
}

3
robo.go Normal file
View File

@ -0,0 +1,3 @@
package robo
const Port = 1337