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

使用注意
  • 因为使用Socket是需要网络授权的,这里的Dome没有做判断操作,如果需要测试可以点击客户端中,连接按钮进行App授权后再进行正常操作
  • 在info文件中添加App Transport Security Settings->Allow Arbitrary Loads=yes
Swift-Socket实现2台手机间数据传输_第1张图片
网络权限
  • 适配系统最好把WIFI权限开启一下
Swift-Socket实现2台手机间数据传输_第2张图片
WIFI权限开启

UI客户端

Swift-Socket实现2台手机间数据传输_第3张图片
image.png

UI服务器端

Swift-Socket实现2台手机间数据传输_第4张图片
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, 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, 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

数据传输思路

WIFI工具可以获取当前ip/WIFI名/热点IP/路由IP

  1. 由于在客户端是无法获取服务器端IP的,故IP必须又服务器端提供,下面提供简单思路(注意:如果是热点传输获取IP是获取热点的IP)
  • 服务器端提供二维码(IP\端口号\WIFI名等等信息),客户端可以通过扫码获取对应信息

2.传输数据注意点(主要是粘包/残包处理)

  • 针对数据传输我当时做的比较简单,就是数据前8位给出body的数据类型数据长度每次拿到数据先校验数据长度是否满足
    a.满足就是正常包
    b.过短(残包),前8位中取出后面的数据长度,拼接下次数据,裁剪拿到正常数据
    c.过长(粘包),将数据进行裁剪,剩余部分记录到残包中去执行b

你可能感兴趣的:(Swift-Socket实现2台手机间数据传输)