GoLang补充提升(八)
TCP通信
1.TCP服务端
func main() {
tcpAddr, err := net.ResolveTCPAddr("tcp4", "127.0.0.1:8986")
if err != nil {
fmt.Println(err)
return
}
listener, err := net.ListenTCP("tcp", tcpAddr)
if err != nil {
fmt.Println(err)
return
}
for {
conn, err := listener.AcceptTCP()
if err != nil {
fmt.Println(err)
return
}
fmt.Println(conn.RemoteAddr(), "链接进入服务器")
go process(conn)
}
}
func process(conn net.Conn) {
defer conn.Close()
for {
reader := bufio.NewReader(conn)
buffer := make([]byte, 1024)
n, err := reader.Read(buffer)
if err != nil {
fmt.Println(err)
return
}
recStr := string(buffer[:n])
fmt.Print("服务器接收客户端消息:", recStr)
_, err = conn.Write(buffer[:n])
if err != nil {
fmt.Println(err)
return
}
}
}
2.TCP客户端
func main() {
laddr, err := net.ResolveTCPAddr("tcp4", "127.0.0.1:8985")
if err != nil {
fmt.Println(err)
return
}
raddr, err := net.ResolveTCPAddr("tcp4", "127.0.0.1:8986")
if err != nil {
fmt.Println(err)
return
}
conn, err := net.DialTCP("tcp4", laddr, raddr)
if err != nil {
fmt.Println(err)
return
}
defer conn.Close()
inputReader := bufio.NewReader(os.Stdin)
go func() {
for {
reader := bufio.NewReader(conn)
buffer := make([]byte, 1024)
n, err := reader.Read(buffer)
if err != nil {
fmt.Println(err)
return
}
fmt.Print("客户端接收到了服务器返回的数据:", string(buffer[:n]))
}
}()
for {
info, err := inputReader.ReadString('\n')
if err != nil {
fmt.Println(err)
return
}
_, err = conn.Write([]byte(info))
if err != nil {
fmt.Println(err)
return
}
}
}
3.黏包解决
func process(conn net.Conn) {
defer conn.Close()
var buffer = make([]byte, 1024)
for {
n, err := conn.Read(buffer)
if err == io.EOF {
fmt.Println(err)
continue
}
if err != nil {
fmt.Println(err)
return
}
reader := bytes.NewReader(buffer[:n])
var off int = 0
for {
var length int32
err = binary.Read(reader, binary.LittleEndian, &length)
if err != nil {
fmt.Println(err)
return
}
if int32(n-off-4) < length {
fmt.Println("长度不够")
return
}
_, err = reader.Seek(int64(length), 1)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(buffer[off+4 : off+4+int(length)]))
off += int(4 + length)
if off >= n {
break
}
}
}
}
func main() {
listen, err := net.Listen("tcp", "127.0.0.1:8081")
if err != nil {
fmt.Println(err)
return
}
defer listen.Close()
for {
conn, err := listen.Accept()
if err != nil {
fmt.Println(err)
return
}
go process(conn)
}
}
UDP通信
1.UDP服务端
func main() {
conn, err := net.ListenUDP("udp", &net.UDPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: 8079,
})
if err != nil {
fmt.Println(err)
return
}
defer conn.Close()
for {
var data = make([]byte, 1024)
_, addr, err := conn.ReadFromUDP(data)
if err != nil {
fmt.Println(err)
continue
}
fmt.Println(string(data))
_, err = conn.WriteToUDP(data, addr)
if err != nil {
fmt.Println(err)
continue
}
}
}
2.UDP客户端
func main() {
conn, err := net.DialUDP("udp", nil, &net.UDPAddr{
IP: net.IPv4(127, 0, 0, 1),
Port: 8079,
})
if err != nil {
fmt.Println(err)
return
}
defer conn.Close()
sendData := []byte("Hello Server")
_, err = conn.Write(sendData)
if err != nil {
fmt.Println(err)
return
}
buffer := make([]byte, 1024)
_, addr, err := conn.ReadFromUDP(buffer)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(addr, string(buffer))
}