package main
import (
"fmt"
"io"
_ "io"
"net"
"os"
)
func process(conn net.Conn) {
defer conn.Close()
for {
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil {
fmt.Printf("客户端退出 err = %v\n", err)
return
}
fmt.Print(string(buf[:n]))
}
}
func main() {
file, err := os.Open("conf.txt")
if err != nil {
panic(err.Error())
}
defer file.Close()
bytes, err := io.ReadAll(file)
if err != nil {
panic(err.Error())
}
address := string(bytes)
fmt.Println("服务端开始监听.." + address)
listen, err := net.Listen("tcp", address)
if err != nil {
fmt.Println("listen err = ", err)
return
}
defer listen.Close()
for {
fmt.Println("等待客户端来连接..")
conn, err := listen.Accept()
if err != nil {
fmt.Println("Accept() err = ", err)
} else {
fmt.Printf("Accept() suc con = %v 客户端ip = %v\n", conn, conn.RemoteAddr().String())
}
go process(conn)
}
}