-
Notifications
You must be signed in to change notification settings - Fork 1
GeneralDocumentation
If you're a beginner to networking, you may want to start by reading the Intro page.
This page provides an overview of each library available in the project, along with information about how to initialize them, configure them, use them, etc. It also points out common sources of confusion, and things to watch out for.
TCP: There are two separate libraries available for TCP: GCDAsyncSocket and AsyncSocket. AsyncSocket is the older of the two. GCDAsyncSocket is newer and faster and thread-safe. We generally recommend GCDAsyncSocket.
The most common way to initialize an instance is simply like this:
socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
The delegate and delegate_queue are required in order for GCDAsyncSocket to invoke your delegate methods. The code above specifies "self" as the delegate, and instructs the library to invoke all delegate methods on the main thread.
Setting a delegate is likely a familiar operation. However, providing a delegateQueue may be a new concept. Most typical libraries are single-threaded. When it's time to invoke a delegate method, they just call it. The libraries assume your delegate code is also single-thread. Or the libraries may be multi-threaded internally, but they assume your delegate code is only single-threaded, and designed to run only on the main thread. So they simply always invoke all delegate methods on the main thread.
GCDAsyncSocket, on the other hand, was designed for performance. It allows you receive delegate callbacks on dedicated gcd queues of your choosing. This allows it to be used in high-performance servers, and can support thousands upon thousands of concurrent connections. But it also helps in typical applications. Want your UI to be a bit snappier? Ever considered moving that network processing code off the UI thread? Even today's mobile devices have multiple CPU cores... perhaps it's time to start taking advantage of them.