package tcp import ( "errors" "net" "server/dao/devices" "server/utils/logger" "strings" ) var ScreenList []devices.Screens func ListenTcp() { // 监听当前的tcp连接 listen, err := net.Listen("tcp", "0.0.0.0:9200") if err != nil { logger.Logger.Errorf("listen failed, err:%v", err) return } for { conn, err := listen.Accept() // 建立连接 if err != nil { logger.Logger.Errorf("Accept failed, err:%v", err) continue } err = CheckConn(conn) if err != nil { continue } } } func CheckConn(conn net.Conn) error { logger.Logger.Debugf("StartDevice addr:%s", conn.RemoteAddr().String()) arr := strings.Split(conn.RemoteAddr().String(), ":") ip := arr[0] ScreenList = devices.QueryAllScreens() for _, v := range ScreenList { if v.IPAddress == ip { dev := Device{info: v} dev.Start(conn) return nil } } return errors.New("not found") }