IOSInstance implementation based onSocket TCP/IPCommunication is the content to be introduced in this article.HttpNetwork transmission layer communication, now the project needs to implementTCP/IPThrough the network, someone has already written a public class library AsyncSocket. The following describes how to use AsyncSocket. Refer to the official document of AsyncSocket.
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/
Summary:IOSInstance implementation based onSocket TCP/IPI hope this article will help you with the introduction of the communication content.