• iOS  UDP 广播 AsyncSocket 用法


    因为业务需要,需要用广播发送一个字段,在iOS开发中,用到了AsynSocket。
    1.定义一个属性,负责发送和接受数据

    #define YX_Local_Host @"255.255.255.255"

    #define YX_Lockl_Port 48899

    @property(nonatomic,strong)AsyncUdpSocket *sendSocket;
     
    2.在viewDidload上初始化这个socket
    self.sendSocket=[[AsyncUdpSocket alloc]initWithDelegate:self];
    //需要有这个初始化,不知道为什么
     [self.sendSocket bindToAddress:@"0.0.0.0" port:5666 error:nil];
     [self.sendSocket receiveWithTimeout:-1 tag:0];
    3.在需要的时候出发它,我这里是在button点击事件出发的
    NSString *sendString=@"HF-A11ASSISTHREAD";
        NSData *data=[sendString dataUsingEncoding:NSUTF8StringEncoding];
        [self.sendSocket sendData:data toHost:YX_Local_Host port:YX_Lockl_Port withTimeout:30 tag:0];
        [self.sendSocket enableBroadcast:YES error:nil];
     
    4.发送出去以后,使用AsyncUdpSocket的代理,接受数据,并对需要的数据进行处理。
    -(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port{
      
        NSString *receiveData=[[NSString alloc ]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"-----receiveData----%@---------",receiveData);
        [self.sendSocket receiveWithTimeout:-1 tag:0];
        return YES;
    }

    - (void)onUdpSocket:(AsyncUdpSocket *)sock didNotReceiveDataWithTag:(long)tag dueToError:(NSError *)error{
        NSLog(@"没有收到");
    }
     
     
     
  • 相关阅读:
    关于Intent
    k8s常用命令
    kube-ui安装
    配置k8s dns
    centos 7 部署k8s集群
    多进程multiprocessing模块
    queue
    github安装k8s
    错误: No API token found for service account "default",
    线程
  • 原文地址:https://www.cnblogs.com/ljlkfx/p/4973438.html
Copyright © 2020-2023  润新知