package main
import ( "fmt" "net" "strings" )
func main() {
listener, err := net.Listen("tcp", "127.0.0.1:8000") if err != nil { fmt.Println(err.Error()) return } defer listener.Close() //接收多个用户 for { conn, err := listener.Accept() if err != nil { fmt.Println(err.Error()) return } //处理用户的连接 go handleConn(conn)
} } func handleConn(conn net.Conn){
defer conn.Close() buff := make([]byte, 1024) for { n, err := conn.Read(buff) if err != nil { fmt.Println(err) return } content := string(buff[:n]) fmt.Println("content=", string(buff[:n])) content = strings.ToUpper(content) conn.Write([]byte(content)) } }
|
请发表评论