Implement socket TCP/IP-based communication in IOS

Source: Internet
Author: User

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/

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.