GCDAsyncSocket

29 篇文章 0 订阅
2 篇文章 0 订阅

Asynchronous socket networking library for Mac and iOS

对于Mac和IOS的异步Socket

CocoaAsyncSocket provides easy-to-use and powerful asynchronous socket libraries for Mac and iOS. The classes are described below.

CocoaAsyncSocket为Mac和IOS提供了简单易用和功能强大的异步Socket库。这些类和描述如下所示。

TCP

GCDAsyncSocket and AsyncSocket are TCP/IP socket networking libraries. Here are the key features available in both:

GCDAAsyncSocket和AsyncSocket都是TCP/IP套接字网络库。这里是关键的特性可用于以下情况

  • Native objective-c, fully self-contained in one class.
  • Native objective-c,完全包含一个类里

  No need to muck around with sockets or streams. This class handles everything for you.

  不需要混淆Socket套接字和流。这个类为你提供了一切。

  • Full delegate support
  • 全delegate支持

  Errors, connections, read completions, write completions, progress, and disconnections all result in a call to your delegate method.

  错误,连接,读完成,写完成,处理和断开连接的所有结果都在你调用的delegate委托的方法中。

  • Queued non-blocking reads and writes, with optional timeouts.
  • 读写无堵塞排队,可选超时

  You tell it what to read or write, and it handles everything for you. Queueing, buffering, and searching for termination sequences within the stream - all handled for you automatically.

  你告诉它去读写什么,它将为你处理一切。排列、缓存和在流内寻找终止序列--所有的一切都将为你自动完成

  • Automatic socket acceptance.
  • Socket自动接收验证

  Spin up a server socket, tell it to accept connections, and it will call you with new instances of itself for each connection.

  启动一个Socket服务器,告诉它一个接收连接,它将自己为每一个连接创建一个新的连接实例。

  • Support for TCP streams over IPv4 and IPv6.
  • 支持IPV4和IPV6上得TCP流

  Automatically connect to IPv4 or IPv6 hosts. Automatically accept incoming connections over both IPv4 and IPv6 with a single instance of this class. No more worrying about multiple sockets

  自动连接到IPV4或IPV4的主机上。用一个类的单一实例自动接收一个传入的连接在IPV4和IPV6上,不用担心多个Socket。

  • Support for TLS / SSL
  • 支持TLS/SSL

  Secure your socket with ease using just a single method call. Available for both client and server sockets.

  它将确保你的Socket轻松,使用单一的一个方法调用。对于Socket客户端和服务器都适用

GCDAsyncSocket is built atop Grand Central Dispatch:

GCDAsyncSocket是建立在Grand Central Dispatch

  • Fully GCD based and Thread-Safe
  • 完全基于GCD和线程安全

  It runs entirely within its own GCD dispatch_queue, and is completely thread-safe. Further, the delegate methods are all invoked asynchronously onto a dispatch_queue of your choosing. This means parallel operation of your socket code, and your delegate/processing code.

  它完全运行在自己的GCD的调度队列上,并且它是完全线程安全的。进一步的说,委托方法在你选择的调度队列上都是异步调用的。这意味着你将平行的操作你的socket代码和委托代码。

  • The Latest Technology & Performance Optimizations
  • 最新的技术和性能优化

  Internally the library takes advantage of technologies such as kqueue's to limit system calls and optimize buffer allocations. In other words, peak performance.

   在内部,库利用诸如kqueue's限制系统调用和优化缓存区分配。总之,这是巅峰之作。

AsyncSocket wraps CFSocket and CFStream:

AsyncSocket包含CFSocket和CFStream

  Use it on the main thread or a worker thread. It plugs into the NSRunLoop with configurable modes.

   在主线程或工作线程上运用它。它使用配置模型插入到NSRunLoop中

UDP

GCDAsyncUdpSocket and AsyncUdpSocket are UDP/IP socket networking libraries. Here are the key features available in both:

