IOS: use open-source IOS for socket programming

Source: Internet
Author: User


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 MFC. In the iPhone also has similar open source project. Cocoa asyncsocket library, the official website: http://code.google.com/p/cocoaasyncsocket/ it is used to simplify the cfnetwork call.

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: add four files in the runloop Directory: asyncsocket. H, asyncsocket. M, asyncudpsocket. H, and asyncudpsocket. M.

3. Add the cfnetwork framework to the Project

Right-click the framework directory, select Add --> existing files..., and select cfnetwork. Framework

Ii. tcp client

1. Define 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 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 and receive. It is defined as a 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 * xmldata = [@ "testdata" datausingencoding: nsutf8stringencoding];

 

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

In the onsocket overload function, it is defined as a function specifically used to process the data sent by the socket:

-(Void) onsocket (asyncsocket *) sock didwritedatawithtag :( long) Tag

{

Nslog (@ "thread (%), onsocket: % P didwritedatawithtag: % d", [[nsthread currentthread] Name],

Sock, tag );

}

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

Related Article

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.