socket第三方库 AsyncSocket(GCDAsyncSocket)
為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??
Socket描述了一個(gè)IP、端口對(duì)。它簡(jiǎn)化了程序員的操作,知道對(duì)方的IP以及PORT就可以給對(duì)方發(fā)送消息,再由服務(wù)器端來(lái)處理發(fā)送的這些消息。所以,Socket一定包含了通信的雙發(fā),即客戶端(Client)與服務(wù)端(server)。
1)服務(wù)端利用Socket監(jiān)聽(tīng)端口;
2)客戶端發(fā)起連接;
3)服務(wù)端返回信息,建立連接,開(kāi)始通信;
4)客戶端,服務(wù)端斷開(kāi)連接。
1套接字(socket)概念
套接字(socket)是通信的基石,是支持TCP/IP協(xié)議的網(wǎng)絡(luò)通信的基本操作單元。
應(yīng)用層通過(guò)傳輸層進(jìn)行數(shù)據(jù)通信時(shí),TCP會(huì)遇到同時(shí)為多個(gè)應(yīng)用程序進(jìn)程提供并發(fā)服務(wù)的問(wèn)題。多個(gè)TCP連接或多個(gè)應(yīng)用程序進(jìn)程可能需要通過(guò)同一個(gè) TCP協(xié)議端口傳輸數(shù)據(jù)。為了區(qū)別不同的應(yīng)用程序進(jìn)程和連接,許多計(jì)算機(jī)操作系統(tǒng)為應(yīng)用程序與TCP/IP協(xié)議交互提供了套接字(Socket)接口。應(yīng) 用層可以和傳輸層通過(guò)Socket接口,區(qū)分來(lái)自不同應(yīng)用程序進(jìn)程或網(wǎng)絡(luò)連接的通信,實(shí)現(xiàn)數(shù)據(jù)傳輸?shù)牟l(fā)服務(wù)。
2 建立socket連接
建立Socket連接至少需要一對(duì)套接字,其中一個(gè)運(yùn)行于客戶端,稱為ClientSocket,另一個(gè)運(yùn)行于服務(wù)器端,稱為ServerSocket。
套接字之間的連接過(guò)程分為三個(gè)步驟:服務(wù)器監(jiān)聽(tīng),客戶端請(qǐng)求,連接確認(rèn)。
服務(wù)器監(jiān)聽(tīng):服務(wù)器端套接字并不定位具體的客戶端套接字,而是處于等待連接的狀態(tài),實(shí)時(shí)監(jiān)控網(wǎng)絡(luò)狀態(tài),等待客戶端的連接請(qǐng)求。
客戶端請(qǐng)求:指客戶端的套接字提出連接請(qǐng)求,要連接的目標(biāo)是服務(wù)器端的套接字。為此,客戶端的套接字必須首先描述它要連接的服務(wù)器的套接字,指出服務(wù)器端套接字的地址和端口號(hào),然后就向服務(wù)器端套接字提出連接請(qǐng)求。
連接確認(rèn):當(dāng)服務(wù)器端套接字監(jiān)聽(tīng)到或者說(shuō)接收到客戶端套接字的連接請(qǐng)求時(shí),就響應(yīng)客戶端套接字的請(qǐng)求,建立一個(gè)新的線程,把服務(wù)器端套接字的描述發(fā) 給客戶端,一旦客戶端確認(rèn)了此描述,雙方就正式建立連接。而服務(wù)器端套接字繼續(xù)處于監(jiān)聽(tīng)狀態(tài),繼續(xù)接收其他客戶端套接字的連接請(qǐng)求。
?
4、SOCKET連接與TCP連接
創(chuàng)建Socket連接時(shí),可以指定使用的傳輸層協(xié)議,Socket可以支持不同的傳輸層協(xié)議(TCP或UDP),當(dāng)使用TCP協(xié)議進(jìn)行連接時(shí),該Socket連接就是一個(gè)TCP連接。
?
5、Socket連接與HTTP連接
由于通常情況下Socket連接就是TCP連接,因此Socket連接一旦建立,通信雙方即可開(kāi)始相互發(fā)送數(shù)據(jù)內(nèi)容,直到雙方連接斷開(kāi)。但在實(shí)際網(wǎng) 絡(luò)應(yīng)用中,客戶端到服務(wù)器之間的通信往往需要穿越多個(gè)中間節(jié)點(diǎn),例如路由器、網(wǎng)關(guān)、防火墻等,大部分防火墻默認(rèn)會(huì)關(guān)閉長(zhǎng)時(shí)間處于非活躍狀態(tài)的連接而導(dǎo)致 Socket 連接斷連,因此需要通過(guò)輪詢告訴網(wǎng)絡(luò),該連接處于活躍狀態(tài)。
而HTTP連接使用的是“請(qǐng)求—響應(yīng)”的方式,不僅在請(qǐng)求時(shí)需要先建立連接,而且需要客戶端向服務(wù)器發(fā)出請(qǐng)求后,服務(wù)器端才能回復(fù)數(shù)據(jù)。
很多情況下,需要服務(wù)器端主動(dòng)向客戶端推送
iphone的標(biāo)準(zhǔn)推薦CFNetwork C庫(kù)編程.但是編程比較煩躁。在其它OS往往用類來(lái)封裝的對(duì)Socket函數(shù)的處理。比如MFC的CAsysncSocket.在iphone也有類似于 開(kāi)源項(xiàng)目.cocoa AsyncSocket庫(kù),?官方網(wǎng)站:http://code.google.com/p/cocoaasyncsocket/?它用來(lái)簡(jiǎn)化 CFnetwork的調(diào)用.
一.在項(xiàng)目引入ASyncSocket庫(kù)
??1.下載ASyncSocket庫(kù)源碼
??2.把ASyncSocket庫(kù)源碼加入項(xiàng)目:只需要增加RunLoop目錄中的AsyncSocket.h、AsyncSocket.m、AsyncUdpSocket.h和AsyncUdpSocket.m四個(gè)文件。
??3.在項(xiàng)目增加CFNetwork框架
?? ? ??在Framework目錄右健,選擇Add-->Existing Files...??? , 選擇?CFNetwork.framework
?
二.TCP客戶端
??1. 在controller頭文件定義AsyncSocket對(duì)象
#import <UIKit/UIKit.h>
#import "AsyncSocket.h"
?
@interface HelloiPhoneViewController : UIViewController {
?? ?UITextField ? ?* textField;
?? ?AsyncSocket * asyncSocket;
}
@property (retain, nonatomic) IBOutlet UITextField *textField;
- (IBAction) buttonPressed: (id)sender;
- (IBAction) textFieldDoneEditing: (id)sender; ? ?
@end
?
??2.在需要聯(lián)接地方使用connectToHost聯(lián)接服務(wù)器
??其中initWithDelegate的參數(shù)中self是必須。這個(gè)對(duì)象指針中的各個(gè)Socket響應(yīng)的函數(shù)將被ASyncSocket所調(diào)用.
?
?? ?asyncSocket = [[AsyncSocket alloc] initWithDelegate:self];?
?? ?NSError *err = nil;?
?? ?if(![asyncSocket connectToHost:host on:port error:&err])?
?? ?{?
?? ? ? ?NSLog(@"Error: %@", err);?
?? ?}?
?
3.增加Socket響應(yīng)事件
?? ? 因?yàn)閕nitWithDelegate把將當(dāng)前對(duì)象傳遞進(jìn)去,這樣只要在當(dāng)前對(duì)象方法實(shí)現(xiàn)相應(yīng)方法.
?
4.關(guān)于NSData對(duì)象
?? ?無(wú)論SOCKET收發(fā)都采用NSData對(duì)象.它的定義是?http://developer.apple.com/library/mac /#documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html
???NSData主要是帶一個(gè)(id)data指向的數(shù)據(jù)空間和長(zhǎng)度 length.
?? ?NSString 轉(zhuǎn)換成NSData 對(duì)象
?
?? ? ?NSData* xmlData = [@"testdata" dataUsingEncoding:NSUTF8StringEncoding];
?
?? NSData 轉(zhuǎn)換成NSString對(duì)象
?
?? NSData * data;
?? NSString *result = [[NSString alloc] initWithData:data ?encoding:NSUTF8StringEncoding];
?
4.發(fā)送數(shù)據(jù)
?? ? AsyncSocket ?writeData ? ?方法來(lái)發(fā)送數(shù)據(jù),它有如下定義
?? ?- (void)writeData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
?
以下是一個(gè)實(shí)例語(yǔ)句.
?? ? NSData* aData= [@"test data" dataUsingEncoding: NSUTF8StringEncoding];
?? ? [sock writeData:aData withTimeout:-1 tag:1];
?在onSocket重載函數(shù),有如定義采用是專門(mén)用來(lái)處理SOCKET的發(fā)送數(shù)據(jù)的:
?? ?-(void)onSocket(AsyncSocket *)sock didWriteDataWithTag:(long)tag
{
?? ? ?NSLog(@"thread(%),onSocket:%p didWriteDataWithTag:%d",[[NSThread currentThread] name],
?? ? sock,tag);
}?
?
5.接收Socket數(shù)據(jù).
?? ?在onSocket重載函數(shù),有如定義采用是專門(mén)用來(lái)處理SOCKET的接收數(shù)據(jù)的.
?? ?-(void) onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
在中間將其轉(zhuǎn)換成NSString進(jìn)行顯示.
?
?? ?NSString* aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];?
?? ?NSLog(@"===%@",aStr);?
?? ?[aStr release];
Asyncsocket的例子 下面是用開(kāi)源的庫(kù)Asyncsocket的例子:// // SocketDemoViewController.h // SocketDemo // // Created by xiang xiva on 10-7-10. // Copyright 2010 __MyCompanyName__. All rights reserved. //#import <UIKit/UIKit.h> #import "AsyncSocket.h" #define SRV_CONNECTED 0 #define SRV_CONNECT_SUC 1 #define SRV_CONNECT_FAIL 2 #define HOST_IP @"192.168.110.1" #define HOST_PORT 8080@interface SocketDemoViewController : UIViewController {UITextField *inputMsg;UILabel *outputMsg;AsyncSocket *client; }@property (nonatomic, retain) AsyncSocket *client; @property (nonatomic, retain) IBOutlet UITextField *inputMsg; @property (nonatomic, retain) IBOutlet UILabel *outputMsg;- (int) connectServer: (NSString *) hostIP port:(int) hostPort; - (void) showMessage:(NSString *) msg; - (IBAction) sendMsg; - (IBAction) reConnect; - (IBAction) textFieldDoneEditing:(id)sender; - (IBAction) backgroundTouch:(id)sender;@end// // SocketDemoViewController.m // SocketDemo // // Created by xiang xiva on 10-7-10. // Copyright 2010 __MyCompanyName__. All rights reserved. //#import "SocketDemoViewController.h"@implementation SocketDemoViewController@synthesize inputMsg, outputMsg; @synthesize client; /* // The designated initializer. Override to perform setup that is required before the view is loaded. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];if (self) {// Custom initialization}return self; } *//* // Implement loadView to create a view hierarchy programmatically, without using a nib. - (void)loadView { } */// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad {//[super viewDidLoad];[self connectServer:HOST_IP port:HOST_PORT];//監(jiān)聽(tīng)讀取}// Override to allow orientations other than the default portrait orientation. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {return YES; }- (void)didReceiveMemoryWarning {// Releases the view if it doesn't have a superview.[super didReceiveMemoryWarning];// Release any cached data, images, etc that aren't in use. }- (void)viewDidUnload {self.client = nil;// Release any retained subviews of the main view.// e.g. self.myOutlet = nil; }- (int) connectServer: (NSString *) hostIP port:(int) hostPort{if (client == nil) {client = [[AsyncSocket alloc] initWithDelegate:self];NSError *err = nil;//192.168.110.128if (![client connectToHost:hostIP onPort:hostPort error:&err]) {NSLog(@"%@ %@", [err code], [err localizedDescription]);UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[@"Connection failed to host "stringByAppendingString:hostIP]message:[[[NSString alloc]initWithFormat:@"%@",[err code]] stringByAppendingString:[err localizedDescription]]delegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil];[alert show];[alert release];//client = nil;return SRV_CONNECT_FAIL;} else {NSLog(@"Conectou!");return SRV_CONNECT_SUC;}}else {[client readDataWithTimeout:-1 tag:0];return SRV_CONNECTED;}}- (IBAction) reConnect{int stat = [self connectServer:HOST_IP port:HOST_PORT];switch (stat) {case SRV_CONNECT_SUC:[self showMessage:@"connect success"];break;case SRV_CONNECTED:[self showMessage:@"It's connected,don't agian"];break;default:break;} }- (IBAction) sendMsg{NSString *inputMsgStr = self.inputMsg.text;NSString * content = [inputMsgStr stringByAppendingString:@"\r\n"];NSLog(@"%a",content);NSData *data = [content dataUsingEncoding:NSISOLatin1StringEncoding];[client writeData:data withTimeout:-1 tag:0];//[data release];//[content release];//[inputMsgStr release];//繼續(xù)監(jiān)聽(tīng)讀取//[client readDataWithTimeout:-1 tag:0]; }#pragma mark - #pragma mark close Keyboard - (IBAction) textFieldDoneEditing:(id)sender{[sender resignFirstResponder]; }- (IBAction) backgroundTouch:(id)sender{[inputMsg resignFirstResponder]; }#pragma mark socket uitl- (void) showMessage:(NSString *) msg{UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"Alert!"message:msgdelegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil];[alert show];[alert release]; }#pragma mark socket delegate- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{[client readDataWithTimeout:-1 tag:0]; }- (void)onSocket:(AsyncSocket *)sock willDisconnectWithError:(NSError *)err {NSLog(@"Error"); }- (void)onSocketDidDisconnect:(AsyncSocket *)sock {NSString *msg = @"Sorry this connect is failure";[self showMessage:msg];[msg release];client = nil; }- (void)onSocketDidSecure:(AsyncSocket *)sock{}- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag{NSString* aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];NSLog(@"Hava received datas is :%@",aStr);self.outputMsg.text = aStr;[aStr release];[client readDataWithTimeout:-1 tag:0]; }#pragma mark dealloc- (void)dealloc {[client release];[inputMsg release];[outputMsg release];[super dealloc]; }@end
轉(zhuǎn)載于:https://my.oschina.net/amoyai/blog/91694
總結(jié)
以上是生活随笔為你收集整理的socket第三方库 AsyncSocket(GCDAsyncSocket)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。