微信端口及协议分析(java、C版)_Javascript教程-查字典教程网
微信端口及协议分析(java、C版)
微信端口及协议分析(java、C版)
发布时间:2016-12-30 来源:查字典编辑
摘要:有朋友公司需求如下,手机通过WIFI连接上网,而老板要求,员工使用手机只能上微信,而不能上其他网页和看在线视频。上网搜索了微信使用协议和端口...

有朋友公司需求如下,手机通过WIFI连接上网,而老板要求,员工使用手机只能上微信,而不能上其他网页和看在线视频。上网搜索了微信使用协议和端口。

微信通过TCP方式来进行通讯。

TCP通讯方式分析

通讯端口分析

远程通讯端口范围: 80,443,80,443,8080。

HTTP方式分析

在特征分析中,发现微信会采用HTTP协议进行通讯。

连接的主机(Host)(正则表达式)为:^short.weixin.qq.com,^(mmsns|mmbiz).qpic.cn。

TLS方式分析

在特征分析中,发现微信会采用TLS协议进行通讯。

连接的TLS主机(正则表达式)为:^(wx|weixin|res.wx).qq.com。

这个协议只是用来检测和屏蔽微信的,不能监控聊天记录内容。

来源:笨驴技术

安桌手机,可以用tcpdump进行抓包。。分析!!

初步分析结果:

使用80、8080,登陆和交互。

使用:tcp 14000 端口 SCOTTY High-Speed Filetransferscotty-ft 发送语音文件

TCP: 5222 5223 5228 80 8080 443

来源:http://bbs.51cto.com/thread-1031608-1.html

微信、手机QQ网络行为简析

测试环境:

智能手机一部(打开wifi关闭gprs) —-> Linux wifi 热点 —-> 公网

分析手段:

在linux 上用tcpdump抓包,用wireshark分析抓到的数据

在linux上用iptables阻断特定流量,模拟网络故障,分别模拟了拦截udp 53/tcp 80/tcp 8080/tcp 14000/tcp全拦截等各种情况以及他们的组合

通过 adb shell在手机内执行netstat了解手机网络链接情况

微信网络行为:

程序启动后,优先尝试DNS解析特定域名(support.weixin.qq.com,short.weixin.qq.com,long.weixin.qq.com,wx.qlogo.cn);

如果DNS查询不可用,程序转为使用hardcode的ip链接服务;

如果dns可用,返回的ip为根据ISP智能解析的结果,程序使用返回的ip链接服务;

程序仅在注册阶段使用https链接,内容不详;

程序使用tcp 80/8080链接服务器,其中80为http协议,8080为未知协议;

80/8080两个端口同时或任何单独一个,均可提供服务;

80端口为短链接,8080为长链接, 程序会优先使用8080端口;

没有使用udp传输数据;

当1-2次发送失败时,客户端会弹出提示“当前网络状况不好,是否提交反馈数据”,确认后客户端试图通过web提交反馈数据;

手机qq网络行为:

仅列出跟微信不同之处

尝试的域名不同: monitor.uu.qq.com,3gimg.qq.com, msfwifi.3g.qq.com, kiss.3g.qq.com;

除了80/8080外,还有tcp 14000,功能与8080相同;

程序会优先尝试80/8080,只有这两个不可用时,才尝试14000;

其余同微信;

来源:http://noops.me/?p=327

方案设想:只开放以上端口,其他上也是全部开放了,在上网出口处加上网行为管理设备或者软件进行控制,只允许以下正则的网址通行:

^short.weixin.qq.com,^(mmsns|mmbiz).qpic.cn

^(wx|weixin|res.wx).qq.com

以下是软件实现方式

微信协议小结

http://www.jb51.net/article/96811.htm

发布的消息对应一个ID(只要单个方向唯一即可,服务器端可能会根ID判断重复接收),消息重传机制确保有限次的重试,重试失败给予用户提示,发送成功会反馈确认,客户端只有收到确认信息才知道发送成功。发送消息可能不会产生新SyncKey。

基于版本号(SynKey)的状态消息同步机制,增量、有序传输需求水到渠成。长连接通知/短连接获取、确认等,交互方式简单,确保了消息可靠谱、准确无误到达。

客户端/服务器端都会存储消息ID处理记录,避免被重复消费客户端获取最新消息,但未确认,服务器端不会认为该消息被消费掉。下次客户端会重新获取,会查询当前消息是否被处理过。根据一些现象猜测。

总体上看,微信协议跨平台(TCP或HTPP都可呈现,处理方式可统一),通过“握手”同步,很可靠,无论哪一个平台都可以支持的很好微信协议最小成本为16字节,大部分时间若干个消息包和在一起,批量传输。微信协议说不上最简洁,也不是最节省流量,但是非常成功的。

若服务器检测到一些不确定因素,可能会导致微启用安全套接层SSL协议进行常规的TCP长连接传输。短连接都没有发生变化

发送消息方式

发送消息走已经建立的TCP长连接通道,发送消息到服务器,然后接受确认信息等,产生一次交互。

小伙伴接收到信息阅读也都会收到服务器端通知,产生一次交互等。

可以确定,微信发送消息走TCP长连接方式,因为不对自身状态数据产生影响,应该不交换SyncKey。

在低速网络下,大概会看到消息发送中的提示,属于消息重发机制

网络不好有时客户端会出现发送失败的红色感叹号

已发送到服务器但未收到确认的消息,客户端显示红色感叹号,再次重发,服务器作为重复消息处理,反馈确认

上传图片,会根据图片大小,分割成若干部分(大概1.5K被划分为一部分),同一时间点,客户端会发起若干次POST请求,各自上传成功之后,服务器大概会合并成一个完整图片,返回一个缩略图,显示在APP聊天窗口内。APP作为常规的文字消息发送到服务器端上传音频,则单独走TCP通道,一个两秒的录制音频,客户端录制完毕,分为两块传输,一块最大1.5K左右,服务端响应一条数据通知确认收到。共三次数据传输。

音频和纯文字信息一致,都是走TCP长连接,客户端发送,服务器端确认。

微信协议简单调研笔记如下介绍:

前言

微信可调研点很多,这里仅仅从协议角度进行调研,会涉及到微信协议交换、消息收发等。所谓“弱水三千,只取一瓢”吧。

杂七杂八的,有些长,可直接拉到最后看结论好了。

一。微信协议概览

微信传输协议,官方

公布甚少,在微信技术总监所透漏PPT《微信之道—至简》文档中,有所体现。

纯个人理解:

因张小龙做邮箱Foxmail起家,继而又做了QQ Mail等,QQ Mail是国内第一个支持Exchange ActiveSync协议的免费邮箱,基于其从业背景,微信从一开始就采取基于ActiveSync的修改版状态同步协议Sync,也就再自然不过了。

一句话:增量式、按序、可靠的状态同步传输的微信协议。

大致交换简图如下:

如何获取新数据呢:

服务器端通知,客户端获取

客户端携带最新的SyncKey,发起数据请求

服务器端生成最新的SyncKey连同最新数据发送给客户端

