-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
64 lines (60 loc) · 1.28 KB
/
client.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
package main
import (
"fmt"
"net"
"bufio"
"os"
"strconv"
)
func main() {
var conn [257]net.Conn
var err error
for i := 0; i < 257; i++ {
addr := "127.0.0.1:" + strconv.Itoa(9000+i)
conn[i], err = net.Dial("udp", addr)
if err != nil {
fmt.Printf("Dial error %v", err)
return
}
defer conn[i].Close()
}
fmt.Printf("Starting readers...")
for i := 0; i < 256; i++ {
go reader(conn[i], i, conn[256])
}
fmt.Printf("Done.\n")
fmt.Printf("Initializing connections...")
for i := 0; i < 256; i++ {
fmt.Fprintf(conn[i], "")
}
fmt.Printf("Done.\n")
fmt.Printf("Sending start signal...")
fmt.Fprintf(conn[256], "")
fmt.Printf("Done.\n")
fmt.Printf("Receiving file...")
outFile, _ := os.Create("testfile")
outFile.Close()
p := make([]byte, 1)
// Wait for server to send end of file signal
_, _ = bufio.NewReader(conn[256]).Read(p)
fmt.Printf("Complete!\n")
return
}
func reader(conn net.Conn, val int, nextConn net.Conn) {
for {
p := make([]byte, 1)
_, err := bufio.NewReader(conn).Read(p)
if err == nil {
writer(byte(val))
}
// Send signal for next byte
fmt.Fprintf(nextConn, "")
}
}
func writer(v byte) {
val := make([]byte, 1)
val[0] = v
outFile, _ := os.OpenFile("testfile",os.O_APPEND|os.O_WRONLY,0666)
_, _ = outFile.Write(val)
outFile.Close()
}