iOS Learning Notes Socket programming

Source: Internet
Author: User

this article reprinted to Http://www.tuicool.com/articles/RNFRvaTime 2014-12-07 12:48:00Blog Park-Original essence Area original http://www.cnblogs.com/chengzi/p/4149242.html

Recently began to study iOS programming, although a bit late to start, but there is a good saying: "If you want to do, at any time is not too late." So today, start learning iOS well. (I've been doing it for 4 years.) NET, Java also trained for one year)

Going to learn iOS, where do I start? We recommend two introductory books "Beginning IOS 7 Development", "OBJECTIVE-C. Program Design (4th edition)", just two hands, one hand.

But I study is the first to learn the foundation, theory, but do not do when the book on the example to learn the usage of the several controls, can not grasp all, rather than use the time to help the document to see how to use. Even if you learn the few properties of those controls, it is best to think of a small project and then use all the controls naturally.

So today I want to make up a socket example, although simple, but in the time of doing still encountered a lot of small trouble.

Pre-coding preparation work:

I used the xcode6, so I used the ARC-enabled gcdasyncsocket when I prepared the socket library.

1, to the online download Gcdasyncsocket address: COCOAASYNCSOCKET/GCD at Master Robbiehanson/cocoaasyncsocket GitHub

2, server-side code (I have used.) Net wrote a simple)

Operating effect:

Directly affixed to 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 (Weak, nonatomic) Iboutlet UILabel*txttitle;@property (Weak, nonatomic) Iboutlet UIButton *btnclickme; @property (Weak, nonatomic) Iboutlet Uitextfield *txtqq;  @property (Weak, nonatomic) Iboutlet Uitextfield *txtpwd;  @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) Txtpwd_doneediting: (ID) sender;-(ibaction) sldvalue_changed: (ID) sender;-(ibaction) Btnconnectsocket_click: (ID) sender;-(ibaction) Btnsendmsg_click: (ID) sender;            

2, m file variable initialization

-(void) ViewdidLoad {    [Super Viewdidload];   From a nib. host = @"192.168.1.103"; port = 52710; self.sockettimeout = +;}      

3. Linked server

Link Socket Server button click 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 (@ "Connecting server:%@ success", host); }  }           /span>          

4. Send Information

Send Message 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];}   

This also calls the

Readdatawithtimeout method to read the information returned by the server

5. Receiving 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 the information returned by the server:%@", newmessage);

[Socket readDataWithTimeout:self.socketTimeOut tag:0];

}

This completes the simple socket to send and receive information, can be processed by themselves to complete a simple IM small project.

iOS Learning Notes Socket programming

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.