基于版本号机制同步协议,可确保数据增量、有序传输

SyncKey,由服务器端序列号生成器生成,一旦有新消息产生,将会产生最新的SyncKey。类似于版本号

服务器端通知有状态更新,客户端主动获取自从上次更新之后有变动的状态数据,增量式,顺序式。

二。微信Web端简单调试

在线版本微信:

https://webpush.weixin.qq.com/

通过Firefox + Firebug组合调试,也能证实了微信大致通过交换SyncKey方式获取新数据的论述。

1. 发起GET长连接检测是否存在新的需要同步的数据

会携带上最新SyncKey

https://webpush.weixin.qq.com/cgi-bin/mmwebwx-bin/synccheck?callback=jQuery18306073923335455973_1393208247730&r=1393209241862&sid=s7c%2FsxpGRSihgZAA&uin=937355&deviceid=e542565508353877&synckey=1_620943725%7C2_620943769%7C3_620943770%7C11_620942796%7C201_1393208420%7C202_1393209127%7C1000_1393203219&_=1393209241865

返回内容:

window.synccheck={retcode:"0",selector:"2"}

selector值大于0,表示有新的消息需要同步。

据目测,心跳周期为27秒左右。

2. 一旦有新数据,客户端POST请求主动获取同步的数据

https://webpush.weixin.qq.com/cgi-bin/mmwebwx-bin/webwxsync?sid=s7c%2FsxpGRSihgZAA&r=1393208447375

携带消息体:

{"BaseRequest":{"Uin":937355,"Sid":"s7c/sxpGRSihgZAA"},"SyncKey":{"Count":6,"List":[{"Key":1,"Val":620943725},{"Key":2,"Val":620943767},{"Key":3,"Val":620943760},{"Key":11,"Val":620942796},{"Key":201,"Val":1393208365},{"Key":1000,"Val":1393203219}]},"rr":1393208447374}

会携带上最新的SyncKey,会返回复杂结构体JSON内容。

但浏览端收取到消息之后,如何通知服务器端已确认收到了?Web版本微信,没有去做。

在以往使用过程中,曾发现WEB端有丢失消息的现象,但属于偶尔现象。但Android微信客户端(只要登陆连接上来之后)貌似就没有丢失过。

3. 发送消息流程

发起一个POST提交,用于提交用户需要发送的消息

https://webpush.weixin.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg?sid=lQ95vHR52DiaLVqo&r=1393988414386

发送内容:

{"BaseRequest":{"Uin":937355,"Sid":"lQ95vHR52DiaLVqo","Skey":"A6A1ECC6A7DE59DEFF6A05F226AA334DECBA457887B25BC6","DeviceID":"e937227863752975"},"Msg":{"FromUserName":"yongboy","ToUserName":"hehe057854","Type":1,"Content":"hello","ClientMsgId":1393988414380,"LocalID":1393988414380}}

相应内容:

{ "BaseResponse": { "Ret": 0, "ErrMsg": "" } , "MsgID": 1020944348, "LocalID": "1393988414380" }

再次发起一个POST请求,用于申请最新SyncKey

https://webpush.weixin.qq.com/cgi-bin/mmwebwx-bin/webwxsync?sid=lQ95vHR52DiaLVqo&r=1393988414756

发送内容:

{"BaseRequest":{"Uin":937355,"Sid":"lQ95vHR52DiaLVqo"},"SyncKey":{"Count":6,"List":[{"Key":1,"Val":620944310},{"Key":2,"Val":620944346},{"Key":3,"Val":620944344},{"Key":11,"Val":620942796},{"Key":201,"Val":1393988357},{"Key":1000,"Val":1393930108}]},"rr":1393988414756}

响应的(部分)内容:

"SKey": "8F8C6A03489E85E9FDF727ACB95C93C2CDCE9FB9532FC15B"

终止GET长连接,使用最新SyncKey再次发起一个新的GET长连接

https://webpush.weixin.qq.com/cgi-bin/mmwebwx-bin/synccheck?callback=jQuery1830245810089652082181393988305564&r=1393988415015&sid=lQ95vHR52DiaLVqo&uin=937355&deviceid=e937227863752975&synckey=1620944310%7C2620944348%7C3620944344%7C11620942796%7C2011393988357%7C10001393930108&=1393988415016

三。微信Android简单分析

Windows桌面端Android虚拟机中运行最新版微信(5.2),通过tcpdump/Wireshark组合封包分析,以下为分析结果。

0. 初始连接记录

简单记录微信启动之后请求:

11:20:35 dns查询

dns.weixin.qq.com

返回一组IP地址

11:20:35 DNS查询

long.weixin.qq.com

返回一组IP地址,本次通信中,微信使用了最后一个IP作为TCP长连接的连接地址。

11:20:35

http://dns.weixin.qq.com/cgi-bin/micromsg-bin/newgetdns?uin=0&clientversion=620888113&scene=0&net=1

用于请求服务器获得最优IP路径。服务器通过结算返回一个xml定义了域名:IP对应列表。仔细阅读,可看到微信已经开始了国际化的步伐:香港、加拿大、韩国等。

具体文本,请参考:https://gist.github.com/yongboy/9341884

11:20:35

获取到long.weixin.qq.com最优IP,然后建立到101.227.131.105的TCP长连接

11:21:25

POST http://short.weixin.qq.com/cgi-bin/micromsg-bin/getprofile HTTP/1.1 (application/octet-stream)

返回一个名为“micromsgresp.dat”的附件,估计是未阅读的离线消息

11:21:31

POST http://short.weixin.qq.com/cgi-bin/micromsg-bin/whatsnews HTTP/1.1 (application/octet-stream)

大概是资讯、订阅更新等

中间进行一些资源请求等,类似于

GET http://wx.qlogo.cn/mmhead/Q3auHgzwzM7NR4TYFcoNjbxZpfO9aiaE7RU5lXGUw13SMicL6iacWIf2A/96

图片等一些静态资源都会被分配到wx.qlogo.cn域名下面

不明白做什么用途

POST http://short.weixin.qq.com/cgi-bin/micromsg-bin/downloadpackage HTTP/1.1 (application/octet-stream)

输出为micromsgresp.dat文件

11:21:47

GET http://support.weixin.qq.com/cgi-bin/mmsupport-bin/reportdevice?channel=34&deviceid=A952001f7a840c2a&clientversion=620888113&platform=0&lang=zh_CN&installtype=0 HTTP/1.1

返回chunked分块数据

11:21:49

POST http://short.weixin.qq.com/cgi-bin/micromsg-bin/reportstrategy HTTP/1.1 (application/octet-stream)

1. 心跳频率约为5分钟

上次使用Wireshark分析有误(得出18分钟结论),再次重新分析,心跳频率在5分钟左右。

2. 登陆之后,会建立一个长连接,端口号为8080

简单目测为HTTP,初始以为是双通道HTTP,难道是自定义的用于双通道通信的HTTP协议吗,网络上可见资料都是模棱两可、语焉不详。

