1、AsyncSocket

  • 基于 CFSocket、GCD 进行的封装(OC)。

  • 支持 TCP 和 UDP。

  • 完整的回调函数(用于处理各种回调事件,连接成功,断开连接,收到数据等)。

  • 需要注意的问题:

    • 1、Socekt 连接成功回调方法中主动调用:[self.socket readDataWithTimeout:-1 tag:0];,相当于主动添加一个读取请求,不然不会执行读取信息回调方法。
    • 2、读取信息回调方法中,读取完信息后,主动调用一下 [self.socket readDataWithTimeout:-1 tag:0];,读取完信息后,重新向队列中添加一个读取请求,不然当收到信息后不会执行读取回调方法。

2、基本使用

2.1 Client 客户端

  • TCP 客户端

    	#import "GCDAsyncSocket.h"
    
    	@interface ViewController () <GCDAsyncSocketDelegate>
    
    	@property (weak, nonatomic) IBOutlet UITextField *addrTF;
    @property (weak, nonatomic) IBOutlet UITextField *portTF; @property (weak, nonatomic) IBOutlet UITextField *msgTF;
    @property (weak, nonatomic) IBOutlet UITextView *logTV; @property (nonatomic, strong) GCDAsyncSocket *clientSockfd; @end @implementation ViewController - (void)viewDidLoad {
    [super viewDidLoad]; // 创建 Socket,在主队列中处理,所有的回执都在主队列中执行。
    self.clientSockfd = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    } - (IBAction)connectBtnClick:(id)sender { NSError *error = nil; // Socket 连接
    if (![self.clientSockfd isConnected]) {
    [self.clientSockfd connectToHost:_addrTF.text onPort:_portTF.text.intValue error:&error];
    } if (error != nil) {
    [self showLogMessage:@"连接失败..."];
    }
    } #pragma mark - GCDAsyncSocketDelegate 协议方法 // 连接成功,协议方法
    - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port { // 读取数据,必须添加,相当于主动添加一个读取请求,不然不会执行读取信息回调方法
    [self.clientSockfd readDataWithTimeout:-1 tag:0]; [self showLogMessage:[NSString stringWithFormat:@"连接服务器地址:%@, 端口:%d 成功", host, port]];
    [self showLogMessage:[NSString stringWithFormat:@"本地地址:%@, 端口:%d", sock.localHost, sock.localPort]];
    } // 已经断开链接,协议方法
    - (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err { [self showLogMessage:@"socket 断开连接..."];
    } // 读取到数据,协议方法
    - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { // 注意:要想长连接,必须还要在 DidReceiveData 的 delegate 中再写一次 [_udpSocket receiveOnce:&error] // 读取数据,读取完信息后,重新向队列中添加一个读取请求,不然当收到信息后不会执行读取回调方法。
    [self.clientSockfd readDataWithTimeout:-1 tag:0]; NSString *strMsg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    [self showLogMessage:[NSString stringWithFormat:@"recv:%@", strMsg]];
    } #pragma mark - 发送数据 - (IBAction)sendBtnClick:(id)sender { // Socket 发送数据
    [self.clientSockfd writeData:[_msgTF.text dataUsingEncoding:NSUTF8StringEncoding] withTimeout:30 tag:0]; [self showLogMessage:[NSString stringWithFormat:@"send:%@", _msgTF.text]];
    } // 显示信息
    - (void)showLogMessage:(NSString *)msg { _logTV.text = [_logTV.text stringByAppendingFormat:@"%@\n", msg];
    } // 键盘回收
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
    } @end

