From 90e2e053d25feabd2dcf384dfc262a8bcdbe3ab8 Mon Sep 17 00:00:00 2001 From: Cody Hiar Date: Sat, 5 Mar 2022 12:53:15 -0700 Subject: Initial Commit --- sock2file/go.mod | 3 +++ sock2file/sock2file.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 sock2file/go.mod create mode 100644 sock2file/sock2file.go (limited to 'sock2file') diff --git a/sock2file/go.mod b/sock2file/go.mod new file mode 100644 index 0000000..01aa5e2 --- /dev/null +++ b/sock2file/go.mod @@ -0,0 +1,3 @@ +module sock2file + +go 1.15 diff --git a/sock2file/sock2file.go b/sock2file/sock2file.go new file mode 100644 index 0000000..3443442 --- /dev/null +++ b/sock2file/sock2file.go @@ -0,0 +1,40 @@ +package main + +import ( + "fmt" + "net" + "time" + "os" + "bufio" +) + +func main() { + ln, _ := net.Listen("tcp", ":8000") + file, _ := os.Create("./test.logs") + writer := bufio.NewWriter(file) + + for { + conn, _ := ln.Accept() + conn.SetReadDeadline(time.Now().Add(time.Second)) + recvData := make([]byte, 1024) + n, err := conn.Read(recvData) + if n > 0 { + fmt.Println("Data Received") + mystring := string(recvData[:]) + fmt.Println(mystring) + written, err := writer.WriteString(mystring) + if err != nil { + fmt.Printf("Error %s", err) + } + fmt.Printf("Written: %d\n", written) + fmt.Printf("Available: %d\n", writer.Available()) + fmt.Printf("Buffered : %d\n", writer.Buffered()) + } + if e, ok := err.(interface{ Timeout() bool }); ok && e.Timeout() { + fmt.Println("Timeout") + } else if err != nil { + fmt.Println("Error") + } + conn.Close() + } +} -- cgit v1.2.3