-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (68 loc) · 1.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"bytes"
"crypto/rand"
"encoding/binary"
"fmt"
"io"
"log"
"net"
"time"
)
type FileServer struct { }
func (fs FileServer) start() {
tcp, err := net.Listen("tcp",":3000")
if err != nil {
log.Fatal(err)
}
defer tcp.Close()
for {
c, err := tcp.Accept()
if err != nil {
log.Fatal(err)
}
go fs.readLoop(c)
}
}
func (fs *FileServer)readLoop (c net.Conn){
packet := new(bytes.Buffer)
for {
var size int64
err := binary.Read(c, binary.LittleEndian, &size)
if err != nil {
fmt.Errorf("Not able to read from binary")
}
n, err := io.CopyN(packet, c, int64(size))
if err != nil {
log.Fatal(err)
}
fmt.Println(packet.Bytes())
fmt.Println("recieved %d bytes over the network",n)
}
}
func sendFile(size int) error{
file := make([]byte, size)
_, err := io.ReadFull(rand.Reader, file)
if err != nil {
return err
}
conn, err := net.Dial("tcp",":3000")
if err != nil {
return err
}
binary.Write(conn, binary.LittleEndian, int64(size))
n, err := io.CopyN(conn, bytes.NewReader(file),int64(size))
if err != nil {
return err
}
fmt.Println("written %d bytes over the network", n)
return nil
}
func main() {
go func(){
time.Sleep(3 * time.Second)
sendFile(20000)
}()
server := &FileServer{}
server.start()
}