diff --git a/gob-listfiles/client/go.mod b/gob-listfiles/client/go.mod new file mode 100644 index 0000000..2edee48 --- /dev/null +++ b/gob-listfiles/client/go.mod @@ -0,0 +1,3 @@ +module client + +go 1.22.4 diff --git a/gob-listfiles/client/main.go b/gob-listfiles/client/main.go new file mode 100644 index 0000000..799f3d0 --- /dev/null +++ b/gob-listfiles/client/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "encoding/gob" + "log" + "net" + "os" +) + +type FileList struct { + List []Item +} + +type Item struct { + Name string + IsFolder bool +} + +func main() { + conn, err := net.Dial("tcp", "127.0.0.1:1337") + if err != nil { + log.Fatal(err) + } + defer conn.Close() + + newList := FileList{} + + list, _ := os.ReadDir("/home/raul") + for _, v := range list { + item := Item{ + Name: v.Name(), + IsFolder: v.IsDir(), + } + newList.List = append(newList.List, item) + } + + enc := gob.NewEncoder(conn) + err = enc.Encode(&newList) + if err != nil { + log.Fatalf("Error happened encoding: %v\n", err) + } + +} diff --git a/gob-listfiles/go.mod b/gob-listfiles/go.mod new file mode 100644 index 0000000..a30201f --- /dev/null +++ b/gob-listfiles/go.mod @@ -0,0 +1,3 @@ +module gob-file + +go 1.22.4 diff --git a/gob-listfiles/main.go b/gob-listfiles/main.go new file mode 100644 index 0000000..6359b15 --- /dev/null +++ b/gob-listfiles/main.go @@ -0,0 +1,47 @@ +package main + +import ( + "encoding/gob" + "fmt" + "log" + "net" +) + +type FileList struct { + List []Item +} + +type Item struct { + Name string + IsFolder bool +} + +func main() { + fmt.Println("Listening on port 1337") + ln, err := net.Listen("tcp", ":1337") + if err != nil { + log.Fatal(err) + } + + for { + conn, err := ln.Accept() + if err != nil { + log.Println(err) + } + go receiveFile(conn) + } + +} + +func receiveFile(conn net.Conn) { + dec := gob.NewDecoder(conn) + c := FileList{} + err := dec.Decode(&c) + if err != nil { + log.Println(err) + return + } + for _, v := range c.List { + fmt.Println(v.Name, v.IsFolder) + } +}