Skip to content

Instantly share code, notes, and snippets.

@shreyaskarnik
Created June 9, 2022 05:19
Show Gist options
  • Save shreyaskarnik/577c32a43d4d9562fa8bdbdb91f28f3d to your computer and use it in GitHub Desktop.
Save shreyaskarnik/577c32a43d4d9562fa8bdbdb91f28f3d to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"errors"
"fmt"
"io"
"net"
"os"
"os/signal"
"syscall"
)
func main() {
c, err := net.Dial("tcp", "tcp-echo.fly.dev:5001")
if err != nil {
panic(err)
}
defer c.Close()
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go func() {
s := <-sig
c.Close()
// translate the signal to a int
os.Exit(int(s.(syscall.Signal)))
}()
for {
reader := bufio.NewReader(os.Stdin)
line, err := reader.ReadString('\n')
if errors.Is(err, io.EOF) {
break
}
if err != nil {
panic(err)
}
if len(line) == 0 {
break
}
_, err = c.Write([]byte(line))
if err != nil {
panic(err)
}
message, _ := bufio.NewReader(c).ReadBytes('\n')
fmt.Println(string(message))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment