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
- /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.
- #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. 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.
- asyncSocket = [[AsyncSocket alloc] initWithDelegate:self];
- NSError *err = nil;
- if(![asyncSocket connectToHost:host on:port error:&err])
- {
- NSLog(@"Error: %@", err);
- }
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
- NSData * data;
-
- NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
4. Send data
AsyncSocket writeData method to send data, as defined below
- (void)writeData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;
The following is an instance statement.
- NSData* aData= [@"test data" dataUsingEncoding: NSUTF8StringEncoding];
-
- [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.
- -(void) onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
Convert it to NSString in the middle for display.
- NSString* aStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- NSLog(@"===%@",aStr);
- [aStr release];
Summary: DetailsIPhoneLowerAsyncSocket network libraryI hope this article will help you with programming!