具体查看长连接初始数据通信,没有发现任何包含"HTTP"字样的数据,以为是微信自定义的TCP/HTTP通信格式。据分析,用于可能用于获取数据、心跳交换消息等用途吧。这个后面会详谈微信是如何做到的。

2.0 初始消息传输

个人资料、离线未阅读消息部分等通过 POST HTTP短连接单独获取。

2.1 二进制简单分析

抽取微信某次HTTP协议方式通信数据,16进制表示,每两个靠近的数字为一个byte字节:

2014-03-03_15h07_30

微信协议可能如下:

一个消息包 = 消息头 + 消息体

消息头固定16字节长度,消息包长度定义在消息头前4个字节中。

单纯摘取第0000行为例,共16个字节的头部:

00 00 00 10 00 10 00 01 00 00 00 06 00 00 00 0f

16进制表示,每两个紧挨着数字代表一个byte字节。

微信消息包格式: 1. 前4字节表示数据包长度,可变 值为16时,意味着一个仅仅包含头部的完整的数据包(可能表示着预先定义好的业务意义),后面可能还有会别的消息包 2. 2个字节表示头部长度,固定值,0x10 = 16 3. 2个字节表示谢意版本,固定值,0x01 = 1 4. 4个字节操作说明数字,可变 5. 序列号,可变 6. 头部后面紧跟着消息体,非明文,加密形式 7. 一个消息包,最小16 byte字节

通过上图(以及其它数据多次采样)分析:

0000 - 0040为单独的数据包

0050行为下一个数据包的头部,前四个字节值为0xca = 202,表示包含了从0050-0110共202个字节数据

一次数据发送,可能包含若干子数据包

换行符n,16进制表示为0x0a,在00f0行,包含了两个换行符号

一个数据体换行符号用于更细粒度的业务数据分割 是否蒙对,需要问问做微信协议的同学

所有被标记为HTTP协议通信所发送数据都包含换行符号

2.2 动手试试猜想,模拟微信TCP长连接

开始很不解为什么会出现如此怪异的HTTP双通道长连接请求,难道基于TCP通信,然后做了一些手脚?很常规的TCP长连接,传输数据时(不是所有数据传输),被wireshark误认为HTTP长连接。这个需要做一个实验证实一下自己想法,设想如下:

写一个Ping-Pong客户端、服务器端程序,然后使用Wireshark看一下结果,是否符合判断。

Java版本的请求端,默认请求8080端口:

