iOS Socket第三方开源类库 AsyncSocket

假如你也是一个java程序员,而你又不是很懂Socket。

下面我的这篇文章也许能帮助你一些。

http://xiva.iteye.com/blog/993336


首先我们写好上面文章中的server端。

下面我们可以访问一下下面的地址:


http://code.google.com/p/cocoaasyncsocket/

这是一个开源框架。呵,不知道拿到自己程序中使用是否涉及侵权。

但是这句话“The CocoaAsyncSocket project is in the public domain.”是我有信心使用它们的源码,否则只能自己用c来写了,或者使用CFSocket、CFNetwork等类自己来写了。不过也无妨,应在在使用线程的情况下,我们也是可以实现的。


总之,为了开发的便捷,我使用了AsyncSocket这个类,这样可以异步通信。


建立一个基于视图的应用程序,按照http://code.google.com/p/cocoaasyncsocket/wiki/Reference_AsyncSocket

我们AsyncSocket.h和AsyncSocket.m到我们的项目中,并且导入CFNetwork.framework。这样基本准备工作就做好了。

 

下面提供我的应用中的代码以及界面图:

 

Socketdemoviewcontroller.h代码   收藏代码
  1. //  
  2. //  SocketDemoViewController.h  
  3. //  SocketDemo  
  4. //  
  5. //  Created by xiang xiva on 10-7-10.  
  6. //  Copyright 2010 __MyCompanyName__. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10. #import "AsyncSocket.h"  
  11. #define SRV_CONNECTED 0  
  12. #define SRV_CONNECT_SUC 1  
  13. #define SRV_CONNECT_FAIL 2  
  14. #define HOST_IP @"192.168.110.1"  
  15. #define HOST_PORT 8080  
  16.   
  17. @interface SocketDemoViewController : UIViewController {  
  18.       
  19.     UITextField *inputMsg;  
  20.     UILabel *outputMsg;  
  21.     AsyncSocket *client;  
  22. }  
  23.   
  24. @property (nonatomic, retain) AsyncSocket *client;  
  25. @property (nonatomic, retain) IBOutlet UITextField *inputMsg;  
  26. @property (nonatomic, retain) IBOutlet UILabel *outputMsg;  
  27.   
  28. - (int) connectServer: (NSString *) hostIP port:(int) hostPort;  
  29. - (void) showMessage:(NSString *) msg;  
  30. - (IBAction) sendMsg;  
  31. - (IBAction) reConnect;  
  32. - (IBAction) textFieldDoneEditing:(id)sender;  
  33. - (IBAction) backgroundTouch:(id)sender;  
  34.   
  35. @end  
 

 

 

