In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "udp protocol and TCP communication configuration in go language", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "go language udp protocol and TCP communication how to configure" article.
Udp protocol server package mainimport ("fmt"net") func main () {/ / create listening socket, err: = net.ListenUDP ("udp4", & net.UDPAddr {IP: net.IPv4 (0,0,0,0), Port: 8080 }) if err! = nil {fmt.Println ("listening failed!", err) return} defer socket.Close () for {/ / read data from the client data: = make ([] byte, 4096) read, remoteAddr Err: = socket.ReadFromUDP (data) if err! = nil {fmt.Println ("failed to read data!", err) continue} fmt.Println (read, remoteAddr) fmt.Printf ("% s\ n\ n", data) / / send data Tell the client that it has received senddata: = [] byte ("hello client!") _, err = socket.WriteToUDP (senddata, remoteAddr) if err! = nil {return fmt.Println ("failed to send data!" Err)} client package mainimport ("fmt"net") func main () {/ / create connection socket, err: = net.DialUDP ("udp4", nil, & net.UDPAddr {IP: net.IPv4 (0,0,0,0), Port: 8080 }) if err! = nil {fmt.Println ("connection failed!", err) return} defer socket.Close () / / send data to the server senddata: = [] byte ("hello server!") _ Err = socket.Write (senddata) if err! = nil {fmt.Println ("failed to send data!", err) return} / / data from the receiving client data: = make ([] byte, 4096) read, remoteAddr Err: = socket.ReadFromUDP (data) if err! = nil {fmt.Println ("failed to read data!", err) return} fmt.Println (read, remoteAddr) fmt.Printf ("% s\ n" Data)} TCP Communications (basic) server package mainimport ("fmt"net") func main () {/ / listening listener, err: = net.Listen ("tcp", "127.0.0.1 package mainimport 8000") if err! = nil {fmt.Println ("err =" Err) return} defer listener.Close () / blocking waiting for user link conn, err: = listener.Accept () if err! = nil {fmt.Println ("err =", err) return} / / receive user's request buf: = make ([] byte 1024) / / 1024 buffer n, err1: = conn.Read (buf) if err1! = nil {fmt.Println ("err1 =", err1) return} fmt.Println ("buf =" String (buf [: n]) defer conn.Close () / close the current user link} client package mainimport ("fmt"net") func main () {/ / actively connect to the server conn, err: = net.Dial ("tcp", "127.0.0.1 defer conn.Close 8000") if err! = nil {fmt.Println ("err =" Err) return} defer conn.Close () / / send data conn.Write ([] byte ("are u ok?"))} TCP communication concurrent server server package mainimport ("fmt"net"strings") / / process user request func HandleConn (conn net.Conn) {/ / function call completed Automatically turn off conn defer conn.Close () / / to obtain the network address information of the client addr: = conn.RemoteAddr () .String () fmt.Println (addr, "conncet sucessful") buf: = make ([] byte, 2048) for {/ / read user data n Err: = conn.Read (buf) if err! = nil {fmt.Println ("err =", err) return} fmt.Printf ("[% s]:% s\ n", addr, string (buf [: n]) fmt.Println ("len =") Len (string (buf [: n])) / / if "exit" = = string (buf [: NMur1]) {/ / nc test if "exit" = = string (buf [: NMel 2]) {/ / client test written by yourself, when sending Two more characters, "\ r\ n" fmt.Println (addr, "exit") return} / / convert the data to uppercase Then send the user conn.Write ([] byte (strings.ToUpper (string (buf [: n]))} func main () {/ / listening listener, err: = net.Listen ("tcp", "127.0.0.1 if err 8000") if err! = nil {fmt.Println ("err =") Err) return} defer listener.Close () / / receive multiple users for {conn, err: = listener.Accept () if err! = nil {fmt.Println ("err =") Err) return} / / process user requests, create a new go HandleConn (conn)}} client package mainimport ("fmt"net"os") func main () {/ / actively connect to the server conn, err: = net.Dial ("tcp") "127.0.0.1 if err 8000") if err! = nil {fmt.Println ("net.Dial err =", err) return} / / main call completed Close the connection defer conn.Close () go func () {/ / enter the content from the keyboard and send the content to the server str: = make ([] byte, 1024) for {n, err: = os.Stdin.Read (str) / / read the content from the keyboard Put it on str if err! = nil {fmt.Println ("os.Stdin. Err = " Err) return} / / send the input to the server conn.Write (str [: n])}} () / / receive the data replied by the server / / slice Buffer buf: = make ([] byte) 1024) for {n, err: = conn.Read (buf) / / receive the request from the server if err! = nil {fmt.Println ("conn.Read err =" Err) return} fmt.Println (string (buf [: n])) / / print the received content, convert it to a string and then print} above is the content of this article on "udp protocol in go language and how to configure TCP communication" I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.