TCP协议
TCP/IP(Transmission Control Protocol/Internet Protocol) 即传输控制协议/网际协议,是一种面向连接(连接导向)的、可靠的、基于字节流的传输层(Transport layer)通信协议,因为是面向连接的协议,数据像水流一样传输,会存在黏包问题。 服务端
package main
import (
"bufio"
"fmt"
"net"
)
func process(conn net.Conn) {
defer conn.Close()
for {
reader := bufio.NewReader(conn)
var buf [128]byte
n, err := reader.Read(buf[:])
if err != nil {
fmt.Println("read from client failed, err:", err)
break
}
recvStr := string(buf[:n])
fmt.Println("收到client端发来的数据:", recvStr)
conn.Write([]byte(recvStr))
}
}
func main() {
listen, err := net.Listen("tcp", "127.0.0.1:8080")
if err != nil {
fmt.Println("listen failed, err:", err)
return
}
for {
conn, err := listen.Accept()
if err != nil {
fmt.Println("accept failed, err:", err)
continue
}
go process(conn)
}
}
客户端
package main
import (
"bufio"
"fmt"
"net"
"os"
"strings"
)
func main() {
conn, err := net.Dial("tcp", "127.0.0.1:8080")
if err != nil {
fmt.Println(err)
}
defer conn.Close()
inputReader := bufio.NewReader(os.Stdin)
for {
input, _ := inputReader.ReadString('\n')
inputInfo := strings.Trim(input, "\r\n")
if strings.ToUpper(inputInfo) == "Q" {
return
}
_, err := conn.Write([]byte(inputInfo))
if err != nil {
return
}
buf := [512]byte{}
read, err := conn.Read(buf[:])
if err != nil {
return
}
fmt.Println("接收数据: ", string(buf[:read]))
}
}
UDP协议
UDP协议(User Datagram Protocol)中文名称是用户数据报协议,是OSI(Open System Interconnection,开放式系统互联)参考模型中一种无连接的传输层协议,不需要建立连接就能直接进行数据发送和接收,属于不可靠的、没有时序的通信,但是UDP协议的实时性比较好,通常用于视频直播相关领域。
服务端
package main
import (
"fmt"
"net"
)
func main() {
updAddr := net.UDPAddr{
IP: net.IPv4(0, 0, 0, 0),
Port: 8080,
}
fmt.Printf("UDP server running... IP: %v Port: %v \n", updAddr.IP, updAddr.Port)
listen, err := net.ListenUDP("udp", &updAddr)
if err != nil {
fmt.Println("listen failed err: ", err)
return
}
defer func(listen *net.UDPConn) {
err := listen.Close()
if err != nil {
fmt.Println("listen failed err: ", err)
}
}(listen)
for {
var data [1024]byte
udp, addr, err := listen.ReadFromUDP(data[:])
if err != nil {
fmt.Println("listen failed err: ", err)
continue
}
fmt.Printf("data: %v add: %v count: %v \n\n", string(data[:udp]), addr, udp)
_, err = listen.WriteToUDP(data[:udp], addr)
if err != nil {
fmt.Println("listen failed err: ", err)
continue
}
}
}
客户端
package main
import (
"fmt"
"net"
)
func main() {
udpAddr := net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 8080}
udpConn, err := net.DialUDP("udp", nil, &udpAddr)
if err != nil {
fmt.Printf("连接服务端失败: %s \n", err)
return
}
defer udpConn.Close()
sendData := []byte("hello udp server")
_, err = udpConn.Write(sendData)
if err != nil {
fmt.Printf("发送数据失败: %s \n", err)
return
}
readData := [1024]byte{}
udp, addr, err := udpConn.ReadFromUDP(readData[:])
if err != nil {
fmt.Printf("接收数据失败: %s \n", err)
return
}
fmt.Printf("recv: %v addr: %v count: %v \n", string(readData[:udp]), addr, udp)
}
|