Swift -> YSocket 新建 TCP 客户端

 
//

import UIKit

class ViewController: UIViewController {
    
    var client:TCPClient = TCPClient(addr: "192.168.43.119", port: 7412)

    
    @IBAction func btn_conn(sender: UIButton) {
        //连接
        var (success,errmsg)=client.connect(timeout: 1)
        if success{
            print("conn success");
            let data=client.read(1024*10);
            if let d=data{
                if let str=String(bytes: d, encoding: NSUTF8StringEncoding){
                    print(str)
                }
            }
        }else{
            print("error:\(errmsg)");
        }
    }
 

    
    @IBAction func btn_disconn(sender: UIButton) {
        client.close();
        print("dis conn");
    }
    
    @IBAction func btn_send(sender: UIButton) {
        client.send(str:"GET / HTTP/1.0\n\n");
        let data=client.read(1024*10);
        if let d=data{
            if let str=String(bytes: d, encoding: NSUTF8StringEncoding){
                print(str)
            }
        }
        
        
      

    }
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

将附件类文件和.h 拖入项目

如果 出现 类文件编译错误,可能是 xcode 版本问题, xcode 7.3.1 经测没问题

猜你喜欢

转载自mft.iteye.com/blog/2297851
今日推荐