tag參数是为了在回调方法中匹配发起调用的方法的,不会加在数据传输中。

调用write方法,等待接收消息。收到消息后,会回调didReadData的delegate方法,

delegate方法中的tag和发起read的方法中的tag是相应的。

- (void)readDataWithTimeout:(NSTimeInterval)timeout tag:(long)tag;

- (void)onSocket:(AsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag;





write和read是一样:writeData方法和相应的delegate方法didWriteDataWithTag的tag是相应的。

- (void)writeData:(NSData *)data withTimeout:(NSTimeInterval)timeout tag:(long)tag;

- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag;

需注意的一点是:发送时的tag和接收时的tag是无关的。

以read为例分析:

- (void)readDataWithTimeout:(NSTimeInterval)timeout tag:(long)tag

上面的方法会生成一个数据类:AsyncReadPacket,此类中包括tag,并把此对象放入数组theReadQueue中。

在CFStream中的回调方法中,会取theReadQueue最新的一个,在回调方法中取得tag,并将tag传

给回调方法:

- (void)onSocket:(AsyncSocket *)sock didWriteDataWithTag:(long)tag;

如此而已。

官方解释:

In addition to this you've probably noticed the tag parameter. The tag you pass during the read/write operation is passed back to you via the delegate method once the read/write operation completes.
It does not get sent over the socket or read from the socket.
It is designed to help simplify the code in your delegate method. For example, your delegate method might look like this:





#define TAG_WELCOME 10

#define TAG_CAPABILITIES 11

#define TAG_MSG 12





... 





- (void)socket:(AsyncSocket *)sender didReadData:(NSData *)data withTag:(long)tag

{

    if (tag == TAG_WELCOME)

    {

        // Ignore welcome message

    }

    else if (tag == TAG_CAPABILITIES)

    {

        [self processCapabilities:data];

    }

    else if (tag == TAG_MSG)

    {

        [self processMessage:data];

    }

AsyncSocket中tag參数的用处的更多相关文章

  1. EBS OAF开发中实现參数式弹出窗体

    EBS OAF开发中实现參数式弹出窗体 (版权声明,本人原创或者翻译的文章如需转载,如转载用于个人学习,请注明出处:否则请与本人联系,违者必究) 概览 參数式弹出窗体和嵌入式弹出窗体不一样,它拥有独立 ...

  2. javascript获取当前url中的參数

    javascript获取当前页面url中的參数能够使用location的search方法,获取到的是url中?后面的部分,比如http:localhost:8080/Manager/index.jsp ...

  3. tcp/ip协议listen函数中backlog參数的含义

    listen函数的定义例如以下所看到的: #include <sys/socket.h> int accept(int sockfd, struct sockaddr * restrict ...

  4. mysqladmin在SuSE linux系统中--sleep參数使用不准确问题

    我们都知道,在MySQL中.能够使用mysqladmin命令的extended-status选项来查看MySQL的执行状态,比方获取我们经常关注的几个值: # mysqladmin -uroot -p ...

  5. C++ 中获取 可变形參函数中的參数

    #include <iostream> #include <stdarg.h> using namespace std; int ArgFunc(const char * st ...

  6. 再次学习javascript中的參数传递

     javascript中的全部函数的參数传递都是依照值传递的,做了以下測试:    function addTen(num){ num +=10; return num; } var count = ...

  7. CSS3中transition-duration參数对hover前后两种过渡时间的影响

    transition-duration这个參数是设置过渡时间的,将transition-duration放在哪个类中.那么在这个类被启用时就会依照transition-duration设定的时间来过渡 ...

  8. REST技术第二步 获取URL中的參数

    获取请求的參数.rest技术相对于servlet来说要方便很多. Servlet我们要获取请求的參数,非常麻烦啊.须要request.getParameter("").假设我们要的 ...

  9. struts开发&lt;struts中的參数传递.三&gt;

    不说废话,直接上干货 1.通过set和get传递參数 添加username 和password两个属性并添加set和get方法 package fzl.user.struts.demo; import ...

随机推荐

  1. SFM作业

    代码:https://github.com/jianxiongxiao/SFMedu PPT:http://3dvision.princeton.edu/courses/SFMedu/slides.p ...

  2. 遇到的django问题

    问题1: No migrations to apply 删除了migrations中0001_initial.py文件,重新执行 python manage.py makemigrations pyt ...

  3. CF633H Fibonacci-ish II

    题目描述 题解: 坑题搞了三天. 莫队+线段树. 还有一些和斐波那契数列有关的性质. 首先答案是$a_1f_1+a_2f_2+…+a_nf_n$, 考虑插进去一个元素对答案产生的影响. 比如插进去一个 ...

  4. [模板] LIS

    树状数组优化LIS到nlogn,网上找了好多,感觉讲得都不是很明白,正好自己复习整理一下. 基本的DP方程 f[i]=max(f[i],f[j]+1) (j<i且a[j]<a[i]) 定义 ...

  5. Python自动化测试框架——概述

    #使用import import unittest #测试用例TestCase ''' 一个测试用例时一个完整的测试流程,包括了环境准备SetUp,测试执行Run,测试环境还原TearDown 一个测 ...

  6. curl 模拟post请求

    curl -H "Content-Type:application/json" -X POST --data '{"openId":"xxxxxxx& ...

  7. InnoDB INFORMATION_SCHEMA FULLTEXT Index Tables

    InnoDB INFORMATION_SCHEMA FULLTEXT Index Tables 下表提供了FULLTEXT索引的元数据: mysql> SHOW TABLES FROM INFO ...

  8. 【HIHOCODER 1513】 小Hi的烦恼 (BitSet)

    描述 小Hi从小的一大兴趣爱好就是学习,但是他发现尽管他认真学习,依旧有学神考的比他好. 小Hi在高中期间参加了市里的期末考试,一共五门:语文.数学.英语.物理.化学. 成绩出来之后,小Hi发现有些同 ...

  9. python 中range和xrange的区别

    range() 相当于直接构造一个列表,而xrange() 是返回一个迭代值. range用法: range( 开始值,结束值,步长) 在需要大量迭代的时候,比较适合使用xrange()

  10. 性能测试工具 - ab 简单应用

    之前知道一般网站性能可以通过 LoadRunner, JMeter, QTP 等相应的软件进行测试, 印象中本科学习 “软件测试” 这门课程时安装并使用过, LoadRunner等不是一个小软件, 安 ...