swift -> AsyncSocket establishes a UDP socket connection

Download location: https://github.com/robbiehanson/CocoaAsyncSocket

Use  AsyncUdpSocket.m and AsyncUdpSocket.h

Because the project is a  Swift  project, to use the objecitive-c library should be imported in the Header file.

1. Copy  AsyncUdpSocket.m and AsyncUdpSocket.h  to the project, xcode will ask you whether to create a Bridge file, confirm to create.

2. In  your project name-Bridging-Header.h  file add:

1

#import "AsyncUdpSocket.h"

 

ViewControler.swift

//
//  ViewController.swift
//  udp_test
//
//  Created by  on 16/5/9.
//  Copyright © 2016年. All rights reserved.
//

import UIKit

class ViewController: UIViewController,AsyncUdpSocketDelegate{
    var udpClient = AsyncUdpSocket ();
    override func viewDidLoad() {
        super.viewDidLoad();
                // Do any additional setup after loading the view, typically from a nib.
    }
    // send manually
    @IBAction func btn_send(sender: UIButton) {
        let data = "Hi, im wang".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true);
        udpClient.sendData(data, toHost: "192.168.1.102", port: 60002, withTimeout: -1, tag: 0);
        //Start listening for reception, and automatically disconnect if there is no message reply within 10 seconds, only receive once,//If the timeout is set to -1, there is no timeout limit
        udpClient.receiveWithTimeout(10, tag: 0);
        
    }
    // Manually close the connection
    @IBAction func close(sender: UIButton) {
        udpClient.close();
    }
    // manual connection
    @IBAction func btn_conn(sender: UIButton) {
        udpClient = AsyncUdpSocket(delegate: self);
    }
    //If the local send is successful, listen
    func onUdpSocket(sock: AsyncUdpSocket!, didSendDataWithTag tag: Int) {
        print("send success");
    }
    //listen to receive
    func onUdpSocket(sock: AsyncUdpSocket!, didReceiveData data: NSData!, withTag tag: Int, fromHost host: String!, port: UInt16) -> Bool {
        let str = NSString(data: data, encoding: NSUTF8StringEncoding);
        print("receive:\(str)");
        //If you can't receive it all at once, there are more
        udpClient.receiveWithTimeout(10, tag: 0);
        return true;
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
}

 

 

 

 

//

import UIKit

class ViewController: UIViewController,AsyncUdpSocketDelegate{
    var udpClient = AsyncUdpSocket ();
    override func viewDidLoad() {
        super.viewDidLoad();
                // Do any additional setup after loading the view, typically from a nib.
    }
    // send manually
    @IBAction func btn_send(sender: UIButton) {
        let data = "Hi, im wang".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true);
        udpClient.sendData(data, toHost: "192.168.1.102", port: 60002, withTimeout: -1, tag: 0);
        //开始监听接收,并如10秒内无消息回复就自动断开连接,只接收一次,//如果设置超时为-1 则没有超时限制
        udpClient.receiveWithTimeout(-1, tag: 0);
        
    }
    //手动关闭连接
    @IBAction func close(sender: UIButton) {
        udpClient.close();
    }
    //手动连接
    @IBAction func btn_conn(sender: UIButton) {
        udpClient = AsyncUdpSocket(delegate: self);
    }
    //如果本地发送成功 监听
    func onUdpSocket(sock: AsyncUdpSocket!, didSendDataWithTag tag: Int) {
        print("send success");
    }
    //监听接收
    func onUdpSocket(sock: AsyncUdpSocket!, didReceiveData data: NSData!, withTag tag: Int, fromHost host: String!, port: UInt16) -> Bool {
        let str = NSString(data: data, encoding: NSUTF8StringEncoding);
        print("receive:\(str)");
        //If you can't receive it all at once, there are more
        udpClient.receiveWithTimeout(-1, tag: 0);
        return true;
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
}

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326850025&siteId=291194637