-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.go
98 lines (82 loc) · 1.87 KB
/
worker.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"fmt"
"io"
"math/rand"
"net"
"os"
"strings"
"sync/atomic"
"time"
)
//实现随机文件名
func GetRoundName(size int) string {
str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ=_*&^$#%-|"
bytes := []byte(str)
result := []byte{}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < size; i++ {
result = append(result, bytes[r.Intn(len(bytes))])
}
return string(result)
}
var cnt int64 = 0
func process(conn net.Conn, fileName string) {
atomic.AddInt64(&cnt, 1)
defer conn.Close()
//按照文件名创建新文件
rand.Seed(time.Now().Unix())
fileName2 := strings.TrimRight(fileName, ".txt")
file, err := os.Create("/gopath/src/dis_test_file/worker/file/" + fileName2 + GetRoundName(20) + ".txt")
if err != nil {
fmt.Printf("os.Create() failed %v\n", err)
return
}
defer file.Close()
//从网络中读取数据
for {
buf := make([]byte, 4096)
n, err1 := conn.Read(buf)
//写入本地文件,读多少、写多少
file.Write(buf[:n])
if err1 != nil {
if err1 == io.EOF {
fmt.Println("文件", cnt, "完成")
} else {
fmt.Printf("conn.Read()方法执行错误 %v\n", err)
}
return
}
}
}
func main() {
tcpaddr, err := net.ResolveTCPAddr("tcp", ":30001")
if err != nil {
fmt.Println("resolve failed, err:", err)
return
}
//TCP协议监听目标端口
listen, err := net.ListenTCP("tcp", tcpaddr)
if err != nil {
fmt.Println("listen failed, err:", err)
return
}
defer listen.Close()
for {
conn, err := listen.Accept()
if err != nil {
fmt.Println("accept failed, err:", err)
continue
}
buf := make([]byte, 128)
n, err := conn.Read(buf)
if err != nil {
fmt.Printf("conn.Read() failed %v\n", err)
}
fileName := string(buf[:n])
//回写ok给发送端
conn.Write([]byte("ok"))
go process(conn, fileName)
fmt.Println(cnt)
}
}