diff options
Diffstat (limited to 'sock2file')
-rw-r--r-- | sock2file/go.mod | 3 | ||||
-rw-r--r-- | sock2file/sock2file.go | 40 |
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() + } +} |