Server 端
要点
1、net.Listen() 监听端口
2、listen.Accept() 间裂socket 连接
3、conn.Read() 循环读取数据
package main
import (
"fmt"
"net"
)
func process(conn net.Conn){
defer conn.Close()
for {
var buf [128]byte
n ,err := conn.Read(buf[:])
if err != nil {
fmt.Println("Read from tcp server failed,err:",err)
break
}
data := string(buf[:n])
fmt.Printf("Recived from client,data:%s\n",data)
}
}
func main() {
// 监听TCP 服务端口
listener,err := net.Listen("tcp","0.0.0.0:9090")
if err != nil {
fmt.Println("Listen tcp server failed,err:",err)
return
}
for{
// 建立socket连接
conn,err :&