码迷,mamicode.com
首页 > 移动开发 > 详细

IOS AsyncSocket

时间:2014-06-09 00:07:26      阅读:337      评论:0      收藏:0      [点我收藏+]

标签:c   class   a   http   int   get   

导入AsyncSocket.h  AsyncSocket.m   AsyncUdpSocket.h   AsyncUdpSocket.m   以及  CFNetWork.framework   

 

 

asyncSocket = [[AsyncSocketalloc] initWithDelegate:self];

    NSError *err = nil;

    if(![asyncSocketconnectToHost:@"www.baidu.com"onPort:80error:&err])

    {

        NSLog(@"Error: %@", err);

    }

    

    

    [asyncSocketwriteData:[@"GET / HTTP/1.1\n\n"dataUsingEncoding:NSUTF8StringEncoding] withTimeout:3tag:1];

 

 

 

 

 

 

//建立连接

-(void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port

{

    NSLog(@"onScoket:%p did connecte to host:%@ on port:%d",sock,host,port);

    [sock readDataWithTimeout:1tag:0];

}

 

//读取数据

-(void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag

{

    NSString *aStr=[[NSStringalloc] initWithData:data encoding:NSUTF8StringEncoding];

    NSLog(@"aStr==%@",aStr);

    

    

    NSData *aData=[@"Hi there"dataUsingEncoding:NSUTF8StringEncoding];

    [sock writeData:aData withTimeout:-1 tag:1];

    [sock readDataWithTimeout:1tag:0];

}

 

//是否加密

-(void)onSocketDidSecure:(AsyncSocket *)sock

{

    NSLog(@"onSocket:%p did go a secure line:YES",sock);

}

 

//遇到错误时关闭连接

-(void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err

{

    NSLog(@"onSocket:%p will disconnect with error:%@",sock,err);

}

 

//断开连接

-(void)onSocketDidDisconnect:(AsyncSocket *)sock

{

    NSLog(@"onSocketDidDisconnect:%p",sock);

}

 

IOS AsyncSocket,布布扣,bubuko.com

IOS AsyncSocket

标签:c   class   a   http   int   get   

原文地址:http://www.cnblogs.com/rollrock/p/3775361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!