GCDAsyncSocket和AsyncSocket都是UDP/IP套接字网络库。这里是关键的特性可用于以下情况:

  • Native objective-c, fully self-contained in one class.
  • Native objective-c,完全包含一个类里

  No need to muck around with low-level sockets. This class handles everything for you.

   不需要和底层套接字混淆。这些类为你提供了所有的东西

  • Full delegate support.
  • 委托支持

  Errors, send completions, receive completions, and disconnections all result in a call to your delegate method.

   错误,发送完成,接收完成和断开连接的结果都在你调用的委托方法中

  • Queued non-blocking send and receive operations, with optional timeouts.
  • 读写无堵塞排队,可选超时

  You tell it what to send or receive, and it handles everything for you. Queueing, buffering, waiting and checking errno - all handled for you automatically.

   你告诉它发送或接收什么,它为你提供所有的东西。队列,缓存,等待和检查错误--都是为你自动提供

  • Support for IPv4 and IPv6.
  • 支持IPV4和IPV6

  Automatically send/recv using IPv4 and/or IPv6. No more worrying about multiple sockets.

   自动使用IPV4和或IPV6发送和接收。不用担心多个Socket

GCDAsyncUdpSocket is built atop Grand Central Dispatch:

GCDAsyncUdpSocket是建立在Grand Central Dispatch

  • Fully GCD based and Thread-Safe
  • 完全基于GCD和线程安全
    It runs entirely within its own GCD dispatch_queue, and is completely thread-safe. Further, the delegate methods are all invoked asynchronously onto a dispatch_queue of your choosing. This means parallel operation of your socket code, and your delegate/processing code.

   它完全运行在自己的GCD的调度队列上,并且它是完全线程安全的。进一步的说,委托方法在你选择的调度队列上都是异步调用的。这意味着你将平行的操作你的socket代码和委托代码。

AsyncUdpSocket wraps CFSocket:

AsyncUdpSocket包含CFSocket:

  Use it on the main thread or a worker thread. It plugs into the NSRunLoop with configurable modes.

   在主线程或工作线程上运用它。它使用配置模型插入到NSRunLoop中

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是使用GCDAsyncSocket接收esp32发送的数据的示例代码: 1. 首先需要导入GCDAsyncSocket库 ```objc #import "GCDAsyncSocket.h" ``` 2. 创建一个GCDAsyncSocket实例,并设置代理 ```objc GCDAsyncSocket *socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; ``` 3. 在连接到esp32时,调用connectToHost方法 ```objc NSError *error = nil; if (![socket connectToHost:host onPort:port error:&error]) { NSLog(@"Error connecting: %@", error); } ``` 其中,host为esp32的IP地址,port为esp32服务器开放的端口号。 4. 实现GCDAsyncSocketDelegate中的方法,处理接收到的数据 ```objc - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { // 处理接收到的数据 NSString *receivedData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"Received data: %@", receivedData); // 继续监听数据 [sock readDataWithTimeout:-1 tag:0]; } ``` 在didReadData方法中,可以处理接收到的数据。然后,通过调用readDataWithTimeout方法,继续监听数据。 完整示例代码如下: ```objc #import "ViewController.h" #import "GCDAsyncSocket.h" @interface ViewController () <GCDAsyncSocketDelegate> @property (nonatomic, strong) GCDAsyncSocket *socket; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 创建socket实例 self.socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; // 连接esp32 NSString *host = @"esp32的IP地址"; uint16_t port = 8080; NSError *error = nil; if (![self.socket connectToHost:host onPort:port error:&error]) { NSLog(@"Error connecting: %@", error); } } #pragma mark - GCDAsyncSocketDelegate - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port { NSLog(@"Connected to host %@:%d", host, port); // 开始监听数据 [sock readDataWithTimeout:-1 tag:0]; } - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { // 处理接收到的数据 NSString *receivedData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"Received data: %@", receivedData); // 继续监听数据 [sock readDataWithTimeout:-1 tag:0]; } - (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err { NSLog(@"Disconnected from host"); } @end ``` 注意,以上代码中的host和port需要替换成实际的esp32的IP地址和端口号。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值