zoukankan      html  css  js  c++  java
  • .cocoa AsyncSocket库

    官方网站:http://code.google.com/p/cocoaasyncsocket/

    这里只上例子,自己在项目中使用的,绝对能使用,如果有疑问,欢迎留言讨论。

    将AsyncUdpSocket.h AsyncUdpSocket.m 导入到你的工程中,

    在自己建立的util中,加入一下代码


    -(void)sendSearchBroadcast{
    NSString* bchost=@"255.255.255.255"; //这里发送广播
    [self sendToUDPServer:@"hello udp" address:bchost port:BCPORT];

    }


    -(void)sendToUDPServer:(NSString*) msg address:(NSString*)address port:(int)port{
    AsyncUdpSocket *udpSocket=[[[AsyncUdpSocket alloc]initWithDelegate:self]autorelease]; //得到udp util
    NSLog(@"address:%@,port:%d,msg:%@",address,port,msg);
    //receiveWithTimeout is necessary or you won't receive anything
    [udpSocket receiveWithTimeout:10 tag:2]; //设置超时10秒
    [udpSocket enableBroadcast:YES error:nil]; //如果你发送广播,这里必须先enableBroadcast
    NSData *data=[msg dataUsingEncoding:NSUTF8StringEncoding];
    [udpSocket sendData:data toHost:address port:port withTimeout:10 tag:1]; //发送udp

    }//下面是发送的相关回调函数

    -(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port{
    NSString* rData= [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]
    autorelease];
    NSLog(@"onUdpSocket:didReceiveData:---%@",rData);
    return YES;

    }

    -(void)onUdpSocket:(AsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error{
    NSLog(@"didNotSendDataWithTag----");

    }

    -(void)onUdpSocket:(AsyncUdpSocket *)sock didNotReceiveDataWithTag:(long)tag dueToError:(NSError *)error{
    NSLog(@"didNotReceiveDataWithTag----");
    }

    -(void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag{
    NSLog(@"didSendDataWithTag----");
    }

    -(void)onUdpSocketDidClose:(AsyncUdpSocket *)sock{
    NSLog(@"onUdpSocketDidClose----");
    }

  • 相关阅读:
    python接口自动化5-Json数据处理
    python接口自动化4-绕过验证码登录(cookie)
    python接口自动化2-发送post请求
    python接口自动化1-发送get请求
    python+selenium个人学习笔记11-登录封装与调用
    [jzoj]5257.小X的佛光
    [jzoj]1417.数学题
    2017.08.15【NOIP提高组】模拟赛B组
    [jzoj]1383.奇怪的问题
    [jzoj]1229.Hanoi
  • 原文地址:https://www.cnblogs.com/coderyangpeizhang/p/4874362.html
Copyright © 2011-2022 走看看