示例#1
0
文件: util.go 项目: sunvim/util
func Recover(desc string) {
	if e := recover(); e != nil {
		buf := make([]byte, 4096)
		i := runtime.Stack(buf, false)
		buf = buf[:i]
		log.Error("desc:", desc, "error out of think=>", e)
		log.Error("Stack:", string(buf))
	}
}
示例#2
0
func Server(listen *net.TCPListener) {
	for {
		conn, err := listen.AcceptTCP()
		if err != nil {
			log.Error("接受客户端连接异常:", err.Error())
			continue
		}
		fmt.Println("客户端连接来自:", conn.RemoteAddr().String())
		defer conn.Close()
		go func() {
			data := make([]byte, 128)
			for {
				i, err := conn.Read(data)
				if err != nil {
					fmt.Println("读取客户端数据错误:", err.Error())
					break
				}
				log.Info("客户端发来数据:", string(data[0:i]))
				conn.Write([]byte{'S', 'a', 'n', 'd', 'L', 'a', 'b'})
			}
		}()
	}
}