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 library similar to the open-source project. Cocoa asyncsocket on the iPhone,
Official website. 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: 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
The nsdata object is used for both socket transmission and receiving. Its definition is nsdata.
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 writedataMethod 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];