<span><pre name="code"Liberation Mono', Courier, monospace; margin-top: 0px !important; margin-bottom: 0px !important; border: none !important; padding: 0px !important; background-color: transparent !important;"><div id="file-pingclient-java-LC1"><span>/**</span></div><div id="file-pingclient-java-LC2"><span> * Ping Client</span></div><div id="file-pingclient-java-LC3"><span> * @author nieyong</span></div><div id="file-pingclient-java-LC4"><span> */</span></div><div id="file-pingclient-java-LC5"><span>package</span> <span>com</span><span>.</span><span>learn</span><span>;</span></div><div id="file-pingclient-java-LC6"> </div><div id="file-pingclient-java-LC7"><span>import</span> <span>io.netty.bootstrap.Bootstrap</span><span>;</span></div><div id="file-pingclient-java-LC8"><span>import</span> <span>io.netty.buffer.ByteBuf</span><span>;</span></div><div id="file-pingclient-java-LC9"><span>import</span> <span>io.netty.buffer.PooledByteBufAllocator</span><span>;</span></div><div id="file-pingclient-java-LC10"><span>import</span> <span>io.netty.channel.ChannelFuture</span><span>;</span></div><div id="file-pingclient-java-LC11"><span>import</span> <span>io.netty.channel.ChannelHandlerContext</span><span>;</span></div><div id="file-pingclient-java-LC12"><span>import</span> <span>io.netty.channel.ChannelInboundHandlerAdapter</span><span>;</span></div><div id="file-pingclient-java-LC13"><span>import</span> <span>io.netty.channel.ChannelInitializer</span><span>;</span></div><div id="file-pingclient-java-LC14"><span>import</span> <span>io.netty.channel.ChannelOption</span><span>;</span></div><div id="file-pingclient-java-LC15"><span>import</span> <span>io.netty.channel.EventLoopGroup</span><span>;</span></div><div id="file-pingclient-java-LC16"><span>import</span> <span>io.netty.channel.nio.NioEventLoopGroup</span><span>;</span></div><div id="file-pingclient-java-LC17"><span>import</span> <span>io.netty.channel.socket.SocketChannel</span><span>;</span></div><div id="file-pingclient-java-LC18"><span>import</span> <span>io.netty.channel.socket.nio.NioSocketChannel</span><span>;</span></div><div id="file-pingclient-java-LC19"> </div><div id="file-pingclient-java-LC20"><span>import</span> <span>java.util.concurrent.TimeUnit</span><span>;</span></div><div id="file-pingclient-java-LC21"> </div><div id="file-pingclient-java-LC22"><span>class</span> <span>PingClientHandler</span> <span>extends</span> <span>ChannelInboundHandlerAdapter</span> <span>{</span></div><div id="file-pingclient-java-LC23"> <span>private</span> <span>final</span> <span>ByteBuf</span> <span>firstMessage</span><span>;</span></div><div id="file-pingclient-java-LC24"> </div><div id="file-pingclient-java-LC25"> <span>public</span> <span>PingClientHandler</span><span>()</span> <span>{</span></div><div id="file-pingclient-java-LC26"> <span>firstMessage</span> <span>=</span> <span>PooledByteBufAllocator</span><span>.</span><span>DEFAULT</span><span>.</span><span>buffer</span><span>(</span><span>22</span><span>);</span></div><div id="file-pingclient-java-LC27"> </div><div id="file-pingclient-java-LC28"> <span>// weixin 16 byte's header</span></div><div id="file-pingclient-java-LC29"> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>0</span><span>);</span></div><div id="file-pingclient-java-LC30"> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>0</span><span>);</span></div><div id="file-pingclient-java-LC31"> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>0</span><span>);</span></div><div id="file-pingclient-java-LC32"> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>16</span><span>);</span></div><div id="file-pingclient-java-LC33"> </div><div id="file-pingclient-java-LC34"> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>0</span><span>);</span></div><div id="file-pingclient-java-LC35"> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>16</span><span>);</span></div><div id="file-pingclient-java-LC36"> </div><div id="file-pingclient-java-LC37"> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>0</span><span>);</span></div><div id="file-pingclient-java-LC38"> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>1</span><span>);</span></div><div id="file-pingclient-java-LC39"> </div><div id="file-pingclient-java-LC40"> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>0</span><span>);</span></div><div id="file-pingclient-java-LC41"> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>0</span><span>);</span></div><div id="file-pingclient-java-LC42"> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>0</span><span>);</span></div><div id="file-pingclient-java-LC43"> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>6</span><span>);</span></div><div id="file-pingclient-java-LC44"> </div><div id="file-pingclient-java-LC45"> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>0</span><span>);</span></div><div id="file-pingclient-java-LC46"> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>0</span><span>);</span></div><div id="file-pingclient-java-LC47"> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>0</span><span>);</span></div><div id="file-pingclient-java-LC48"> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>1</span><span>);</span></div><div id="file-pingclient-java-LC49"> </div><div id="file-pingclient-java-LC50"> <span>// just for /n</span></div><div id="file-pingclient-java-LC51"> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>'n'</span><span>);</span> <span>// 1 byte</span></div><div id="file-pingclient-java-LC52"> </div><div id="file-pingclient-java-LC53"> <span>// footer 16 byte</span></div><div id="file-pingclient-java-LC54"> <span>String</span> <span>welcome</span> <span>=</span> <span>"hello"</span><span>;</span> <span>// 5 byte</span></div><div id="file-pingclient-java-LC55"> <span>firstMessage</span><span>.</span><span>writeBytes</span><span>(</span><span>welcome</span><span>.</span><span>getBytes</span><span>());</span></div><div id="file-pingclient-java-LC56"> <span>}</span></div><div id="file-pingclient-java-LC57"> </div><div id="file-pingclient-java-LC58"> <span>@Override</span></div><div id="file-pingclient-java-LC59"> <span>public</span> <span>void</span> <span>channelActive</span><span>(</span><span>ChannelHandlerContext</span> <span>ctx</span><span>)</span> <span>{</span></div><div id="file-pingclient-java-LC60"> <span>ctx</span><span>.</span><span>writeAndFlush</span><span>(</span><span>firstMessage</span><span>);</span></div><div id="file-pingclient-java-LC61"> <span>}</span></div><div id="file-pingclient-java-LC62"> </div><div id="file-pingclient-java-LC63"> <span>@Override</span></div><div id="file-pingclient-java-LC64"> <span>public</span> <span>void</span> <span>channelRead</span><span>(</span><span>final</span> <span>ChannelHandlerContext</span> <span>ctx</span><span>,</span> <span>final</span> <span>Object</span> <span>msg</span><span>)</span></div><div id="file-pingclient-java-LC65"> <span>throws</span> <span>Exception</span> <span>{</span></div><div id="file-pingclient-java-LC66"> <span>ctx</span><span>.</span><span>executor</span><span>().</span><span>schedule</span><span>(</span><span>new</span> <span>Runnable</span><span>()</span> <span>{</span></div><div id="file-pingclient-java-LC67"> <span>@Override</span></div><div id="file-pingclient-java-LC68"> <span>public</span> <span>void</span> <span>run</span><span>()</span> <span>{</span></div><div id="file-pingclient-java-LC69"> <span>ctx</span><span>.</span><span>channel</span><span>().</span><span>writeAndFlush</span><span>(</span><span>msg</span><span>);</span></div><div id="file-pingclient-java-LC70"> <span>}</span></div><div id="file-pingclient-java-LC71"> <span>},</span> <span>1</span><span>,</span> <span>TimeUnit</span><span>.</span><span>SECONDS</span><span>);</span></div><div id="file-pingclient-java-LC72"> <span>}</span></div><div id="file-pingclient-java-LC73"> </div><div id="file-pingclient-java-LC74"> <span>@Override</span></div><div id="file-pingclient-java-LC75"> <span>public</span> <span>void</span> <span>channelReadComplete</span><span>(</span><span>ChannelHandlerContext</span> <span>ctx</span><span>)</span> <span>throws</span> <span>Exception</span> <span>{</span></div><div id="file-pingclient-java-LC76"> <span>ctx</span><span>.</span><span>flush</span><span>();</span></div><div id="file-pingclient-java-LC77"> <span>}</span></div><div id="file-pingclient-java-LC78"> </div><div id="file-pingclient-java-LC79"> <span>@Override</span></div><div id="file-pingclient-java-LC80"> <span>public</span> <span>void</span> <span>exceptionCaught</span><span>(</span><span>ChannelHandlerContext</span> <span>ctx</span><span>,</span> <span>Throwable</span> <span>cause</span><span>)</span> <span>{</span></div><div id="file-pingclient-java-LC81"> <span>System</span><span>.</span><span>err</span><span>.</span><span>println</span><span>(</span><span>"Unexpected exception from downstream :"</span></div><div id="file-pingclient-java-LC82"> <span>+</span> <span>cause</span><span>.</span><span>getMessage</span><span>());</span></div><div id="file-pingclient-java-LC83"> <span>ctx</span><span>.</span><span>close</span><span>();</span></div><div id="file-pingclient-java-LC84"> <span>}</span></div><div id="file-pingclient-java-LC85"><span>}</span></div><div id="file-pingclient-java-LC86"> </div><div id="file-pingclient-java-LC87"><span>public</span> <span>class</span> <span>PingClient</span> <span>{</span></div><div id="file-pingclient-java-LC88"> </div><div id="file-pingclient-java-LC89"> <span>private</span> <span>final</span> <span>String</span> <span>host</span><span>;</span></div><div id="file-pingclient-java-LC90"> <span>private</span> <span>final</span> <span>int</span> <span>port</span><span>;</span></div><div id="file-pingclient-java-LC91"> </div><div id="file-pingclient-java-LC92"> <span>public</span> <span>PingClient</span><span>(</span><span>String</span> <span>host</span><span>,</span> <span>int</span> <span>port</span><span>)</span> <span>{</span></div><div id="file-pingclient-java-LC93"> <span>this</span><span>.</span><span>host</span> <span>=</span> <span>host</span><span>;</span></div><div id="file-pingclient-java-LC94"> <span>this</span><span>.</span><span>port</span> <span>=</span> <span>port</span><span>;</span></div><div id="file-pingclient-java-LC95"> <span>}</span></div><div id="file-pingclient-java-LC96"> </div><div id="file-pingclient-java-LC97"> <span>public</span> <span>void</span> <span>run</span><span>()</span> <span>throws</span> <span>Exception</span> <span>{</span></div><div id="file-pingclient-java-LC98"> <span>EventLoopGroup</span> <span>group</span> <span>=</span> <span>new</span> <span>NioEventLoopGroup</span><span>();</span></div><div id="file-pingclient-java-LC99"> <span>try</span> <span>{</span></div><div id="file-pingclient-java-LC100"> <span>Bootstrap</span> <span>b</span> <span>=</span> <span>new</span> <span>Bootstrap</span><span>();</span></div><div id="file-pingclient-java-LC101"> <span>b</span><span>.</span><span>group</span><span>(</span><span>group</span><span>).</span><span>channel</span><span>(</span><span>NioSocketChannel</span><span>.</span><span>class</span><span>)</span></div><div id="file-pingclient-java-LC102"> <span>.</span><span>option</span><span>(</span><span>ChannelOption</span><span>.</span><span>TCP_NODELAY</span><span>,</span> <span>true</span><span>)</span></div><div id="file-pingclient-java-LC103"> <span>.</span><span>handler</span><span>(</span><span>new</span> <span>ChannelInitializer</span><span><</span><span>SocketChannel</span><span>>()</span> <span>{</span></div><div id="file-pingclient-java-LC104"> <span>@Override</span></div><div id="file-pingclient-java-LC105"> <span>public</span> <span>void</span> <span>initChannel</span><span>(</span><span>SocketChannel</span> <span>ch</span><span>)</span></div><div id="file-pingclient-java-LC106"> <span>throws</span> <span>Exception</span> <span>{</span></div><div id="file-pingclient-java-LC107"> <span>ch</span><span>.</span><span>pipeline</span><span>().</span><span>addLast</span><span>(</span><span>new</span> <span>PingClientHandler</span><span>());</span></div><div id="file-pingclient-java-LC108"> <span>}</span></div><div id="file-pingclient-java-LC109"> <span>});</span></div><div id="file-pingclient-java-LC110"> </div><div id="file-pingclient-java-LC111"> <span>ChannelFuture</span> <span>f</span> <span>=</span> <span>b</span><span>.</span><span>connect</span><span>(</span><span>host</span><span>,</span> <span>port</span><span>).</span><span>sync</span><span>();</span></div><div id="file-pingclient-java-LC112"> </div><div id="file-pingclient-java-LC113"> <span>f</span><span>.</span><span>channel</span><span>().</span><span>closeFuture</span><span>().</span><span>sync</span><span>();</span></div><div id="file-pingclient-java-LC114"> <span>}</span> <span>finally</span> <span>{</span></div><div id="file-pingclient-java-LC115"> <span>// Shut down the event loop to terminate all threads.</span></div><div id="file-pingclient-java-LC116"> <span>group</span><span>.</span><span>shutdownGracefully</span><span>();</span></div><div id="file-pingclient-java-LC117"> <span>}</span></div><div id="file-pingclient-java-LC118"> <span>}</span></div><div id="file-pingclient-java-LC119"> </div><div id="file-pingclient-java-LC120"> <span>public</span> <span>static</span> <span>void</span> <span>main</span><span>(</span><span>String</span><span>[]</span> <span>args</span><span>)</span> <span>throws</span> <span>Exception</span> <span>{</span></div><div id="file-pingclient-java-LC121"> <span>String</span> <span>host</span> <span>=</span> <span>"127.0.0.1"</span><span>;</span></div><div id="file-pingclient-java-LC122"> <span>int</span> <span>port</span> <span>=</span> <span>8080</span><span>;</span></div><div id="file-pingclient-java-LC123"> </div><div id="file-pingclient-java-LC124"> <span>if</span> <span>(</span><span>args</span><span>.</span><span>length</span> <span>==</span> <span>3</span><span>)</span> <span>{</span></div><div id="file-pingclient-java-LC125"> <span>host</span> <span>=</span> <span>args</span><span>[</span><span>0</span><span>];</span></div><div id="file-pingclient-java-LC126"> <span>port</span> <span>=</span> <span>Integer</span><span>.</span><span>parseInt</span><span>(</span><span>args</span><span>[</span><span>1</span><span>]);</span></div><div id="file-pingclient-java-LC127"> <span>}</span></div><div id="file-pingclient-java-LC128"> </div><div id="file-pingclient-java-LC129"> <span>new</span> <span>PingClient</span><span>(</span><span>host</span><span>,</span> <span>port</span><span>).</span><span>run</span><span>();</span></div><div id="file-pingclient-java-LC130"> <span>}</span></div><div id="file-pingclient-java-LC131"><span>}</span></div></span>

