summaryrefslogtreecommitdiff
path: root/sock2file
diff options
context:
space:
mode:
authorCody Hiar <cody@hiar.ca>2022-03-05 12:53:15 -0700
committerCody Hiar <cody@hiar.ca>2022-03-05 12:53:15 -0700
commit90e2e053d25feabd2dcf384dfc262a8bcdbe3ab8 (patch)
tree8e2799822b4de29014b86ca708123c1dfc8f45f5 /sock2file
Initial CommitHEADmaster
Diffstat (limited to 'sock2file')
-rw-r--r--sock2file/go.mod3
-rw-r--r--sock2file/sock2file.go40
2 files changed, 43 insertions, 0 deletions
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()
+ }
+}