From 9410140cad520b980947fff1b0dc1a3ffab0b56a Mon Sep 17 00:00:00 2001 From: 45Tatami Date: Fri, 31 Dec 2021 15:59:58 +0100 Subject: [PATCH] Add example client --- example_client/go.mod | 3 +++ example_client/main.go | 46 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 example_client/go.mod create mode 100644 example_client/main.go diff --git a/example_client/go.mod b/example_client/go.mod new file mode 100644 index 0000000..74046f5 --- /dev/null +++ b/example_client/go.mod @@ -0,0 +1,3 @@ +module gosend + +go 1.17 diff --git a/example_client/main.go b/example_client/main.go new file mode 100644 index 0000000..17209a2 --- /dev/null +++ b/example_client/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "bufio" + "encoding/binary" + "log" + "math" + "net" + "os" +) + +const remote_addr = "localhost:30501" + +func main() { + c, err := net.Dial("tcp", remote_addr) + if err != nil { + log.Panic("Error connecting:", err) + } + defer c.Close() + + log.Printf("Connected to '%s'. Please input lines\n", remote_addr) + + s := bufio.NewScanner(os.Stdin) + for s.Scan() { + msg := []byte(s.Text()) + + msg_len := len(msg) + if msg_len > math.MaxUint32 { + log.Println("Message too long") + continue + } + + len_buf := make([]byte, 4) + binary.LittleEndian.PutUint32(len_buf, uint32(msg_len)) + + if _, err := c.Write(len_buf); err != nil { + log.Panic("Error sending len:", err) + } + if _, err := c.Write(msg); err != nil { + log.Panic("Error sending payload:", err) + } + } + if err := s.Err(); err != nil { + log.Println("Error reading:", err) + } +}