UDP通信功能
UDP通信功能
1. UDP交互沟通案例
效果
clent有点小问题,输出终止,不退出。
clent
server
服务器
1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "net" 7 ) 8 9 10 func ServerHandleError(err error,when string) { 11 if err != nil { 12 fmt.Println(err, "net.Listen") 13 os.Exit(1) 14 15 } 16 } 17 18 19 func main(){ 20 21 //解析得到UDP地址 22 udpAddr, e := net.ResolveUDPAddr("udp", "127.0.0.1:8080") 23 ServerHandleError(e,"net.ResolveUDPAddr") 24 25 //在UDP地址上建议UDP监听,得到链接 26 udpConn, err := net.ListenUDP("udp", udpAddr) 27 ServerHandleError(err,"net.ListenUDP") 28 29 //建议缓冲区 30 buffer := make([]byte, 1024) 31 32 //从广口连接中源源不断地读取, 来自任何客户端的数据包 33 for { 34 35 //读取一个数据包,丢入到buffer中,同时获得该数据包的客户段信息 36 n, remoteAddr, _ := udpConn.ReadFromUDP(buffer) 37 38 //打印数据包消息 39 clientMsg := string(buffer[:n]) 40 fmt.Printf("读到来自%v的内容: %s\n",remoteAddr,clientMsg) 41 42 //回复数据包的客户端 43 if clientMsg != "ino" { 44 45 udpConn.WriteToUDP([]byte("朕已阅:"+clientMsg), remoteAddr) 46 47 }else { 48 49 udpConn.WriteToUDP([]byte("去死吧:"+clientMsg), remoteAddr) 50 } 51 } 52 53 }
客户端
1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "net" 7 "bufio" 8 ) 9 10 11 12 13 func main() { 14 15 //拨号申请连接 16 conn, e := net.Dial("udp", "127.0.0.1:8080") 17 ClentHandleError(e,"net.Dial") 18 19 //创建命令行读取器 20 reader := bufio.NewReader(os.Stdin) 21 //创建缓冲区 22 buffer := make([]byte, 1024) 23 24 25 for { 26 27 linBytes, _, _ := reader.ReadLine() 28 conn.Write(linBytes) 29 30 //从连接中读入信息 31 n,_ := conn.Read(buffer) 32 serverMsg := string(buffer[:n]) 33 fmt.Println("服务器:",serverMsg) 34 35 if serverMsg == "ino" { 36 37 //发出死前的哀叹 38 conn.Write([]byte("啊啊啊啊....")) 39 break 40 } 41 42 } 43 44 fmt.Println("GAME OVER!") 45 } 46 47 48 func ClentHandleError(err error,when string) { 49 if err != nil { 50 fmt.Println(err, "net.Listen") 51 os.Exit(1) 52 53 } 54 }
2. 自动收发,一次性完事。
效果
server
clent
服务器端
1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "net" 7 ) 8 9 10 func ServerHandleError(err error,when string) { 11 if err != nil { 12 fmt.Println(err, "net.Listen") 13 os.Exit(1) 14 15 } 16 } 17 18 19 func main(){ 20 21 //解析得到UDP地址 22 udpAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:8080") 23 ServerHandleError(err,"net.ResolveUDPAddr") 24 25 //在UDP地址上建议UDP监听,得到链接 26 udpConn, err := net.ListenUDP("udp", udpAddr) 27 ServerHandleError(err,"net.ListenUDP") 28 defer udpConn.Close() 29 30 //建议缓冲区 31 buffer := make([]byte, 1024) 32 33 //从链接中读入内容,丢入缓冲区 34 n, remoteAddr, err := udpConn.ReadFromUDP(buffer) 35 ServerHandleError(err,"udpConn.ReadFromUDP(buffer)") 36 fmt.Printf("读到来自%v的内容: %s\n",remoteAddr,string(buffer[:n])) 37 38 //向客户端写出内容 39 udpConn.WriteToUDP([]byte("朕已阅!"),remoteAddr) 40 41 42 }
客户端
1 package main 2 3 import ( 4 "fmt" 5 "os" 6 "net" 7 ) 8 9 10 11 func ClentHandleError(err error,when string) { 12 if err != nil { 13 fmt.Println(err, "net.Listen") 14 os.Exit(1) 15 16 } 17 } 18 19 20 21 22 23 func main() { 24 25 //拨号申请连接 26 conn, e := net.Dial("udp", "127.0.0.1:8080") 27 ClentHandleError(e,"net.Dial") 28 defer conn.Close() 29 30 // 向连接中写出信息 31 n, err := conn.Write([]byte("只有main包下的main函数才能运行啊啊啊!")) 32 ClentHandleError(err,"conn.Write") 33 fmt.Printf("成功写出%d个字节\n",n) 34 35 //创建缓冲区 36 buffer := make([]byte, 1024) 37 38 //从连接中读入信息 39 n,err = conn.Read(buffer) 40 ClentHandleError(err,"conn.Read(buffer)") 41 fmt.Println(string(buffer[:n])) 42 43 fmt.Println("GAME OVER!") 44 }