ios开发那些事 Objective - C 开发那些事

Swift-Socket实现2台手机间数据传输

2019-05-25  本文已影响33人  SK丿希望

使用注意
因为使用Socket是需要网络授权的,这里的Dome没有做判断操作,如果需要测试可以点击客户端中,连接按钮进行App授权后再进行正常操作

UI客户端

image.png

UI服务器端

image.png

实现

服务器端

class GCDSocketServerViewController: UIViewController {
    @IBOutlet weak var portTF: UITextField!
    @IBOutlet weak var msgTF: UITextField!
    @IBOutlet weak var logTV: UITextView!
    @IBOutlet weak var ipLabel: UILabel!
    /// 服务器
    private var serverSocket : GCDAsyncSocket?
    /// 客户端
    private var clientSocket : GCDAsyncSocket?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let ip = HWWIFITools.hw_getCurrentIP() ?? ""
        let name = HWWIFITools.hw_getCurreWiFiSsid() ?? ""
        ipLabel.text = "WIFI名:\(name) ip:\(ip)"
        HWPrint(name + ip)
        setupServerSocket()
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    // MARK: - 创建服务器Socket
    func setupServerSocket() {
        self.serverSocket = GCDAsyncSocket(delegate: self, delegateQueue: DispatchQueue.main)
    }
    
    func showLogMsg(_ log:String) {
        logTV.text = logTV.text + "\n" + log
    }
    // MARK: - 监听按钮点击
    @IBAction func listeningBtnClick(_ sender: Any) {
        do {
            try self.serverSocket?.accept(onPort: UInt16(self.portTF.text ?? "")!)
        }
        catch {
            HWPrint("监听出错")
            return
        }
        showLogMsg("正在监听...")
    }
    
    @IBAction func sendBtnClick(_ sender: Any) {
        if let data = msgTF.text?.data(using: String.Encoding.utf8) {
            showLogMsg(String(format: "你: %@", msgTF.text ?? ""))
            self.clientSocket?.write(data, withTimeout: -1, tag: 0)
            msgTF.text = ""
        } else {
            HWPrint("发送失败")
        }
    }
}

extension GCDSocketServerViewController : GCDAsyncSocketDelegate {
    //接收到请求
    func socket(_ sock: GCDAsyncSocket, didAcceptNewSocket newSocket: GCDAsyncSocket) {
        showLogMsg("收到客服点连接.....")
        let ip = newSocket.connectedHost ?? ""
        let port = newSocket.connectedPort
        let log = String(format: "客户端地址:%@ 端口:%d", ip, port)
        showLogMsg(log)
        self.clientSocket = newSocket
        newSocket.readData(withTimeout: -1, tag: 0)
        if let data = "你连接成功了,大兄弟".data(using: String.Encoding.utf8) {
            self.clientSocket?.write(data, withTimeout: -1, tag: 0)
            showLogMsg("你连接成功了,大兄弟")
        }
    }
    // 收到数据
    func socket(_ sock: GCDAsyncSocket, didRead data: Data, withTag tag: Int) {
        let strMsg = String(bytes: data, encoding: String.Encoding.utf8)
        let log = String(format: "客户端: %@", strMsg ?? "")
        showLogMsg(log)
        sock.readData(withTimeout: -1, tag: 0)
    } 
}

客户端

class GCDSocketClientViewController: UIViewController {

    @IBOutlet weak var ipTF: UITextField!
    @IBOutlet weak var portTF: UITextField!
    @IBOutlet weak var msgTF: UITextField!
    @IBOutlet weak var logTV: UITextView!
    private var socket : GCDAsyncSocket?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupClientSocket()
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    // MARK: - 创建客户端Socket
    func setupClientSocket() {
        //在主队列中处理,  所有的回执都在主队列中执行。
        self.socket = GCDAsyncSocket(delegate: self, delegateQueue: DispatchQueue.main)
    }
    func showLogMsg(_ log:String) {
        logTV.text = logTV.text + "\n" + log
    }
    // MARK: - 点击了连接
    @IBAction func connectBtnClick(_ sender: Any) {
        if self.socket == nil {
            setupClientSocket()
        }
        do {
            if self.socket?.isConnected == false {
                try self.socket?.connect(toHost: ipTF.text ?? "", onPort: UInt16(portTF.text ?? "0")!)
            }
        }
        catch {
            showLogMsg("连接失败...")
            return
        }
        showLogMsg("连接成功")
    }
    @IBAction func sendBtnClick(_ sender: Any) {
        if let data = msgTF.text?.data(using: String.Encoding.utf8) {
            showLogMsg(String(format: "你: %@", msgTF.text ?? ""))
            self.socket?.write(data, withTimeout: 30, tag: 100)
            msgTF.text = ""
        } else {
            HWPrint("发送失败")
        }
    }
    func sendMsg(_ msg: String?) {
        if let data = msg?.data(using: String.Encoding.utf8) {
            showLogMsg(String(format: "你: %@", msgTF.text ?? ""))
            self.socket?.write(data, withTimeout: 30, tag: 100)
            msgTF.text = ""
        } else {
            HWPrint("发送失败")
        }
    }
}

extension GCDSocketClientViewController : GCDAsyncSocketDelegate {
   // 连接成功
    func socket(_ sock: GCDAsyncSocket, didConnectToHost host: String, port: UInt16) {
        let strMsg = "我是客户端 连接你来了"
        sendMsg(strMsg)
        self.socket?.readData(withTimeout: -1, tag: 100)
    }
    func socketDidDisconnect(_ sock: GCDAsyncSocket, withError err: Error?) {
        showLogMsg("socket断开连接...")
    }
    // 接收到数据
    func socket(_ sock: GCDAsyncSocket, didRead data: Data, withTag tag: Int) {
        let strMsg = String(bytes: data, encoding: String.Encoding.utf8)
        let log = String(format: "服务器: %@", strMsg ?? "")
        showLogMsg(log)
        sock.readData(withTimeout: -1, tag: 100)
    }
}

Dome

上一篇下一篇

猜你喜欢

热点阅读