2.2 Server 服务端

  • TCP 服务端

    	#import <arpa/inet.h>
    #import <ifaddrs.h> #import "GCDAsyncSocket.h" @interface ViewController () <GCDAsyncSocketDelegate> @property (weak, nonatomic) IBOutlet UITextField *addrTF;
    @property (weak, nonatomic) IBOutlet UITextField *portTF; @property (weak, nonatomic) IBOutlet UITextField *msgTF;
    @property (weak, nonatomic) IBOutlet UITextView *logTV; @property (nonatomic, strong) GCDAsyncSocket *serverSocketfd;
    @property (nonatomic, strong) GCDAsyncSocket *clientSocketfd; @end @implementation ViewController - (void)viewDidLoad {
    [super viewDidLoad]; NSString *ip_addr = [self getIPAddress];
    _addrTF.text = ip_addr; // 创建 Socket,在主队列中处理,所有的回执都在主队列中执行。
    self.serverSocketfd = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    } - (IBAction)listenBtnClick:(id)sender { NSError *error = nil; // Socket 监听
    [self.serverSocketfd acceptOnPort:_portTF.text.intValue error:&error]; if (error != nil) {
    NSLog(@"监听出错:%@", error);
    } else{
    [self showLogMessage:@"正在监听..."];
    }
    } #pragma mark - GCDAsyncSocketDelegate 协议方法 // 接收到连接请求,协议方法
    - (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket { // 读取数据,必须添加,相当于主动添加一个读取请求,不然不会执行读取信息回调方法
    [newSocket readDataWithTimeout:-1 tag:0]; [self showLogMessage:@"收到客户端连接...."];
    [self showLogMessage:[NSString stringWithFormat:@"客户端地址:%@, 端口:%d", newSocket.connectedHost, newSocket.connectedPort]]; self.clientSocketfd = newSocket;
    } // 已经断开链接,协议方法
    - (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err { [self showLogMessage:@"socket 断开连接..."];
    } // 读取到数据,协议方法
    - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { // 读取数据,读取完信息后,重新向队列中添加一个读取请求,不然当收到信息后不会执行读取回调方法。
    [sock readDataWithTimeout:-1 tag:0]; NSString *strMsg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    [self showLogMessage:[NSString stringWithFormat:@"recv:%@",strMsg]];
    } #pragma mark - 发送数据 - (IBAction)sendBtnClick:(id)sender { // Socket 发送数据
    [self.clientSocketfd writeData:[_msgTF.text dataUsingEncoding:NSUTF8StringEncoding] withTimeout:30 tag:0]; [self showLogMessage:[NSString stringWithFormat:@"send:%@", _msgTF.text]];
    } #pragma mark - 获取本地 IP 地址 - (NSString *)getIPAddress { NSString *address = @"error";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0; // retrieve the current interfaces - returns 0 on success
    success = getifaddrs(&interfaces); if (success == 0) { // Loop through linked list of interfaces
    temp_addr = interfaces; while (temp_addr != NULL) { if (temp_addr->ifa_addr->sa_family == AF_INET) { // Check if interface is en0 which is the wifi connection on the iPhone
    if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) { // Get NSString from C String
    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
    }
    }
    temp_addr = temp_addr->ifa_next;
    }
    } // Free memory
    freeifaddrs(interfaces);
    return address;
    } // 显示信息
    - (void)showLogMessage:(NSString *)msg { _logTV.text = [_logTV.text stringByAppendingFormat:@"%@\n", msg];
    } // 键盘回收
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
    } @end

