Socket programming for IOS Study Notes
Recently, I started to study IOS programming quietly. Although it was a little late, there was a saying: "If you want to do it, it's never too late ". So today, I want to learn about IOS. (I have been engaged in. Net for the past four years, and java has been trained for one year.) Where should I start with learning IOS? We recommend two introductory books, Beginning iOS 7 Development and Objective-C. Programming (version 4th). They are just two hands and one hand. However, I learned the basics and theories first, but when I was doing this, I didn't want to learn the usage of those controls from the examples in the book. I couldn't grasp all of them, it is better to go to the document to see the usage when using it. Even if I learned how to use only those attributes of those controls, I 'd better think of a small project, and then naturally use all the controls. So today I want to compile a socket example. Although it is simple, I still encountered a lot of trouble while doing it. Preparations before coding: I use xcode6, so I used GCDAsyncSocket 1 that supports ARC when preparing the socket library. Download GCDAsyncSocket from the Internet: cocoaAsyncSocket/GCD at master · robbiehanson/CocoaAsyncSocket · GitHub 2. server code (I have used it. NET has written a simple) directly paste the background code: 1. header file # import <UIKit/UIKit. h> # import "GCDAsyncSocket. h "@ interface ViewController: UIViewController {NSString * host; uint16_t port; GCDAsyncSocket * socket;} @ property NSTimeInterval socketTimeOut; @ property (w Eak, nonatomic) extends UILabel * txtTitle; @ property (weak, nonatomic) IBOutlet UIButton * btnClickMe; @ property (weak, nonatomic) extends UITextField * txtQQ; @ property) IBOutlet UITextField * handle; @ property (weak, nonatomic) IBOutlet UISlider * sldValue; @ property (weak, nonatomic) IBOutlet UIButton * btnConnectSocket; @ property (weak, nonatomic) IBOutlet UILabel * lblSocketStatus ;@ Property (weak, nonatomic) IBOutlet UIButton * btnSendMsg;-(IBAction) btnClickMe_Click :( id) sender;-(IBAction) Consumer :( id) sender;-(IBAction) sldValue_Changed :( id) sender;-(IBAction) btnConnectSocket_click :( id) sender;-(IBAction) btnSendMsg_Click :( id) sender; 2. initialize m file variables-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. host = @ "192.168.1.103"; port = 52710; self. socketTimeOut = 100;} 3. link the server // link the socket server button and click the event-(IBAction) btnConnectSocket_click :( id) sender {_ lblSocketStatus. text = host; socket = [[GCDAsyncSocket alloc] initWithDelegate: self delegateQueue: dispatch_get_main_queue ()]; socket. delegate = self; NSError * err = nil; BOOL connState = [socket connectToHost: host onPort: port error: & err]; if (! ConnState) {_ lblSocketStatus. text = [_ lblSocketStatus. text stringByAppendingString: err. description];} else {NSLog (@ "connection server: % @ successful", host);} 4. send information // send information click event-(IBAction) btnSendMsg_Click :( id) sender {[socket writeData: [_ txtQQ. text dataUsingEncoding: NSUTF8StringEncoding] withTimeout:-1 tag: 0]; NSLog (@ "sent QQ number: % @", _ txtQQ. text); [socket readDataWithTimeout: self. socketTimeOut tag: 0];} 1 readDat is also called here AWithTimeout method to read the information returned by the server <br> 5. Receive information // read the data obtained by the server-(void) socket :( GCDAsyncSocket *) sock didReadData :( NSData *) data withTag :( long) tag {NSString * newMessage = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; NSLog (@ "received information returned by the server: % @", newMessage); [socket readDataWithTimeout: self. socketTimeOut tag: 0];} at this point, a simple socket sending and receiving information is completed, and a simple IM small project can be processed by yourself.