Commit 1d574ae6 authored by barnettZQG's avatar barnettZQG

change server

parent ddae7032
Pipeline #721 canceled with stages
...@@ -9,8 +9,10 @@ import ( ...@@ -9,8 +9,10 @@ import (
) )
func main() { func main() {
//address := "106.15.152.33:27065"
address := "127.0.0.1:8080" address := "127.0.0.1:8080"
con, err := net.Dial("udp", address) udpAddr, err := net.ResolveUDPAddr("udp", address)
con, err := net.DialUDP("udp", nil, udpAddr)
if err != nil { if err != nil {
logrus.Errorf("create udp client failure %s", err.Error()) logrus.Errorf("create udp client failure %s", err.Error())
} }
...@@ -19,8 +21,11 @@ func main() { ...@@ -19,8 +21,11 @@ func main() {
con.Write([]byte(fmt.Sprintf("hello word %d", n))) con.Write([]byte(fmt.Sprintf("hello word %d", n)))
n++ n++
data := make([]byte, 1024) data := make([]byte, 1024)
con.Read(data) n, _, err := con.ReadFromUDP(data)
fmt.Println(string(data)) if err != nil {
time.Sleep(time.Second * 5) fmt.Println("failed read udp msg, error: " + err.Error())
}
fmt.Println(string(data[:n]))
time.Sleep(time.Second * 3)
} }
} }
...@@ -15,13 +15,13 @@ var limitChan = make(chan bool, 1000) ...@@ -15,13 +15,13 @@ var limitChan = make(chan bool, 1000)
func udpProcess(conn *net.UDPConn, index int) { func udpProcess(conn *net.UDPConn, index int) {
// 最大读取数据大小 // 最大读取数据大小
data := make([]byte, 1024) data := make([]byte, 1024)
n, _, err := conn.ReadFromUDP(data) n, rAddr, err := conn.ReadFromUDP(data)
if err != nil { if err != nil {
fmt.Println("failed read udp msg, error: " + err.Error()) fmt.Println("failed read udp msg, error: " + err.Error())
} }
str := string(data[:n]) str := string(data[:n])
fmt.Println("receive from client, data:" + str) fmt.Println("receive from client, data:" + str)
conn.Write([]byte(fmt.Sprintf("receive %d", index))) conn.WriteToUDP([]byte(fmt.Sprintf("receive %d", index)), rAddr)
<-limitChan <-limitChan
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment