SwiftSocket 接收服务端byte[] 本地转换

前端之家收集整理的这篇文章主要介绍了SwiftSocket 接收服务端byte[] 本地转换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
let client:TCPClient  = TCPClient(addr:"192.168.0.124",port:10001);
        let (success,errmsg) = client.connect(timeout: 10);
        if(success){
           
            let pack:Pack = Pack(id: 100)
            pack.put(101,v: "101")
            pack.put(102,v: "Hello")
            pack.put(103,v: "30")
            
            let (success,errmsg) = client.send(data: pack.toNSData())
            if(success){
                
                let result:[UInt8] = client.read(1024*100)!
                
                if let str = NSString(bytes: result,length: result.count,encoding: NSUTF8StringEncoding) as? String {
                    print(str)
                } else {
                    print("not a valid UTF-8 sequence")
                }
                
                let data:NSData = NSData(bytes: result,length: result.count)
                let str:String = String(data: data,encoding: NSUTF8StringEncoding)!
                print(str)
                
            }else{
                
                print(errmsg)
            }
            
        }else{
            print(errmsg)
        }

猜你在找的Swift相关文章