Cocoaasyncsocket using Notes

Source: Internet
Author: User

First go to GitHub's website to download the latest package, then take a look at the introduction. It's written in more detail.


Many of the online posts are old versions. The authorities have introduced Gcdasyncsocket to replace the old Asyncsocket.

My project is No-arc, this frame only has the arc version. So after introducing the gcdasyncsocket. h and. m files, modify the properties of the project in Xcode.

1) Find the compiler for C/C++/OBJECTIVE-C option in "Build Settings" in targets. Change to Apple LLVM compiler 3 only if 3.0 or more

2) Find GCDASYNCSOCKET.M in "Build Phases" in "Compile sources", add parameter-fobj-arc

3) Introduction of Gcdasyncsocket required framework, cfnetwork and security these two

This allows you to use the Gcdasyncsocket library in my No-arc project. The following is the client I wrote to connect Echoserver

-(void) initnetwork{

socket= [[Gcdasyncsocketalloc] InitWithDelegate:selfdelegateQueue:dispatch_get_main_queue ()];

Nserror*error = nil;

if (![ socketconnecttohost:@ "Server IP" onPort: Server port Error:&error]) {

NSLog (@ "is aready connected");



-(void) senddata{

NSLog (@ "Send Message to Server");

Nsdata*datas = [@ "Client send msg" datausingencoding:nsutf8stringencoding];

[Socket Writedata:datas withtimeout:-1 tag:1];//Here the tag is useless

[Socket readdatawithtimeout:-1 tag:1];//This sentence must be added, or the server will not receive the return data


-(void) Socket: (gcdasyncsocket*) sock didconnecttohost: (nsstring*) host port: (uint16_t) port{

NSLog (@ "connected to the server");


-(void) Socket: (gcdasyncsocket*) sock didreaddata: (nsdata*) Data withtag: (long) tag{

Nsstring*msg = [[Nsstringalloc] Initwithdata:data encoding:nsutf8stringencoding];

NSLog (@ "Return message=%@", msg);


-(void) Socketdiddisconnect: (gcdasyncsocket*) sock witherror: (nserror*) err{

NSLog (@ "Close connected");


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Cocoaasyncsocket using Notes

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: 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.