标签:rom bsp listen pad 大小 取数据 golang add net
通过Goroutine实现UDP消息并发处理
package main import ( "net" "fmt" "os" ) // 限制goroutine数量 var limitChan = make(chan bool, 1000) // UDP goroutine 实现并发读取UDP数据 func udpProcess(conn *net.UDPConn) { // 最大读取数据大小 data := make([]byte, 1024) n, _, err := conn.ReadFromUDP(data) if err != nil { fmt.Println("failed read udp msg, error: " + err.Error()) } str := string(data[:n]) fmt.Println("receive from client, data:" + str) <- limitChan } func udpServer(address string) { udpAddr, err := net.ResolveUDPAddr("udp", address) conn, err := net.ListenUDP("udp", udpAddr) defer conn.Close() if err != nil { fmt.Println("read from connect failed, err:" + err.Error()) os.Exit(1) } for { limitChan <- true go udpProcess(conn) } } func main() { address := "0.0.0.0:8080" udpServer(address) }
标签:rom bsp listen pad 大小 取数据 golang add net
原文地址:https://www.cnblogs.com/vincenshen/p/10800336.html