First go to GitHub's website to download the latest package, then take a look at the introduction. It's written in more detail.
Https://github.com/robbiehanson/CocoaAsyncSocket/wiki/Intro_GCDAsyncSocket
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