iOS uses Asyncsocket for socket programming

Source: Internet
Author: User

The standard recommended for the iphone is Cfnetwork C library programming. But programming is rather irritable. In other OSes, the processing of the socket function is often encapsulated with classes. For example, MFC's Casysncsocket. The iphone also has a similar open source project. Cocoa Asyncsocket Library, official website: http://code.google.com/p/cocoaasyncsocket/ It is used to simplify the invocation of cfnetwork.

I. Introduction of the Asyncsocket library in the project

1. Download Asyncsocket Library Source code

2. Add the Asyncsocket Library source to the project: Just add four files in AsyncSocket.h, ASYNCSOCKET.M, AsyncUdpSocket.h and ASYNCUDPSOCKET.M in the Runloop directory.

3. Increased Cfnetwork framework in the project

In the framework directory right-click, select Add-->existing Files ..., select Cfnetwork.framework

Two. TCP Client

1. Define the Asyncsocket object in 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 servers where needed

Where the initwithdelegate parameter in self is required. The function of each socket response in this object pointer is called by Asyncsocket.

Asyncsocket = [[Asyncsocket alloc] initwithdelegate:self];

Nserror *err = nil;

if (![ Asyncsocket connecttohost:host On:port Error:&err])

{

NSLog (@ "Error:%@", err);

}

3. Increase the socket response event

Because Initwithdelegate will pass the current object in, so long as the current object method to implement the corresponding method.

4. About NSData Objects

The NSData object is used regardless of whether the socket is sent or received. It is defined as http://developer.apple.com/library/mac/#documentation/cocoa/reference/foundation/ Classes/nsdata_class/reference/reference.html

The NSData is primarily a data space and length with a (ID) that is pointed to.

NSString Convert to NSData object

nsdata* xmlData = [@ "testdata" datausingencoding:nsutf8stringencoding];

NSData Convert to NSString object

NSData * data;

NSString *result = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];

4. Send data

Asyncsocket WriteData method to send data, it has the following definition

-(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];

In the Onsocket overload function, as defined by the send data that is specifically used to handle the socket:

-(void) onsocket (Asyncsocket *) sock Didwritedatawithtag: (long) tag

{

NSLog (@ "thread (%), onsocket:%p didwritedatawithtag:%d", [[Nsthread CurrentThread] name],

Sock,tag);

}

5. Receive the socket data.

In the Onsocket overload function, as defined by the receive data that is specifically used to handle the socket.

-(void) Onsocket: (Asyncsocket *) sock didreaddata: (NSData *) data withtag: (long) tag

In the middle, it is converted to NSString for display.

nsstring* aStr = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];

NSLog (@ "===%@", aStr);

[ASTR release];

iOS uses Asyncsocket for socket 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.