CocoaAsyncSocket

repository·master·Indexed 11 days ago

https://github.com/robbiehanson/cocoaasyncsocket

Asynchronous socket libraries for macOS, iOS, and tvOS built on Grand Central Dispatch (GCD). Includes GCDAsyncSocket for TCP/IP and GCDAsyncUdpSocket for UDP/IP, featuring non-blocking I/O, IPv4/IPv6 support, TLS/SSL security, and full delegate support for connection and data events.

Tokens
1K
Snippets
4
Records
8
Agent score
46%

What's inside CocoaAsyncSocket

  1. What is GCDAsyncSocket (TCP)?

    master

    GCDAsyncSocket is a TCP/IP socket networking library built on top of Grand Central Dispatch (GCD). It is designed to be fully self-contained and thread-safe, running entirely within its own GCD dispatch queue.

    Key capabilities include:

    • Full Delegate Support: Receives callbacks for errors, connections, read/write completions, progress, and disconnections.
    • Non-blocking I/O: Supports queued non-blocking reads and writes with optional timeouts, including automatic buffering and termination sequence searching.
    • Automatic Server Management: Automatically accepts incoming connections and provides new instances of the socket for each connection.
    • IPv4/IPv6 Support: Automatically handles both protocols.
    • Security: Supports TLS/SSL for both client and server sockets via a single method call.
  2. What is GCDAsyncUdpSocket (UDP)?

    master

    GCDAsyncUdpSocket is a UDP/IP socket networking library built on top of Grand Central Dispatch (GCD). It is fully thread-safe and runs within its own dispatch queue.

    Key capabilities include:

    • Full Delegate Support: Receives callbacks for errors, send/receive completions, and disconnections.
    • Non-blocking I/O: Supports queued non-blocking send and receive operations with optional timeouts, handling buffering and errno automatically.
    • IPv4/IPv6 Support: Automatically handles both protocols for sending and receiving.
  3. Specify a network interface for AsyncSocket connections

    master

    By default, CocoaAsyncSocket uses the primary network interface (WiFi on iPhone, or the system-configured primary interface on Mac).

    • Server sockets (listening/accepting) will accept incoming connections on any available interface.
    • Client sockets (connecting) will attempt outgoing connections on the primary interface.

    You can override this behavior to force a connection through a specific interface, such as WiFi, Cellular (3G), Bluetooth, or Loopback. This is useful for:

    • Ensuring a connection only uses WiFi (e.g., to save cellular data).
    • Making Bluetooth connections.
    • Creating local-only server sockets on Mac for inter-process communication (using the Loopback interface).
  4. Import CocoaAsyncSocket in Objective-C or Swift

    master

    Depending on your language and whether you are using Clang Modules, use the following import statements:

    Swift

    import CocoaAsyncSocket

    Objective-C (with Clang Modules)

    @import CocoaAsyncSocket;

    Objective-C (without Clang Modules)

    #import "GCDAsyncSocket.h" // for TCP
    #import "GCDAsyncUdpSocket.h" // for UDP
  5. Install CocoaAsyncSocket via Carthage

    master

    To include CocoaAsyncSocket in your project using Carthage, add the following line to your Cartfile:

    github "robbiehanson/CocoaAsyncSocket" "master"

    After building, the frameworks are located at:

    • Carthage/Build/iOS/CocoaAsyncSocket.framework
    • Carthage/Build/tvOS/CocoaAsyncSocket.framework
    • Carthage/Build/Mac/CocoaAsyncSocket.framework

    Drag the appropriate framework into your project.

  6. Install CocoaAsyncSocket via CocoaPods

    master

    To install CocoaAsyncSocket using CocoaPods, add the following line to your Podfile. If you are targeting iOS 8+ or using Swift, ensure you also include use_frameworks!.

    use_frameworks! # Add this if you are targeting iOS 8+ or using Swift
    pod 'CocoaAsyncSocket'