码迷,mamicode.com
首页 > 其他好文 > 详细

CocoaAsyncSocketDemo

时间:2015-01-12 12:59:31      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

工程图:

技术分享

 

代码:

viewController.h

#import <UIKit/UIKit.h>
//加入头文件
#import "AsyncSocket.h"
@interface ViewController : UIViewController<AsyncSocketDelegate>
@end

 

viewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    AsyncSocket *socket=[[AsyncSocket alloc] initWithDelegate:self];
    [socket connectToHost:@"www.baidu.com" onPort:80 error:nil];
    
    [socket readDataWithTimeout:3 tag:1];
    [socket writeData:[@"GET / HTTP/1.1nn" dataUsingEncoding:NSUTF8StringEncoding] withTimeout:3 tag:1];
    
}

#pragma -mark -socket代理
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{
    NSLog(@"did connect to host");
}
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
    
    NSLog(@"did read data");
    NSString* message = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"message is: n%@",message);
}

 

输出结果:

2015-01-12 11:25:05.460 CocoaAsyncSocketDemo[2001:127210] did connect to host

2015-01-12 11:25:42.699 CocoaAsyncSocketDemo[2001:127988] Terminating since there is no system app.

 

CocoaAsyncSocketDemo

标签:

原文地址:http://www.cnblogs.com/yang-guang-girl/p/4217996.html

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