2.3 UDP 通信

  • UDP 通信

    	#import <arpa/inet.h>
    #import <ifaddrs.h> #import "GCDAsyncUdpSocket.h" @interface ViewController () <GCDAsyncUdpSocketDelegate> @property (weak, nonatomic) IBOutlet UITextField *desAddrTF;
    @property (weak, nonatomic) IBOutlet UITextField *desPortTF; @property (weak, nonatomic) IBOutlet UITextField *locAddrTF;
    @property (weak, nonatomic) IBOutlet UITextField *locPortTF; @property (weak, nonatomic) IBOutlet UITextField *msgTF;
    @property (weak, nonatomic) IBOutlet UITextView *logTV; @property (nonatomic, strong) GCDAsyncUdpSocket *udpSocketfd; @end @implementation ViewController - (void)viewDidLoad {
    [super viewDidLoad]; NSString *ip_addr = [self getIPAddress];
    _locAddrTF.text = ip_addr; // 创建 Socket,在主队列中处理,所有的回执都在主队列中执行
    self.udpSocketfd = [[GCDAsyncUdpSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
    } - (IBAction)connectBtnClick:(id)sender { NSError *error = nil; // 绑定端口
    [self.udpSocketfd bindToPort:_locPortTF.text.intValue error:&error]; if (error != nil) {
    NSLog(@"绑定端口出错:%@", error);
    return;
    } else{
    [self showLogMessage:[NSString stringWithFormat:@"绑定端口 %d 成功...", _locPortTF.text.intValue]];
    } // 开始接收数据
    [self.udpSocketfd beginReceiving:&error]; if (error != nil) {
    NSLog(@"开始接收数据出错:%@", error);
    } else{
    [self showLogMessage:@"开始接收数据..."];
    }
    } #pragma mark - GCDAsyncUdpSocketDelegate 协议方法 // 接收到的数据,协议方法
    - (void)udpSocket:(GCDAsyncUdpSocket *)sock didReceiveData:(NSData *)data
    fromAddress:(NSData *)address
    withFilterContext:(id)filterContext { NSString *strMsg = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; [self showLogMessage:[NSString stringWithFormat:@"recv:%@", strMsg]];
    } #pragma mark - 发送数据 - (IBAction)sendBtnClick:(id)sender { // Socket 发送数据
    [self.udpSocketfd sendData:[_msgTF.text dataUsingEncoding:NSUTF8StringEncoding]
    toHost:_desAddrTF.text
    port:_desPortTF.text.intValue
    withTimeout:30 tag:0]; [self showLogMessage:[NSString stringWithFormat:@"send:%@", _msgTF.text]];
    } #pragma mark - 获取本地 IP 地址 - (NSString *)getIPAddress { NSString *address = @"error";
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0; // retrieve the current interfaces - returns 0 on success
    success = getifaddrs(&interfaces); if (success == 0) { // Loop through linked list of interfaces
    temp_addr = interfaces; while (temp_addr != NULL) { if (temp_addr->ifa_addr->sa_family == AF_INET) { // Check if interface is en0 which is the wifi connection on the iPhone
    if ([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) { // Get NSString from C String
    address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
    }
    }
    temp_addr = temp_addr->ifa_next;
    }
    } // Free memory
    freeifaddrs(interfaces);
    return address;
    } // 显示信息
    - (void)showLogMessage:(NSString *)msg { _logTV.text = [_logTV.text stringByAppendingFormat:@"%@\n", msg];
    } // 键盘回收
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
    } @end

