IOS instances implement Socket-Based TCP/IP communication

Source: Internet
Author: User

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:

 
 
  1. #import <UIKit/UIKit.h>   
  2. #import "AsyncSocket.h"   
  3. @interface iphone_socketViewController : UIViewController {   
  4.  
  5.     AsyncSocket *asyncSocket;   
  6. }   
  7.  
  8. @end 

Corresponding Method implementation:

 
 
  1. # Import "iphone_socketViewController.h"
  2. @ Implementation iphone_socketViewController
  3. -(Void) viewDidLoad {
  4. [Super viewDidLoad];
  5. AsyncSocket = [[AsyncSocket alloc] initWithDelegate: self];
  6. NSError * err = nil;
  7. If (! [AsyncSocket connectToHost: @ "192.168.0.113" onPort: 25001 error: & err])
  8. {
  9. NSLog (@ "Error: % @", err );
  10. }
  11. }
  12. -(Void) onSocket :( AsyncSocket *) sock didConnectToHost :( NSString *) host port :( UInt16) port
  13. {
  14. NSLog (@ "onSocket: % p didConnectToHost: % @ port: % hu", sock, host, port );
  15. [Sock readDataWithTimeout: 1 tag: 0];
  16. }
  17. -(Void) onSocket :( AsyncSocket *) sock didReadData :( NSData *) data withTag :( long) tag
  18. {
  19. NSString * aStr = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
  20. NSLog (@ "==%@", aStr );
  21. [AStr release];
  22. NSData * aData = [@ "<xml> I Like You <xml>" dataUsingEncoding: NSUTF8StringEncoding];
  23. [Sock writeData: aData withTimeout:-1 tag: 1];
  24. [Sock readDataWithTimeout: 1 tag: 0];
  25. }
  26. -(Void) onSocket :( AsyncSocket *) sock didSecure :( BOOL) flag
  27. {
  28. NSLog (@ "onSocket: % p didSecure: YES", sock );
  29. }
  30. -(Void) onSocket :( AsyncSocket *) sock willDisconnectWithError :( NSError *) err
  31. {
  32. NSLog (@ "onSocket: % p willDisconnectWithError: % @", sock, err );
  33. }
  34. -(Void) onSocketDidDisconnect :( AsyncSocket *) sock
  35. {
  36. // Disconnected
  37. NSLog (@ "onSocketDidDisconnect: % p", sock );
  38. }
  39. -(Void) didReceiveMemoryWarning {
  40. [Super didReceiveMemoryWarning];
  41. }
  42. -(Void) viewDidUnload {
  43. AsyncSocket = nil;
  44. }
  45. -(Void) dealloc {
  46. [AsyncSocket release];
  47. [Super dealloc];
  48. }
  49. @ End

Here we only implement a simple client. The server implementation is written by pathy. In the source code.

Compile the running result:

Server:

 
 
  1. bogon:iosworkspace vsp$ ./Servers.py  

The client's IP address is: ('192. 168.0.169 ', 192)

<Xml> I Like You <xml>

Subsequent data

Client:

 
 
  1. 19:14:47. 723 iphone. socket [3186: 307] WB: Notice: WinterBoard
  2. 19:14:48. 892 iphone. socket [3186: 307] onSocket: 0x16bd00 didConnectToHost: 192.168.0.113 port: 25001
  3. 19:14:48. 897 iphone. socket [3186: 307] === I am a server data
  4. 19:14:48. 911 iphone. socket [3186: 307] === I don't like you
  5. 19:14:48. 918 iphone. socket [3186: 307] onSocket: 0x16bd00 willDisconnectWithError :( null)
  6. 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.

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.