Go语言网络通信---tcp群发消息
2021-02-10 04:17
标签:ack error accept cep highlight pac ror byte lis Go语言网络通信---tcp群发消息 标签:ack error accept cep highlight pac ror byte lis 原文地址:https://www.cnblogs.com/yunweiqiang/p/12745575.htmlserver
package main
import (
"fmt"
"net"
"os"
"time"
)
func SHandleError(err error, when string) {
if err != nil{
fmt.Println("err=", err, when)
os.Exit(1)
}
}
func main() {
listener, e := net.Listen("tcp", ":8080")
SHandleError(e,"net.Listen")
conns := make([]net.Conn, 0)
for len(conns)
client
package main
import (
"fmt"
"net"
"os"
)
func CHandleError(err error, when string) {
if err != nil{
fmt.Println("err=", err, when)
os.Exit(1)
}
}
func main() {
conn, e := net.Dial("tcp", "127.0.0.1:8080")
CHandleError(e, "net.Dial")
defer func() {
conn.Close()
fmt.Println("客户端已退出")
}()
buffer := make([]byte, 1024)
n, e := conn.Read(buffer)
CHandleError(e ,"conn.Read")
msg := buffer[:n]
fmt.Println(string(msg))
}
文章标题:Go语言网络通信---tcp群发消息
文章链接:http://soscw.com/index.php/essay/53400.html