go socket 服务端处理多用户

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))
}
}
posted on 2019-10-02 15:00  paulversion  阅读(475)  评论(0编辑  收藏  举报