I have previously written about HTTP-based communication at the network transmission layer. Now the project needs to implement the TCP/IP communication protocol. I searched through the network. Someone has already written a public class library asyncsocket, the following describes how to use asyncsocket.
Asyncsocket official documentation: http://code.google.com/p/cocoaasyncsocket/
The usage is as follows:
1. Create a project.
2. Add asyncsocket to the project.
3. Add cfnetwork. Framework to the project.
4. Implementation test:
# Import <uikit/uikit. h>
# Import "asyncsocket. H"
@ Interface iphone_socketviewcontroller: uiviewcontroller {
Asyncsocket * asyncsocket;
}
@ End
Corresponding Method implementation:
# Import "iphone_socketviewcontroller.h"
@ Implementation iphone_socketviewcontroller
-(Void) viewdidload {
[Super viewdidload];
Asyncsocket = [[asyncsocket alloc] initwithdelegate: Self];
Nserror * err = nil;
If (! [Asyncsocket connecttohost: @ "192.168.0.113" onport: 25001 error: & err])
{
Nslog (@ "error: % @", err );
}
}
-(Void) onsocket :( asyncsocket *) sock didconnecttohost :( nsstring *) host port :( uint16) Port
{
Nslog (@ "onsocket: % P didconnecttohost: % @ port: % Hu", Sock, host, Port );
[Sock readdatawithtimeout: 1 Tag: 0];
}
-(Void) onsocket :( asyncsocket *) sock didreaddata :( nsdata *) Data withtag :( long) Tag
{
Nsstring * astr = [[nsstring alloc] initwithdata: data encoding: nsutf8stringencoding];
Nslog (@ "==%@", astr );
[Astr release];
Nsdata * aData = [@ "<XML> I Like You <XML>" datausingencoding: nsutf8stringencoding];
[Sock writedata: aData withtimeout:-1 Tag: 1];
[Sock readdatawithtimeout: 1 Tag: 0];
}
-(Void) onsocket :( asyncsocket *) sock didsecure :( bool) Flag
{
Nslog (@ "onsocket: % P didsecure: yes", sock );
}
-(Void) onsocket :( asyncsocket *) sock willdisconnectwitherror :( nserror *) err
{
Nslog (@ "onsocket: % P willdisconnectwitherror: % @", Sock, err );
}
-(Void) onsocketdiddisconnect :( asyncsocket *) sock
{
// Disconnected
Nslog (@ "onsocketdiddisconnect: % P", sock );
}
-(Void) didreceivememorywarning {
[Super didreceivememorywarning];
}
-(Void) viewdidunload {
Asyncsocket = nil;
}
-(Void) dealloc {
[Asyncsocket release];
[Super dealloc];
}
@ End
Here we only implement a simple client. The server implementation is written by pathy. In the source code.
Compile the running result:
Server:
Bogon: iosworkspace VSP $./servers. py
The client's IP address is: ('192. 168.0.169 ', 192)
<XML> I Like You <XML>
-----
Subsequent data ---
Client:
19:14:47. 723 iPhone. Socket [3186: 307] WB: Notice: winterboard
19:14:48. 892 iPhone. Socket [3186: 307] onsocket: 0x16bd00 didconnecttohost: 192.168.0.113 port: 25001
19:14:48. 897 iPhone. Socket [3186: 307] === I am a server data
19:14:48. 911 iPhone. Socket [3186: 307] === I don't like you
19:14:48. 918 iPhone. Socket [3186: 307] onsocket: 0x16bd00 willdisconnectwitherror :( null)
19:14:48. 928 iPhone. Socket [3186: 307] onsocketdiddisconnect: 0x16bd00
Source code: http://easymorse-iphone.googlecode.com/svn/trunk/iphone.socket/
Share:
I have previously written about HTTP-based communication at the network transmission layer. Now the project needs to implement the TCP/IP communication protocol. I searched through the network. Someone has already written a public class library asyncsocket, the following describes how to use asyncsocket.
Asyncsocket official documentation: http://code.google.com/p/cocoaasyncsocket/
The usage is as follows:
1. Create a project.
2. Add asyncsocket to the project.
3. Add cfnetwork. Framework to the project.
4. Implementation test:
# Import <uikit/uikit. h>
# Import "asyncsocket. H"
@ Interface iphone_socketviewcontroller: uiviewcontroller {
Asyncsocket * asyncsocket;
}
@ End
Corresponding Method implementation:
# Import "iphone_socketviewcontroller.h"
@ Implementation iphone_socketviewcontroller
-(Void) viewdidload {
[Super viewdidload];
Asyncsocket = [[asyncsocket alloc] initwithdelegate: Self];
Nserror * err = nil;
If (! [Asyncsocket connecttohost: @ "192.168.0.113" onport: 25001 error: & err])
{
Nslog (@ "error: % @", err );
}
}
-(Void) onsocket :( asyncsocket *) sock didconnecttohost :( nsstring *) host port :( uint16) Port
{
Nslog (@ "onsocket: % P didconnecttohost: % @ port: % Hu", Sock, host, Port );
[Sock readdatawithtimeout: 1 Tag: 0];
}
-(Void) onsocket :( asyncsocket *) sock didreaddata :( nsdata *) Data withtag :( long) Tag
{
Nsstring * astr = [[nsstring alloc] initwithdata: data encoding: nsutf8stringencoding];
Nslog (@ "==%@", astr );
[Astr release];
Nsdata * aData = [@ "<XML> I Like You <XML>" datausingencoding: nsutf8stringencoding];
[Sock writedata: aData withtimeout:-1 Tag: 1];
[Sock readdatawithtimeout: 1 Tag: 0];
}
-(Void) onsocket :( asyncsocket *) sock didsecure :( bool) Flag
{
Nslog (@ "onsocket: % P didsecure: yes", sock );
}
-(Void) onsocket :( asyncsocket *) sock willdisconnectwitherror :( nserror *) err
{
Nslog (@ "onsocket: % P willdisconnectwitherror: % @", Sock, err );
}
-(Void) onsocketdiddisconnect :( asyncsocket *) sock
{
// Disconnected
Nslog (@ "onsocketdiddisconnect: % P", sock );
}
-(Void) didreceivememorywarning {
[Super didreceivememorywarning];
}
-(Void) viewdidunload {
Asyncsocket = nil;
}
-(Void) dealloc {
[Asyncsocket release];
[Super dealloc];
}
@ End
Here we only implement a simple client. The server implementation is written by pathy. In the source code.
Compile the running result:
Server:
Bogon: iosworkspace VSP $./servers. py
The client's IP address is: ('192. 168.0.169 ', 192)
<XML> I Like You <XML>
-----
Subsequent data ---
Client:
19:14:47. 723 iPhone. Socket [3186: 307] WB: Notice: winterboard
19:14:48. 892 iPhone. Socket [3186: 307] onsocket: 0x16bd00 didconnecttohost: 192.168.0.113 port: 25001
19:14:48. 897 iPhone. Socket [3186: 307] === I am a server data
19:14:48. 911 iPhone. Socket [3186: 307] === I don't like you
19:14:48. 918 iPhone. Socket [3186: 307] onsocket: 0x16bd00 willdisconnectwitherror :( null)
19:14:48. 928 iPhone. Socket [3186: 307] onsocketdiddisconnect: 0x16bd00
Source code: http://easymorse-iphone.googlecode.com/svn/trunk/iphone.socket/