Socket - Object-c 第三方库(AsyncSocket)实现Socket
(2013-05-23 11:53:10)
标签:
object-csocketit |
分类: Mac/IOS那些事 |
下载 AsyncSocket
引入 AsyncSocket.h
AsyncSocket.h
|-- RootViewController.h
#import
#import "AsyncSocket.h"
@interface RootViewController : UIViewController
{
AsyncSocket
*_asySocket;
NSMutableData *_data;
}
@property (nonatomic,retain) AsyncSocket *asySocket;
@property (nonatomic,retain) NSMutableData *data;
@end
|-- RootViewController.m
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
-(void)loadView
{
[super
loadView];
self.view.backgroundColor = [UIColor orangeColor];
}
- (void)viewDidLoad
{
[super
viewDidLoad];
// Do any
additional setup after loading the view.
UIButton
*socketBtn = [[UIButton alloc]initWithFrame:CGRectMake(50, 50, 100,
40)];
[socketBtn
setTitle:@"socketTitle" forState:UIControlStateNormal];
[socketBtn
setBackgroundColor:[UIColor greenColor]];
[socketBtn
addTarget:self action:@selector(socketAction)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:socketBtn];
[socketBtn
release];
UITextField
*text = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200,
40)];
text.backgroundColor = [UIColor blueColor];
[text
addTarget:self action:@selector(textAction)
forControlEvents:UIControlEventEditingChanged];
[self.view
addSubview:text];
[text
release];
}
-(void)socketAction
{
_asySocket =
[[AsyncSocket alloc]initWithDelegate:self];
NSError
*error = [[NSError alloc]init];
if
(![_asySocket connectToHost:@"www.baidu.com" onPort:80
error:&error]) {
NSLog(@"error:%@",error);
}
//_data=[[NSMutableData dataWithLength:50] retain];
[_asySocket
readDataToLength:50 withTimeout:5 tag:1];
[_asySocket
readDataToLength:50 withTimeout:5 tag:2];
[_asySocket
writeData:[@"GET / HTTP/1.1\n\n"
dataUsingEncoding:NSUTF8StringEncoding] withTimeout:3 tag:1];
}
-(void)textAction
{
NSLog(@"textAction ...");
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose
of any resources that can be recreated.
}
#pragma mark -- AsyncSocket Delegate
//创建了新的Socket用于处理和客户端的请求,如果这个新socket实例你不打算保留(retain),
//那么将拒绝和该客户端连接
- (void)onSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket{
if
(!_asySocket) {
_asySocket=[newSocket retain];
NSLog(@"did accept new socket");
}
}
//提供线程的runloop实例给AsyncSocket,后者将使用这个runloop执行socket通讯的操作
- (NSRunLoop *)onSocket:(AsyncSocket *)sock wantsRunLoopForNewSocket:(AsyncSocket *)newSocket{
NSLog(@"wants runloop for new socket.");
return
[NSRunLoop currentRunLoop];
}
- (BOOL)onSocketWillConnect:(AsyncSocket *)sock{
NSLog(@"will
connect");
return
YES;
}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{
NSLog(@"did
connect to host");
[_asySocket
readDataWithTimeout:-1 tag:1];
}
//[_asySocket readDataToLength:50 withTimeout:5 tag:1];
//这句话主要是回调函数来读取数据
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
NSLog(@"did
read data");
NSString*
message = [[[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"message is: \n%@",message);
[_asySocket
writeData:data withTimeout:2 tag:1];
}
//[_asySocket writeData:[@"GET / HTTP/1.1\n\n" dataUsingEncoding:NSUTF8StringEncoding] withTimeout:3 tag:1];
//回调这个函数
- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag{
NSLog(@"message did write");
[_asySocket
readDataWithTimeout:-1 tag:1];
}
- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err{
NSLog(@"onSocket:%p willDisconnectWithError:%@", sock, err);
}
- (void)onSocketDidDisconnect:(AsyncSocket *)sock{
NSLog(@"socket did disconnect");
[_asySocket
release];
_asySocket=nil;
}
@end
引入 AsyncSocket.h
|-- RootViewController.h
#import
#import "AsyncSocket.h"
@interface RootViewController : UIViewController
{
}
@property (nonatomic,retain) AsyncSocket *asySocket;
@property (nonatomic,retain) NSMutableData *data;
@end
|-- RootViewController.m
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
-(void)loadView
{
}
- (void)viewDidLoad
{
}
-(void)socketAction
{
}
-(void)textAction
{
}
- (void)didReceiveMemoryWarning
{
}
#pragma mark -- AsyncSocket Delegate
//创建了新的Socket用于处理和客户端的请求,如果这个新socket实例你不打算保留(retain),
//那么将拒绝和该客户端连接
- (void)onSocket:(AsyncSocket *)sock didAcceptNewSocket:(AsyncSocket *)newSocket{
}
//提供线程的runloop实例给AsyncSocket,后者将使用这个runloop执行socket通讯的操作
- (NSRunLoop *)onSocket:(AsyncSocket *)sock wantsRunLoopForNewSocket
}
- (BOOL)onSocketWillConnect:(AsyncSocket *)sock{
}
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{
}
//[_asySocket readDataToLength:50 withTimeout:5 tag:1];
//这句话主要是回调函数来读取数据
- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{
}
//[_asySocket writeData:[@"GET / HTTP/1.1\n\n" dataUsingEncoding:NSUTF8StringEncoding] withTimeout:3 tag:1];
//回调这个函数
- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag{
}
- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err{
}
- (void)onSocketDidDisconnect:(AsyncSocket *)sock{
}
@end