golang-exercises/gob-file/main.go

43 lines
560 B
Go
Raw Permalink Normal View History

package main
import (
"encoding/gob"
"fmt"
"log"
"net"
"os"
)
type File struct {
Filename string
File []byte
}
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 := File{}
err := dec.Decode(&c)
if err != nil {
log.Println(err)
return
}
err = os.WriteFile("./foobar.png", c.File, 0664)
}