C语言版本的服务器程序,收到什么发送什么,没有任何逻辑,默认绑定8080端口:

<span><pre name="code"Liberation Mono', Courier, monospace; margin-top: 0px !important; margin-bottom: 0px !important; border: none !important; padding: 0px !important; background-color: transparent !important;"><div id="file-pong_server-c-LC1"><span>/**</span></div><div id="file-pong_server-c-LC2"><span> * nieyong@youku.com</span></div><div id="file-pong_server-c-LC3"><span> * how to compile it:</span></div><div id="file-pong_server-c-LC4"><span> * gcc pong_server.c -o pong_server /usr/local/lib/libev.a -lm </span></div><div id="file-pong_server-c-LC5"><span> */</span></div><div id="file-pong_server-c-LC6"><span>#include <arpa/inet.h></span></div><div id="file-pong_server-c-LC7"><span>#include <stdlib.h></span></div><div id="file-pong_server-c-LC8"><span>#include <stdio.h></span></div><div id="file-pong_server-c-LC9"><span>#include <string.h></span></div><div id="file-pong_server-c-LC10"><span>#include <fcntl.h></span></div><div id="file-pong_server-c-LC11"><span>#include <errno.h></span></div><div id="file-pong_server-c-LC12"><span>#include <err.h></span></div><div id="file-pong_server-c-LC13"><span>#include <unistd.h></span></div><div id="file-pong_server-c-LC14"> </div><div id="file-pong_server-c-LC15"><span>#include "../include/ev.h"</span></div><div id="file-pong_server-c-LC16"> </div><div id="file-pong_server-c-LC17"><span>static</span> <span>int</span> <span>server_port</span> <span>=</span> <span>8080</span><span>;</span></div><div id="file-pong_server-c-LC18"> </div><div id="file-pong_server-c-LC19"><span>struct</span> <span>ev_loop</span> <span>*</span><span>loop</span><span>;</span></div><div id="file-pong_server-c-LC20"><span>typedef</span> <span>struct</span> <span>{</span></div><div id="file-pong_server-c-LC21"> <span>int</span> <span>fd</span><span>;</span></div><div id="file-pong_server-c-LC22"> <span>ev_io</span> <span>ev_read</span><span>;</span></div><div id="file-pong_server-c-LC23"><span>}</span> <span>client_t</span><span>;</span></div><div id="file-pong_server-c-LC24"> </div><div id="file-pong_server-c-LC25"><span>ev_io</span> <span>ev_accept</span><span>;</span></div><div id="file-pong_server-c-LC26"> </div><div id="file-pong_server-c-LC27"><span>static</span> <span>void</span> <span>free_res</span><span>(</span><span>struct</span> <span>ev_loop</span> <span>*</span><span>loop</span><span>,</span> <span>ev_io</span> <span>*</span><span>ws</span><span>);</span></div><div id="file-pong_server-c-LC28"> </div><div id="file-pong_server-c-LC29"><span>int</span> <span>setnonblock</span><span>(</span><span>int</span> <span>fd</span><span>)</span> <span>{</span></div><div id="file-pong_server-c-LC30"> <span>int</span> <span>flags</span> <span>=</span> <span>fcntl</span><span>(</span><span>fd</span><span>,</span> <span>F_GETFL</span><span>);</span></div><div id="file-pong_server-c-LC31"> <span>if</span> <span>(</span><span>flags</span> <span><</span> <span>0</span><span>)</span></div><div id="file-pong_server-c-LC32"> <span>return</span> <span>flags</span><span>;</span></div><div id="file-pong_server-c-LC33"> </div><div id="file-pong_server-c-LC34"> <span>flags</span> <span>|=</span> <span>O_NONBLOCK</span><span>;</span></div><div id="file-pong_server-c-LC35"> <span>if</span> <span>(</span><span>fcntl</span><span>(</span><span>fd</span><span>,</span> <span>F_SETFL</span><span>,</span> <span>flags</span><span>)</span> <span><</span> <span>0</span><span>)</span></div><div id="file-pong_server-c-LC36"> <span>return</span> <span>-</span><span>1</span><span>;</span></div><div id="file-pong_server-c-LC37"> </div><div id="file-pong_server-c-LC38"> <span>return</span> <span>0</span><span>;</span></div><div id="file-pong_server-c-LC39"><span>}</span></div><div id="file-pong_server-c-LC40"> </div><div id="file-pong_server-c-LC41"><span>static</span> <span>void</span> <span>read_cb</span><span>(</span><span>struct</span> <span>ev_loop</span> <span>*</span><span>loop</span><span>,</span> <span>ev_io</span> <span>*</span><span>w</span><span>,</span> <span>int</span> <span>revents</span><span>)</span> <span>{</span></div><div id="file-pong_server-c-LC42"> <span>client_t</span> <span>*</span><span>client</span> <span>=</span> <span>w</span><span>-></span><span>data</span><span>;</span></div><div id="file-pong_server-c-LC43"> <span>int</span> <span>r</span> <span>=</span> <span>0</span><span>;</span></div><div id="file-pong_server-c-LC44"> <span>char</span> <span>rbuff</span><span>[</span><span>1024</span><span>];</span></div><div id="file-pong_server-c-LC45"> <span>if</span> <span>(</span><span>revents</span> <span>&</span> <span>EV_READ</span><span>)</span> <span>{</span></div><div id="file-pong_server-c-LC46"> <span>r</span> <span>=</span> <span>read</span><span>(</span><span>client</span><span>-></span><span>fd</span><span>,</span> <span>&</span><span>rbuff</span><span>,</span> <span>1024</span><span>);</span></div><div id="file-pong_server-c-LC47"> <span>}</span></div><div id="file-pong_server-c-LC48"> </div><div id="file-pong_server-c-LC49"> <span>if</span> <span>(</span><span>EV_ERROR</span> <span>&</span> <span>revents</span><span>)</span> <span>{</span></div><div id="file-pong_server-c-LC50"> <span>fprintf</span><span>(</span><span>stderr</span><span>,</span> <span>"error event in read</span><span>n</span><span>"</span><span>);</span></div><div id="file-pong_server-c-LC51"> <span>free_res</span><span>(</span><span>loop</span><span>,</span> <span>w</span><span>);</span></div><div id="file-pong_server-c-LC52"> <span>return</span> <span>;</span></div><div id="file-pong_server-c-LC53"> <span>}</span></div><div id="file-pong_server-c-LC54"> </div><div id="file-pong_server-c-LC55"> <span>if</span> <span>(</span><span>r</span> <span><</span> <span>0</span><span>)</span> <span>{</span></div><div id="file-pong_server-c-LC56"> <span>fprintf</span><span>(</span><span>stderr</span><span>,</span> <span>"read error</span><span>n</span><span>"</span><span>);</span></div><div id="file-pong_server-c-LC57"> <span>ev_io_stop</span><span>(</span><span>EV_A_</span> <span>w</span><span>);</span></div><div id="file-pong_server-c-LC58"> <span>free_res</span><span>(</span><span>loop</span><span>,</span> <span>w</span><span>);</span></div><div id="file-pong_server-c-LC59"> <span>return</span><span>;</span></div><div id="file-pong_server-c-LC60"> <span>}</span></div><div id="file-pong_server-c-LC61"> </div><div id="file-pong_server-c-LC62"> <span>if</span> <span>(</span><span>r</span> <span>==</span> <span>0</span><span>)</span> <span>{</span></div><div id="file-pong_server-c-LC63"> <span>fprintf</span><span>(</span><span>stderr</span><span>,</span> <span>"client disconnected.</span><span>n</span><span>"</span><span>);</span></div><div id="file-pong_server-c-LC64"> <span>ev_io_stop</span><span>(</span><span>EV_A_</span> <span>w</span><span>);</span></div><div id="file-pong_server-c-LC65"> <span>free_res</span><span>(</span><span>loop</span><span>,</span> <span>w</span><span>);</span></div><div id="file-pong_server-c-LC66"> <span>return</span><span>;</span></div><div id="file-pong_server-c-LC67"> <span>}</span></div><div id="file-pong_server-c-LC68"> </div><div id="file-pong_server-c-LC69"> <span>send</span><span>(</span><span>client</span><span>-></span><span>fd</span><span>,</span> <span>rbuff</span><span>,</span> <span>r</span><span>,</span> <span>0</span><span>);</span></div><div id="file-pong_server-c-LC70"><span>}</span></div><div id="file-pong_server-c-LC71"> </div><div id="file-pong_server-c-LC72"><span>static</span> <span>void</span> <span>accept_cb</span><span>(</span><span>struct</span> <span>ev_loop</span> <span>*</span><span>loop</span><span>,</span> <span>ev_io</span> <span>*</span><span>w</span><span>,</span> <span>int</span> <span>revents</span><span>)</span> <span>{</span></div><div id="file-pong_server-c-LC73"> <span>struct</span> <span>sockaddr_in</span> <span>client_addr</span><span>;</span></div><div id="file-pong_server-c-LC74"> <span>socklen_t</span> <span>client_len</span> <span>=</span> <span>sizeof</span><span>(</span><span>client_addr</span><span>);</span></div><div id="file-pong_server-c-LC75"> <span>int</span> <span>client_fd</span> <span>=</span> <span>accept</span><span>(</span><span>w</span><span>-></span><span>fd</span><span>,</span> <span>(</span><span>struct</span> <span>sockaddr</span> <span>*</span><span>)</span> <span>&</span><span>client_addr</span><span>,</span> <span>&</span><span>client_len</span><span>);</span></div><div id="file-pong_server-c-LC76"> <span>if</span> <span>(</span><span>client_fd</span> <span>==</span> <span>-</span><span>1</span><span>)</span> <span>{</span></div><div id="file-pong_server-c-LC77"> <span>fprintf</span><span>(</span><span>stderr</span><span>,</span> <span>"the client_fd is NULL !</span><span>n</span><span>"</span><span>);</span></div><div id="file-pong_server-c-LC78"> <span>return</span><span>;</span></div><div id="file-pong_server-c-LC79"> <span>}</span></div><div id="file-pong_server-c-LC80"> </div><div id="file-pong_server-c-LC81"> <span>client_t</span> <span>*</span><span>client</span> <span>=</span> <span>malloc</span><span>(</span><span>sizeof</span><span>(</span><span>client_t</span><span>));</span></div><div id="file-pong_server-c-LC82"> <span>client</span><span>-></span><span>fd</span> <span>=</span> <span>client_fd</span><span>;</span></div><div id="file-pong_server-c-LC83"> <span>if</span> <span>(</span><span>setnonblock</span><span>(</span><span>client</span><span>-></span><span>fd</span><span>)</span> <span><</span> <span>0</span><span>)</span></div><div id="file-pong_server-c-LC84"> <span>err</span><span>(</span><span>1</span><span>,</span> <span>"failed to set client socket to non-blocking"</span><span>);</span></div><div id="file-pong_server-c-LC85"> </div><div id="file-pong_server-c-LC86"> <span>client</span><span>-></span><span>ev_read</span><span>.</span><span>data</span> <span>=</span> <span>client</span><span>;</span></div><div id="file-pong_server-c-LC87"> </div><div id="file-pong_server-c-LC88"> <span>ev_io_init</span><span>(</span><span>&</span><span>client</span><span>-></span><span>ev_read</span><span>,</span> <span>read_cb</span><span>,</span> <span>client</span><span>-></span><span>fd</span><span>,</span> <span>EV_READ</span><span>);</span></div><div id="file-pong_server-c-LC89"> <span>ev_io_start</span><span>(</span><span>loop</span><span>,</span> <span>&</span><span>client</span><span>-></span><span>ev_read</span><span>);</span></div><div id="file-pong_server-c-LC90"><span>}</span></div><div id="file-pong_server-c-LC91"> </div><div id="file-pong_server-c-LC92"><span>int</span> <span>main</span><span>(</span><span>int</span> <span>argc</span><span>,</span> <span>char</span> <span>const</span> <span>*</span><span>argv</span><span>[])</span> <span>{</span></div><div id="file-pong_server-c-LC93"> <span>int</span> <span>ch</span><span>;</span></div><div id="file-pong_server-c-LC94"> <span>while</span> <span>((</span><span>ch</span> <span>=</span> <span>getopt</span><span>(</span><span>argc</span><span>,</span> <span>argv</span><span>,</span> <span>"p:"</span><span>))</span> <span>!=</span> <span>-</span><span>1</span><span>)</span> <span>{</span></div><div id="file-pong_server-c-LC95"> <span>switch</span> <span>(</span><span>ch</span><span>)</span> <span>{</span></div><div id="file-pong_server-c-LC96"> <span>case</span> <span>'p'</span><span>:</span></div><div id="file-pong_server-c-LC97"> <span>server_port</span> <span>=</span> <span>atoi</span><span>(</span><span>optarg</span><span>);</span></div><div id="file-pong_server-c-LC98"> <span>break</span><span>;</span></div><div id="file-pong_server-c-LC99"> <span>}</span></div><div id="file-pong_server-c-LC100"> <span>}</span></div><div id="file-pong_server-c-LC101"> </div><div id="file-pong_server-c-LC102"> <span>loop</span> <span>=</span> <span>ev_default_loop</span><span>(</span><span>0</span><span>);</span></div><div id="file-pong_server-c-LC103"> <span>struct</span> <span>sockaddr_in</span> <span>listen_addr</span><span>;</span></div><div id="file-pong_server-c-LC104"> <span>int</span> <span>reuseaddr_on</span> <span>=</span> <span>1</span><span>;</span></div><div id="file-pong_server-c-LC105"> <span>int</span> <span>listen_fd</span> <span>=</span> <span>socket</span><span>(</span><span>AF_INET</span><span>,</span> <span>SOCK_STREAM</span><span>,</span> <span>0</span><span>);</span></div><div id="file-pong_server-c-LC106"> <span>if</span> <span>(</span><span>listen_fd</span> <span><</span> <span>0</span><span>)</span></div><div id="file-pong_server-c-LC107"> <span>err</span><span>(</span><span>1</span><span>,</span> <span>"listen failed"</span><span>);</span></div><div id="file-pong_server-c-LC108"> <span>if</span> <span>(</span><span>setsockopt</span><span>(</span><span>listen_fd</span><span>,</span> <span>SOL_SOCKET</span><span>,</span> <span>SO_REUSEADDR</span><span>,</span> <span>&</span><span>reuseaddr_on</span><span>,</span> <span>sizeof</span><span>(</span><span>reuseaddr_on</span><span>))</span> <span>==</span> <span>-</span><span>1</span><span>)</span></div><div id="file-pong_server-c-LC109"> <span>err</span><span>(</span><span>1</span><span>,</span> <span>"setsockopt failed"</span><span>);</span></div><div id="file-pong_server-c-LC110"> </div><div id="file-pong_server-c-LC111"> <span>memset</span><span>(</span><span>&</span><span>listen_addr</span><span>,</span> <span>0</span><span>,</span> <span>sizeof</span><span>(</span><span>listen_addr</span><span>));</span></div><div id="file-pong_server-c-LC112"> <span>listen_addr</span><span>.</span><span>sin_family</span> <span>=</span> <span>AF_INET</span><span>;</span></div><div id="file-pong_server-c-LC113"> <span>listen_addr</span><span>.</span><span>sin_addr</span><span>.</span><span>s_addr</span> <span>=</span> <span>INADDR_ANY</span><span>;</span></div><div id="file-pong_server-c-LC114"> <span>listen_addr</span><span>.</span><span>sin_port</span> <span>=</span> <span>htons</span><span>(</span><span>server_port</span><span>);</span></div><div id="file-pong_server-c-LC115"> </div><div id="file-pong_server-c-LC116"> <span>if</span> <span>(</span><span>bind</span><span>(</span><span>listen_fd</span><span>,</span> <span>(</span><span>struct</span> <span>sockaddr</span> <span>*</span><span>)</span> <span>&</span><span>listen_addr</span><span>,</span> <span>sizeof</span><span>(</span><span>listen_addr</span><span>))</span> <span><</span> <span>0</span><span>)</span></div><div id="file-pong_server-c-LC117"> <span>err</span><span>(</span><span>1</span><span>,</span> <span>"bind failed"</span><span>);</span></div><div id="file-pong_server-c-LC118"> <span>if</span> <span>(</span><span>listen</span><span>(</span><span>listen_fd</span><span>,</span> <span>5</span><span>)</span> <span><</span> <span>0</span><span>)</span></div><div id="file-pong_server-c-LC119"> <span>err</span><span>(</span><span>1</span><span>,</span> <span>"listen failed"</span><span>);</span></div><div id="file-pong_server-c-LC120"> <span>if</span> <span>(</span><span>setnonblock</span><span>(</span><span>listen_fd</span><span>)</span> <span><</span> <span>0</span><span>)</span></div><div id="file-pong_server-c-LC121"> <span>err</span><span>(</span><span>1</span><span>,</span> <span>"failed to set server socket to non-blocking"</span><span>);</span></div><div id="file-pong_server-c-LC122"> </div><div id="file-pong_server-c-LC123"> <span>ev_io_init</span><span>(</span><span>&</span><span>ev_accept</span><span>,</span> <span>accept_cb</span><span>,</span> <span>listen_fd</span><span>,</span> <span>EV_READ</span><span>);</span></div><div id="file-pong_server-c-LC124"> <span>ev_io_start</span><span>(</span><span>loop</span><span>,</span> <span>&</span><span>ev_accept</span><span>);</span></div><div id="file-pong_server-c-LC125"> <span>ev_loop</span><span>(</span><span>loop</span><span>,</span> <span>0</span><span>);</span></div><div id="file-pong_server-c-LC126"> </div><div id="file-pong_server-c-LC127"> <span>return</span> <span>0</span><span>;</span></div><div id="file-pong_server-c-LC128"><span>}</span></div><div id="file-pong_server-c-LC129"> </div><div id="file-pong_server-c-LC130"><span>static</span> <span>void</span> <span>free_res</span><span>(</span><span>struct</span> <span>ev_loop</span> <span>*</span><span>loop</span><span>,</span> <span>ev_io</span> <span>*</span><span>w</span><span>)</span> <span>{</span></div><div id="file-pong_server-c-LC131"> <span>client_t</span> <span>*</span><span>client</span> <span>=</span> <span>w</span><span>-></span><span>data</span><span>;</span></div><div id="file-pong_server-c-LC132"> <span>if</span> <span>(</span><span>client</span> <span>==</span> <span>NULL</span><span>)</span> <span>{</span></div><div id="file-pong_server-c-LC133"> <span>fprintf</span><span>(</span><span>stderr</span><span>,</span> <span>"the client is NULL !!!!!!"</span><span>);</span></div><div id="file-pong_server-c-LC134"> <span>return</span><span>;</span></div><div id="file-pong_server-c-LC135"> <span>}</span></div><div id="file-pong_server-c-LC136"> </div><div id="file-pong_server-c-LC137"> <span>ev_io_stop</span><span>(</span><span>loop</span><span>,</span> <span>&</span><span>client</span><span>-></span><span>ev_read</span><span>);</span></div><div id="file-pong_server-c-LC138"> <span>close</span><span>(</span><span>client</span><span>-></span><span>fd</span><span>);</span></div><div id="file-pong_server-c-LC139"> <span>free</span><span>(</span><span>client</span><span>);</span></div><div id="file-pong_server-c-LC140"><span>}</span></div></span>