socketdemoviewcontroller.m代码   收藏代码
  1. //  
  2. //  SocketDemoViewController.m  
  3. //  SocketDemo  
  4. //  
  5. //  Created by xiang xiva on 10-7-10.  
  6. //  Copyright 2010 __MyCompanyName__. All rights reserved.  
  7. //  
  8.   
  9. #import "SocketDemoViewController.h"  
  10.   
  11. @implementation SocketDemoViewController  
  12.   
  13. @synthesize inputMsg, outputMsg;  
  14. @synthesize client;  
  15. /*  
  16. // The designated initializer. Override to perform setup that is required before the view is loaded.  
  17. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {  
  18.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  19.     if (self) {  
  20.         // Custom initialization  
  21.     }  
  22.     return self;  
  23. }  
  24. */  
  25.   
  26. /*  
  27. // Implement loadView to create a view hierarchy programmatically, without using a nib.  
  28. - (void)loadView {  
  29. }  
  30. */  
  31.   
  32.   
  33.   
  34. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.  
  35. - (void)viewDidLoad {  
  36.     //[super viewDidLoad];  
  37.     [self connectServer:HOST_IP port:HOST_PORT];  
  38.     //监听读取  
  39.       
  40. }  
  41.   
  42.   
  43.   
  44. // Override to allow orientations other than the default portrait orientation.  
  45. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {  
  46.     return YES;  
  47. }  
  48.   
  49. - (void)didReceiveMemoryWarning {  
  50.     // Releases the view if it doesn't have a superview.  
  51.     [super didReceiveMemoryWarning];  
  52.       
  53.     // Release any cached data, images, etc that aren't in use.  
  54. }  
  55.   
  56. - (void)viewDidUnload {  
  57.     self.client = nil;  
  58.     // Release any retained subviews of the main view.  
  59.     // e.g. self.myOutlet = nil;  
  60. }  
  61.   
  62. - (int) connectServer: (NSString *) hostIP port:(int) hostPort{  
  63.       
  64.     if (client == nil) {  
  65.         client = [[AsyncSocket alloc] initWithDelegate:self];  
  66.         NSError *err = nil;  
  67.         //192.168.110.128  
  68.         if (![client connectToHost:hostIP onPort:hostPort error:&err]) {  
  69.             NSLog(@"%@ %@", [err code], [err localizedDescription]);  
  70.               
  71.             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[@"Connection failed to host "   
  72.                                             stringByAppendingString:hostIP]   
  73.                                                             message:[[[NSString alloc]initWithFormat:@"%@",[err code]] stringByAppendingString:[err localizedDescription]]   
  74.                                                            delegate:self   
  75.                                                   cancelButtonTitle:@"OK"  
  76.                                                   otherButtonTitles:nil];  
  77.             [alert show];  
  78.             [alert release];  
  79.             //client = nil;  
  80.             return SRV_CONNECT_FAIL;  
  81.         } else {  
  82.             NSLog(@"Conectou!");  
  83.             return SRV_CONNECT_SUC;  
  84.         }  
  85.     }  
  86.     else {  
  87.         [client readDataWithTimeout:-1 tag:0];  
  88.         return SRV_CONNECTED;  
  89.     }  
  90.       
  91. }  
  92.   
  93. - (IBAction) reConnect{  
  94.     int stat = [self connectServer:HOST_IP port:HOST_PORT];  
  95.     switch (stat) {  
  96.         case SRV_CONNECT_SUC:  
  97.             [self showMessage:@"connect success"];  
  98.             break;  
  99.         case SRV_CONNECTED:  
  100.             [self showMessage:@"It's connected,don't agian"];  
  101.             break;  
  102.         default:  
  103.             break;  
  104.     }  
  105. }  
  106.   
  107. - (IBAction) sendMsg{  
  108.       
  109.     NSString *inputMsgStr = self.inputMsg.text;  
  110.     NSString * content = [inputMsgStr stringByAppendingString:@"\r\n"];  
  111.     NSLog(@"%a",content);  
  112.     NSData *data = [content dataUsingEncoding:NSISOLatin1StringEncoding];  
  113.     [client writeData:data withTimeout:-1 tag:0];  
  114.       
  115.     //[data release];  
  116.     //[content release];  
  117.     //[inputMsgStr release];  
  118.     //继续监听读取  
  119.     //[client readDataWithTimeout:-1 tag:0];  
  120. }  
  121.   
  122. #pragma mark -  
  123. #pragma mark close Keyboard  
  124. - (IBAction) textFieldDoneEditing:(id)sender{  
  125.     [sender resignFirstResponder];  
  126. }  
  127.   
  128. - (IBAction) backgroundTouch:(id)sender{  
  129.     [inputMsg resignFirstResponder];  
  130. }  
  131.   
  132. #pragma mark socket uitl  
  133.   
  134. - (void) showMessage:(NSString *) msg{  
  135.     UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"Alert!"  
  136.                                                     message:msg  
  137.                                                    delegate:nil  
  138.                                           cancelButtonTitle:@"OK"  
  139.                                           otherButtonTitles:nil];  
  140.     [alert show];  
  141.     [alert release];  
  142. }  
  143.   
  144.   
  145. #pragma mark socket delegate  
  146.   
  147. - (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{  
  148.     [client readDataWithTimeout:-1 tag:0];  
  149. }  
  150.   
  151. - (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err  
  152. {  
  153.     NSLog(@"Error");  
  154. }  
  155.   
  156. - (void)onSocketDidDisconnect:(AsyncSocket *)sock  
  157. {  
  158.     NSString *msg = @"Sorry this connect is failure";  
  159.     [self showMessage:msg];  
  160.     [msg release];  
  161.     client = nil;  
  162. }  
  163.   
  164. - (void)onSocketDidSecure:(AsyncSocket *)sock{  
  165.       
  166. }  
  167.   
  168. - (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{  
  169.       
  170.     NSString* aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];  
  171.     NSLog(@"Hava received datas is :%@",aStr);  
  172.     self.outputMsg.text = aStr;  
  173.     [aStr release];  
  174.     [client readDataWithTimeout:-1 tag:0];  
  175. }  
  176.   
  177. #pragma mark dealloc  
  178.   
  179. - (void)dealloc {  
  180.       
  181.     [client release];  
  182.     [inputMsg release];  
  183.     [outputMsg release];  
  184.     [super dealloc];  
  185. }  
  186.   
  187. @end  

 

 

 

还是先给出我的界面吧,否则很难懂这些代码

iPad界面

 

 

这样大家满意了吧!


好了说了这么多我们还是来看看代码究竟怎么回事吧。

首先从头文件开始看吧,

1,导入头文件#import "AsyncSocket.h",然后是一些宏

2,声明一个AsyncSocket对象,其他就是一些IBoutlet

再次我们看看视图加载,

 

Java代码   收藏代码
  1. - (void)viewDidLoad {  
  2.     //[super viewDidLoad];  
  3.     [self connectServer:HOST_IP port:HOST_PORT];  
  4.     //监听读取  
  5.       
  6. }  
 显然我们调用了connectServer::这个方法。

在这个方法中,首先初始化我们的对象,使用代理的方式。对象显示是self。然后我们便需在我们的类中实现它的各种方法,来得到各种我们想得到的。

client = [[AsyncSocket alloc] initWithDelegate:self];


下面就是连接服务器了,

[client connectToHost:hostIP onPort:hostPort error:&err]


并且当client不为空时,我们就读取服务器的信息

[client readDataWithTimeout:-1 tag:0];

 

Java代码   收藏代码
  1. - (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{  
  2.       
  3.     NSString* aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];  
  4.     NSLog(@"Hava received datas is :%@",aStr);  
  5.     self.outputMsg.text = aStr;  
  6.     [aStr release];  
  7.     [client readDataWithTimeout:-1 tag:0];  
  8. }  
 

 

在这个方法中很耐人寻味,主要就是在于递归的调用。

 

Sendmsg代码   收藏代码
  1. - (IBAction) sendMsg{  
  2.       
  3.     NSString *inputMsgStr = self.inputMsg.text;  
  4.     NSString * content = [inputMsgStr stringByAppendingString:@"\r\n"];  
  5.     NSLog(@"%a",content);  
  6.     NSData *data = [content dataUsingEncoding:NSISOLatin1StringEncoding];  
  7.     [client writeData:data withTimeout:-1 tag:0];  
  8. }  

 我们在看看上面发送消息的代码,中的在于"\r\n"的拼接,否则在java端的程序,无法知道你发过来的信息是否结束,当然你也可以使用其他的方式来读取客户端,比如定时;但是我在java端写的server是readLine来判断的,所以需要拼接这个\r\n.


 

其他的代码除了asyncSocket代理外都是我们所熟悉的。

这些都是asyncsocket代理的代码   收藏代码
  1. - (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{  
  2.     [client readDataWithTimeout:-1 tag:0];  
  3. }  
  4.   
  5. - (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err  
  6. {  
  7.     NSLog(@"Error");  
  8. }  
  9.   
  10. - (void)onSocketDidDisconnect:(AsyncSocket *)sock  
  11. {  
  12.     NSString *msg = @"Sorry this connect is failure";  
  13.     [self showMessage:msg];  
  14.     [msg release];  
  15.     client = nil;  
  16. }  
  17.   
  18. - (void)onSocketDidSecure:(AsyncSocket *)sock{  
  19.       
  20. }  
  21.   
  22. - (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{  
  23.       
  24.     NSString* aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];  
  25.     NSLog(@"Hava received datas is :%@",aStr);  
  26.     self.outputMsg.text = aStr;  
  27.     [aStr release];  
  28.     [client readDataWithTimeout:-1 tag:0];  
  29. }  
 

到此就结束了。

  • 0
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值