2.4 Socket 长连接

  • PublicTool.h

    	@interface PublicTool : NSObject
    
    	// 字典转换成 JSON 字符串
    + (NSString *)JSONStringWithDic:(NSDictionary *)dic; // JSON 字符串转换成字典
    + (NSDictionary *)dictionaryWithJSON:(NSString *)json; @end
  • PublicTool.m

    	@implementation PublicTool
    
    	// 字典转成成 JSON 字符串
    + (NSString *)JSONStringWithDic:(NSDictionary *)dic { NSError *error = nil; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic
    options:0
    error:&error]; if (jsonData == nil) {
    NSLog(@"fail to get JSON from dictionary: %@, error: %@", self, error); return nil;
    }
    NSString *jsonString = [[NSString alloc] initWithData:jsonData
    encoding:NSUTF8StringEncoding]; return jsonString;
    } // JSON 字符串转换成字典
    + (NSDictionary *)dictionaryWithJSON:(NSString *)json { NSError *error = nil; NSData *jsonData = [json dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData
    options:NSJSONReadingMutableContainers |
    NSJSONReadingAllowFragments
    error:&error]; if (jsonDict == nil) {
    NSLog(@"fail to get dictioanry from JSON: %@, error: %@", json, error); return nil;
    } return jsonDict;
    } @end
  • SocketSingleTon.h

    	@interface SocketSingleTon : NSObject
    
    	@property (nonatomic, copy) NSString *hostAddr;
    @property (nonatomic, copy) NSString *port; @property (nonatomic, copy) void(^msgLog)(NSString *); + (instancetype)shareInstance; // 连接到服务器
    - (void)connectToServer; // 断开连接
    - (void)cutOffSocket; // 发送数据到服务器
    - (void)sendDataToServer:(NSData*)data; @end
  • SocketSingleTon.m

    	#import "GCDAsyncSocket.h"
    #import <netdb.h>
    #import <arpa/inet.h> #import "PublicTool.h" typedef NS_ENUM(NSInteger, SocketOffline) {
    SocketOfflineByServer,
    SocketOfflineByUser
    }; @interface SocketSingleTon () <GCDAsyncSocketDelegate> @property (nonatomic, strong) GCDAsyncSocket *socket; @property (nonatomic, strong) NSTimer *beatTimer; @end @implementation SocketSingleTon + (instancetype)shareInstance { static SocketSingleTon *shareInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    shareInstance = [[self alloc] init];
    });
    return shareInstance;
    } // 连接到服务器
    - (void)connectToServer { NSError *error = nil; if (self.socket != nil) { if ([self.socket isConnected]) { // 断开后再连接
    self.socket.userData = @(SocketOfflineByUser);
    [self cutOffSocket]; [self.socket connectToHost:self.hostAddr onPort:self.port.intValue error:&error]; } else { [self.socket connectToHost:self.hostAddr onPort:self.port.intValue error:&error];
    } } else {
    self.socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; [self.socket connectToHost:self.hostAddr onPort:self.port.intValue error:&error];
    } if (error != nil) {
    NSLog(@"socket 连接失败:%@", error);
    } else {
    NSLog(@"socket 连接成功");
    }
    } // 断开连接
    - (void)cutOffSocket { self.socket.userData = @(SocketOfflineByUser);
    [self.socket disconnect];
    } #pragma mark - GCDAsyncSocketDelegate 协议方法 // 连接成功,协议方法
    - (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port { [sock readDataWithTimeout:-1 tag:0]; NSString *logStr = [NSString stringWithFormat:@"连接主机:%@:%d 成功\n", host, port];
    NSLog(@"%@", logStr); if (self.msgLog) {
    self.msgLog(logStr);
    } // 创建定时器,定时发送 beat 包
    self.beatTimer = [NSTimer scheduledTimerWithTimeInterval:5
    target:self
    selector:@selector(longConnectToServer)
    userInfo:nil
    repeats:YES];
    } // 连接断开,协议方法
    - (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err { self.socket = nil; [self.beatTimer invalidate];
    self.beatTimer = nil; if ([sock.userData isEqual: @(SocketOfflineByUser)]) { NSLog(@"the socket have been cutted off by user"); if (self.msgLog) {
    self.msgLog(@"the socket have been cutted off by user");
    } } else if (sock.userData == SocketOfflineByServer) { NSLog(@"the socket have been cutted off by server"); if (self.msgLog) {
    self.msgLog(@"the socket have been cutted off by server");
    } // reconnect
    [self connectToServer]; } else { NSLog(@"%@", err.localizedDescription); if (self.msgLog) {
    self.msgLog([NSString stringWithFormat:@"%@", err.localizedDescription]);
    } [self connectToServer];
    }
    } // 收到消息,协议方法
    - (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { [sock readDataWithTimeout:-1 tag:0]; char buf[1024];
    [data getBytes:buf length:1024]; NSString *receivedData = [NSString stringWithCString:buf encoding:NSUTF8StringEncoding];
    NSLog(@"receivedData:%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); if (receivedData.length > 0) { NSDictionary *dataDic = [PublicTool dictionaryWithJSON:receivedData]; if ([dataDic[@"msgType"] isEqualToString:@"beta"]) {
    NSString *strMsg = [NSString stringWithFormat:@"收到心跳确认的数据:%@\n", receivedData];
    if (self.msgLog) {
    self.msgLog(strMsg);
    }
    } else if ([dataDic[@"msgType"] isEqualToString:@"normal"]) {
    NSString *strMsg = [NSString stringWithFormat:@"收到正常的数据:%@\n", receivedData];
    if (self.msgLog) {
    self.msgLog(strMsg);
    }
    } else if ([dataDic[@"msgType"] isEqualToString:@"exit"]) {
    NSString *strMsg = [NSString stringWithFormat:@"收到关闭的数据:%@\n", receivedData];
    if (self.msgLog) {
    self.msgLog(strMsg);
    }
    [self cutOffSocket];
    }
    }
    } // 发送数据
    - (void)longConnectToServer {
    [self sendDataToServer:[@"hello" dataUsingEncoding:NSUTF8StringEncoding]];
    } // 发送数据到服务器
    - (void)sendDataToServer:(NSData*)data { NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSMutableDictionary *dicParams = [NSMutableDictionary dictionary]; if ([dataStr isEqualToString:@"hello"]) {
    [dicParams setValue:dataStr forKey:@"msg"];
    [dicParams setValue:@"beta" forKey:@"msgType"];
    } else {
    [dicParams setValue:dataStr forKey:@"msg"];
    [dicParams setValue:@"normal" forKey:@"msgType"];
    } NSData *sendData = [[PublicTool JSONStringWithDic:dicParams] dataUsingEncoding:NSUTF8StringEncoding]; NSString *strMsg = [NSString stringWithFormat:@"发送数据: %@\n", [PublicTool JSONStringWithDic:dicParams]];
    if (self.msgLog) {
    self.msgLog(strMsg);
    } [self.socket writeData:sendData withTimeout:30 tag:0];
    } @end
  • ViewController.m

    	#import "SocketSingleTon.h"
    #import "PublicTool.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UITextField *addressTF;
    @property (weak, nonatomic) IBOutlet UITextField *portTF; @property (weak, nonatomic) IBOutlet UITextField *msgTF;
    @property (weak, nonatomic) IBOutlet UITextView *logTV; @end @implementation ViewController - (IBAction)connectToServer:(id)sender { // 创建 Socket
    SocketSingleTon *socketInstance = [SocketSingleTon shareInstance]; socketInstance.hostAddr = _addressTF.text;
    socketInstance.port = _portTF.text; __weak __typeof (self)weakSelf = self;
    socketInstance.msgLog = ^(NSString *log){ dispatch_async(dispatch_get_main_queue(), ^{
    weakSelf.logTV.text = [weakSelf.logTV.text stringByAppendingString:log];
    });
    }; // 连接到服务器
    [socketInstance connectToServer];
    } - (IBAction)cutOffConnect:(id)sender { SocketSingleTon *socketInstance = [SocketSingleTon shareInstance]; // 断开连接
    [socketInstance cutOffSocket];
    } - (IBAction)sendDataToServer:(id)sender { SocketSingleTon *socketInstance = [SocketSingleTon shareInstance]; // 发送数据到服务器
    [socketInstance sendDataToServer:[_msgTF.text dataUsingEncoding:NSUTF8StringEncoding]];
    } - (IBAction)sendBetaDataToServer:(id)sender { SocketSingleTon *socketInstance = [SocketSingleTon shareInstance]; NSMutableDictionary *dicParams = [NSMutableDictionary dictionary];
    [dicParams setValue:@"beta" forKey:@"msgType"];
    [dicParams setValue:@"hello" forKey:@"msg"];
    NSString *strMsg = [PublicTool JSONStringWithDic:dicParams]; // 发送心跳数据到服务器
    [socketInstance sendDataToServer:[strMsg dataUsingEncoding:NSUTF8StringEncoding]];
    } - (IBAction)clearLog:(id)sender {
    _logTV.text = nil;
    } // 键盘回收
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
    } @end

iOS - AsyncSocket 的使用的更多相关文章

  1. IOS AsyncSocket

    导入AsyncSocket.h  AsyncSocket.m   AsyncUdpSocket.h   AsyncUdpSocket.m   以及  CFNetWork.framework async ...

  2. iOS开发之AsyncSocket使用教程

    用socket可以实现像QQ那样发送即时消息的功能.客户端和服务端需要建立长连接,在长连接的情况下,发送消息.客户端可以发送心跳包来检测长连接. 在iOS开发中使用socket,一般都是用第三方库As ...

  3. iOS开发之即时通讯之Socket(AsyncSocket)

    1.AsyncSocket介绍 如果需要在项目中像QQ微信一样做到即时通讯,必须使用socket通讯. iOS中Socket编程的方式: BSD Socket: BSD Socket 是UNIX系统中 ...

  4. iOS学习之Socket使用简明教程- AsyncSocket

    转载自:http://my.oschina.net/joanfen/blog/287238 如果需要在项目中像QQ微信一样做到即时通讯,必须使用socket通讯,本人也是刚学习,分享一下,有什么不对的 ...

  5. IOS使用Asyncsocket进行socket编程

    iphone的标准推荐CFNetwork C库编程.但是编程比较烦躁.在其它OS往往用类来封装的对Socket函数的处理.比如MFC的CAsysncSocket.在iphone也有类似于开源项目.co ...

  6. iOS socket编程 第三方库 AsyncSocket(GCDAsyncSocket)

    Socket描述了一个IP.端口对.它简化了程序员的操作,知道对方的IP以及PORT就可以给对方发送消息,再由服务器端来处理发送的这些消息.所以,Socket一定包含了通信的双发,即客户端(Clien ...

  7. iOS Socket第三方开源类库 ----AsyncSocket

    假如你也是一个java程序员,而你又不是很懂Socket. 下面我的这篇文章也许能帮助你一些. http://xiva.iteye.com/blog/993336 首先我们写好上面文章中的server ...

  8. iOS Socket第三方开源类库 ----AsyncSocket 分类: ios相关 ios技术 2015-03-11 22:14 59人阅读 评论(0) 收藏

    假如你也是一个java程序员,而你又不是很懂Socket. 下面我的这篇文章也许能帮助你一些. http://xiva.iteye.com/blog/993336 首先我们写好上面文章中的server ...

  9. IOS socket编程--Asyncsocket

    iPhone的标准推荐是CFNetwork 库编程,其封装好的开源库是 cocoa AsyncSocket库,用它来简化CFNetwork的调用,它提供了异步操作 主要特性有: 队列的非阻塞的读和写, ...

随机推荐

  1. Objective-C:MRC(引用计数器)获得对象所有权的方式(init、retain、copy等)

    .h声明文件 // Integer.h // 02-MRC // // Created by ma c on 15/8/13. // Copyright (c) 2015年. All rights r ...

  2. Cesium随笔(2)加载天地图地图服务【转】

    Cesium自带的图层是bing地图和esri,mapbox等图层,木有中文注记,想加载中文注记的图层?废话不说,上代码: var viewer = new Cesium.Viewer('cesium ...

  3. JNI 详细使用 基础【步骤】

    1.定义本地[native]方法.通常情况下,应单独定义一个类来封装所有native方法.native方法相当于一个[接口]中的方法,只有方法声明,没有方法体. 2.在项目根目录下创建[jni文件夹] ...

  4. Codeforces Round #256 (Div. 2/A)/Codeforces448A_Rewards(水题)

    解题报告 意思就是说有n行柜子,放奖杯和奖牌.要求每行柜子要么全是奖杯要么全是奖牌,并且奖杯每行最多5个,奖牌最多10个. 直接把奖杯奖牌各自累加,分别出5和10,向上取整和N比較 #include ...

  5. 动态改变UIPopupList选项(NGUI)

    NGUI的UIPopupList 可以通过修改items属性来动态改变菜单选项: public class popListvahnge : MonoBehaviour { public UIPopup ...

  6. Mali GPU OpenGL ES 应用性能优化--測试+定位+优化流程

    1. 使用DS-5 Streamline定位瓶颈 DS-5 Streamline要求GPU驱动启用性能測试,在Mali GPU驱动中激活性能測试对性能影响微不足道. 1.1 DS-5 Streamli ...

  7. C++ RegCreateKeyEx成功了,但是注册表并没有这一项

    C++ - RegCreateKeyEx success but without result Could anybody tell me what's wrong is with this code ...

  8. WebClient.DownloadData突然失灵

      有如下的代码: try { byte[] acsMetadata; using (WebClient webClient = new WebClient()) { acsMetadata = we ...

  9. Linux内核二层数据包接收流程

    本文主要讲解了Linux内核二层数据包接收流程,使用的内核的版本是2.6.32.27 为了方便理解,本文采用整体流程图加伪代码的方式从内核高层面上梳理了二层数据包接收的流程,希望可以对大家有所帮助.阅 ...

  10. 为什么学习Python及Python环境安装

    大部分人在工作中可能是以c/c++.java之类的语言为主.这也可能是我们接触的第一个开发语言,这类语言一般有丰富地类库.高效地运行速率.灵活地组合控制,须要经过编译在运行.适用于大型的项目proje ...