A simple implementation of the socket on the iOS client-leveraging the Gcasyncsocket framework

Source: Internet
Author: User

Gcasyncsocket This is a 2003 development of an open source framework first drag the Gcdasyncsocket. h and. m files into the project to attempt the controller to comply with the Gcdasyncsocketdelegate protocol Example: On the page has a simple textview and TextField in the TextField input text, click Send to send text to the server, the server side of the returned text data will be displayed on the TextView//server host IP address
#define KHost @ "172.16.3.101"
Server Host communication port
#define Kport 10024

@interface Lyviewcontroller () <GCDAsyncSocketDelegate]]]]>

Create a socket object for communication
@property (nonatomic, strong) Gcdasyncsocket *socket;
@property (nonatomic, strong) Gcdasyncsocket *socketserver;
Creating an installation Socket object
-(void) _setupsocket;
-(void) _setupserversocket;
//
-(void) _keyboardframechange: (nsnotification *) notification;

@end @implementation Lyviewcontroller

-(void) viewdidload
{
[Super Viewdidload];
Self.textField.delegate = self;
[Self _setupsocket];
[Self _setupserversocket];
}
-(BOOL) Prefersstatusbarhidden
{
return YES;
}
#pragma mark-the keyboard bounces to change the position of the input box
#pragma mark Registration Notice change the position of the input box
-(void) Viewwillappear: (BOOL) animated
{
[Super Viewwillappear:yes];
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (_keyboardframechange:) Name: Uikeyboardwillchangeframenotification Object:nil];
}
#pragma mark Removal Notice
-(void) Viewwilldisappear: (BOOL) animated
{
[Super Viewdiddisappear:yes];
[[Nsnotificationcenter Defaultcenter] removeobserver:self name:uikeyboardwillchangeframenotification Object:nil];
}
#pragma mark receives a keyboard bounce notification corresponding method
-(void) _keyboardframechange: (nsnotification *) notification
{
CGRect textfieldrect = self.textField.frame;

Nsdictionary *userinfo = Notification.userinfo;
CGRect beginrect = [Userinfo[uikeyboardframebeginuserinfokey] cgrectvalue];
CGRect endrect = [Userinfo[uikeyboardframeenduserinfokey] cgrectvalue];
CGFloat tempy = Cgrectgetminy (endrect)-Cgrectgetminy (Beginrect);

TEXTFIELDRECT.ORIGIN.Y + = Tempy;

[UIView animatewithduration:.25 animations:^{
Self.textField.frame = Textfieldrect;
}];

}
#pragma mark clicks the return keypad to retract the Send message (write data)
-(BOOL) Textfieldshouldreturn: (Uitextfield *) TextField
{

NSString *datastring = [Self.textField.text stringbyappendingstring:@ "\ r \ n"];
NSData *data = [datastring datausingencoding:nsutf8stringencoding];
[Self.socket writedata:data withtimeout:-1 tag:0];


[TextField Resignfirstresponder];
return YES;

}

#pragma amrk-private methods
Creating an installation Socket object
-(void) _setupsocket
{
Self.socket = [[Gcdasyncsocket alloc] initwithdelegate:self delegatequeue:dispatch_get_main_queue ()];
Connect to Server
[Self.socket connecttohost:khost Onport:kport Error:nil];
}

Create a Server object
-(void) _setupserversocket
{
Self.socketserver = [[Gcdasyncsocket alloc] initwithdelegate:self delegatequeue:dispatch_get_main_queue ()];

[Self.socket acceptonport:10024 Error:nil];

}

#pragma mark-socket delegate
Connection Server succeeded
-(void) Socket: (Gcdasyncsocket *) sock didconnecttohost: (NSString *) host port: (uint16_t) port
{
NSLog (@ "%s", __function__);
Waiting for data to be read
[Self.socket readdatatodata:[gcdasyncsocket Crlfdata] withtimeout:-1 Maxlength:nsintegermax tag:0];
}

callback method for reading data
-(void) Socket: (Gcdasyncsocket *) sock didreaddata: (NSData *) data withtag: (long) tag
{
NSString *content = [[NSString alloc] Initwithdata:data encoding:nsutf8stringencoding];
Self.textView.text = content;
}
-(void) Socket: (Gcdasyncsocket *) sock Didwritedatawithtag: (long) tag
{
[Self.socket readdatatodata:[gcdasyncsocket Crlfdata] withtimeout:-1 Maxlength:nsintegermax tag:0];

}

-(void) didreceivememorywarning
{
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}

@end

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.