Detailed description of AsyncSocket network library programming for iPhone

Source: Internet
Author: User

DetailsIPhoneLowerAsyncSocket network libraryProgramming is the content to be introduced in this article. CFNetwork C library programming is recommended for iphone standards, but programming is quite annoying. In other operating systems, classes are often used to encapsulate Socket functions. For example, the CAsysncSocket of MFC also has a similar open-source project on the iphone. cocoa AsyncSocket Library

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

It is used to simplify CFnetwork calls.

1. Introduce the ASyncSocket library in the project

1. Download the source code of the ASyncSocket Library

2. Add the source code of the ASyncSocket Library to the project.

3. Add the CFNetwork framework to the Project

Note that XCode has a BUG. The CFNetwork framework is not in the Frame list.

In XCode 3.1.4, right-click the Framework directory and select Add --> Existing Files...

Select the following directory

 
 
  1. /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOSX.Y.sdk/System/Library/Frameworks/CFNetwork.framework 

Note that the iPhone OSX. Y. sdk is the corresponding version. In XCode 3.1.4, you can select the maximum version of iPhone OS 3.1.3sdk.

Ii. TCP client

1. Add the AsyncSocket pointer to the controller header file.

 
 
  1. #import <UIKit/UIKit.h> 
  2. #import "AsyncSocket.h"  
  3.  
  4. @interface HelloiPhoneViewController : UIViewController {  
  5.     UITextField    * textField;  
  6.     AsyncSocket * asyncSocket;  
  7.  
  8. }  
  9.  
  10. @property (retain, nonatomic) IBOutlet UITextField *textField;  
  11.  
  12. - (IBAction) buttonPressed: (id)sender;  
  13. - (IBAction) textFieldDoneEditing: (id)sender;      
  14.  
  15. @end 

2. Use connectToHost to connect to the server

Here, self is required in the initWithDelegate parameter. The Socket response functions in this object pointer will be called by ASyncSocket.

 
 
  1. asyncSocket = [[AsyncSocket alloc] initWithDelegate:self];   
  2.     NSError *err = nil;   
  3.     if(![asyncSocket connectToHost:host on:port error:&err])   
  4.     {   
  5.         NSLog(@"Error: %@", err);   
  6.     }  

3. Added Socket response events.

Because initWithDelegate transfers the current object, you only need to implement the corresponding method in the current object method.

4. About NSData objects

NSData objects are used for both SOCKET transmission and receiving. Its definition is

Http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html

NSData mainly includes a data space and length pointed to by (id) data.

NSString to NSData object

NSData to NSString object

 
 
  1. NSData * data;  
  2.  
  3. NSString *result = [[NSString alloc] initWithData:data  encoding:NSUTF8StringEncoding]; 

4. Send data

AsyncSocket writeData method to send data, as defined below

 
 
  1. (void)writeData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag; 

The following is an instance statement.

 
 
  1. NSData* aData= [@"test data" dataUsingEncoding: NSUTF8StringEncoding];  
  2.        
  3. [sock writeData:aData withTimeout:-1 tag:1]; 

5. Receive Socket data.

In the onSocket overload function, the definition is used to process the received data of the SOCKET.

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

Convert it to NSString in the middle for display.

 
 
  1. NSString* aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];      
  2.  NSLog(@"===%@",aStr);    
  3.    [aStr release];  

Summary: DetailsIPhoneLowerAsyncSocket network libraryI hope this article will help you with programming!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.