这里有一个现场图:

可以尝试稍微改变输出内容,去除换行符“n”,把端口换成9000,试试看,就会发现Wireshark输出不同的结果来。

2.3 结论是什么呢?

若使用原始TCP进行双向通信,则需要满足以下条件,可以被类似于Wireshark协议拦截器误认为是HTTP长连接:

使用80/8080端口(81/3128/8000经测试无效) 也许8080一般被作为WEB代理服务端口,微信才会享用这个红利吧。

输出的内容中,一定要包含换行字符"n"

因此,可以定性为微信使用了基于8080端口TCP长连接,一旦数据包中含有换行"n"符号,就会被Wireshark误认为HTTP协议。可能微信是无心为之吧。

3. 新消息获取方式

TCP长连接接收到服务器通知有新消息需要获取

APP发起一个HTTP POST请求获取新状态消息,会带上当前SyncKey 地址为:http://short.weixin.qq.com/cgi-bin/micromsg-bin/reportstrategy HTTP/1.1,看不到明文

APP获取到新的消息,会再次发起一次HTTP POST请求,告诉服务器已确认收到,同时获取最新SyncKey 地址为:http://short.weixin.qq.com/cgi-bin/micromsg-bin/kvreport,看不到明文

接受一个消息,TCP长连接至少交互两次,客户端发起两次HTTP POST请求 具体每次交互内容是什么,有些模糊

