-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.go
59 lines (51 loc) · 1.11 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
package main
import (
"fmt"
"github.com/ikilobyte/netman/util"
"net"
"os"
"sync"
"time"
)
func main() {
fmt.Println(os.Getpid())
wg := &sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(1)
go connect(i, wg)
}
wg.Wait()
}
func connect(id int, wg *sync.WaitGroup) {
defer wg.Done()
conn, err := net.Dial("udp", "127.0.0.1:6565")
if err != nil {
fmt.Printf("id dail udp err %v\n", err)
return
}
packer := util.NewDataPacker()
packet, _ := packer.Pack(0, []byte(fmt.Sprintf("from %d hello udp server", id)))
headLen := int(packer.GetHeaderLength())
for {
// 发送数据
_, err := conn.Write(packet)
if err != nil {
fmt.Printf("id@%d write err %v\n", id, err)
return
}
// 读取数据
buffer := make([]byte, 1024)
_, err = conn.Read(buffer)
if err != nil {
fmt.Printf("id@%d read err %v\n", id, err)
return
}
message, err := packer.UnPack(buffer)
if err != nil {
fmt.Printf("id@%d unpack err %v\n", id, err)
}
message.SetData(buffer[headLen : headLen+message.Len()])
fmt.Printf("id@%d recv from server %s\n", id, message.String())
time.Sleep(time.Second * 2)
}
}