服务器需要支持:状态消息获取标记,状态消息确认收取标记。只有被确认收到,此状态消息才算是被正确消费掉

多个不同设备同一账号同时使用微信,同一个状态消息会会被同时分发到多个设备上

此时消息请求截图如下:

4. 发送消息方式

发送消息走已经建立的TCP长连接通道,发送消息到服务器,然后接受确认信息等,产生一次交互。

小伙伴接收到信息阅读也都会收到服务器端通知,产生一次交互等。

可以确定,微信发送消息走TCP长连接方式,因为不对自身状态数据产生影响,应该不交换SyncKey。

在低速网络下,大概会看到消息发送中的提示,属于消息重发机制

网络不好有时客户端会出现发送失败的红色感叹号

已发送到服务器但未收到确认的消息,客户端显示红色感叹号,再次重发,服务器作为重复消息处理,反馈确认

上传图片,会根据图片大小,分割成若干部分(大概1.5K被划分为一部分),同一时间点,客户端会发起若干次POST请求,各自上传成功之后,服务器大概会合并成一个完整图片,返回一个缩略图,显示在APP聊天窗口内。APP作为常规的文字消息发送到服务器端上传音频,则单独走TCP通道,一个两秒的录制音频,客户端录制完毕,分为两块传输,一块最大1.5K左右,服务端响应一条数据通知确认收到。共三次数据传输。音频和纯文字信息一致,都是走TCP长连接,客户端发送,服务器端确认。

四。微信协议小结

发布的消息对应一个ID(只要单个方向唯一即可,服务器端可能会根ID判断重复接收),消息重传机制确保有限次的重试,重试失败给予用户提示,发送成功会反馈确认,客户端只有收到确认信息才知道发送成功。发送消息可能不会产生新SyncKey。

基于版本号(SynKey)的状态消息同步机制,增量、有序传输需求水到渠成。长连接通知/短连接获取、确认等,交互方式简单,确保了消息可靠谱、准确无误到达。

客户端/服务器端都会存储消息ID处理记录,避免被重复消费客户端获取最新消息,但未确认,服务器端不会认为该消息被消费掉。下次客户端会重新获取,会查询当前消息是否被处理过。根据一些现象猜测。

总体上看,微信协议跨平台(TCP或HTPP都可呈现,处理方式可统一),通过“握手”同步,很可靠,无论哪一个平台都可以支持的很好微信协议最小成本为16字节,大部分时间若干个消息包和在一起,批量传输。微信协议说不上最简洁,也不是最节省流量,但是非常成功的。

若服务器检测到一些不确定因素,可能会导致微启用安全套接层SSL协议进行常规的TCP长连接传输。短连接都没有发生变化

以上,根据有限资料和数据拦截观察总结得出,啰啰嗦嗦,勉强凑成一篇,会存在一些不正确之处,欢迎给予纠正。在多次

五。附录

Microsoft Exchange Active Sync协议,简称EAS,分为folderrsync(同步文件夹目录,即邮箱内有哪几个文件夹)和sync(每个文件夹内有哪些文档)两部分。

某网友总结的协议一次回话大致示范:

Client: synckey=0 //第一次key为0

Server: newsynckey=1235434 //第一次返回新key

Client: synckey=1235434 //使用新key查询

Server: newsynckey=1647645,data=*****//第一次查询,得到新key和数据

Client: synckey=1647645

Server: newsynckey=5637535,data=null //第二次查询,无新消息

Client: synckey=5637535

Server: newsynckey=8654542, data=****//第三次查询,增量同步

上页中的相邻请求都是隔固定时间的,如两分钟

客户端每次使用旧key标记自己的状态,服务端每次将新key和增量数据一起返回。

key是递增的,但不要求连续

请求的某个参数决定服务器是否立即返回

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新Javascript教程学习
    热门Javascript教程学习
    编程开发子分类