微信端口及协议分析(java、C版)(4)

<span><pre><div><span>/**</span></div><div><span> * Ping Client</span></div><div><span> * @author nieyong</span></div><div><span> */</span></div><div><span>package</span> <span>com</span><span>.</span><span>learn</span><span>;</span></div><div> </div><div><span>import</span> <span>io.netty.bootstrap.Bootstrap</span><span>;</span></div><div><span>import</span> <span>io.netty.buffer.ByteBuf</span><span>;</span></div><div><span>import</span> <span>io.netty.buffer.PooledByteBufAllocator</span><span>;</span></div><div><span>import</span> <span>io.netty.channel.ChannelFuture</span><span>;</span></div><div><span>import</span> <span>io.netty.channel.ChannelHandlerContext</span><span>;</span></div><div><span>import</span> <span>io.netty.channel.ChannelInboundHandlerAdapter</span><span>;</span></div><div><span>import</span> <span>io.netty.channel.ChannelInitializer</span><span>;</span></div><div><span>import</span> <span>io.netty.channel.ChannelOption</span><span>;</span></div><div><span>import</span> <span>io.netty.channel.EventLoopGroup</span><span>;</span></div><div><span>import</span> <span>io.netty.channel.nio.NioEventLoopGroup</span><span>;</span></div><div><span>import</span> <span>io.netty.channel.socket.SocketChannel</span><span>;</span></div><div><span>import</span> <span>io.netty.channel.socket.nio.NioSocketChannel</span><span>;</span></div><div> </div><div><span>import</span> <span>java.util.concurrent.TimeUnit</span><span>;</span></div><div> </div><div><span>class</span> <span>PingClientHandler</span> <span>extends</span> <span>ChannelInboundHandlerAdapter</span> <span>{</span></div><div> <span>private</span> <span>final</span> <span>ByteBuf</span> <span>firstMessage</span><span>;</span></div><div> </div><div> <span>public</span> <span>PingClientHandler</span><span>()</span> <span>{</span></div><div> <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> </div><div> <span>// weixin 16 byte's header</span></div><div> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>0</span><span>);</span></div><div> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>0</span><span>);</span></div><div> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>0</span><span>);</span></div><div> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>16</span><span>);</span></div><div> </div><div> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>0</span><span>);</span></div><div> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>16</span><span>);</span></div><div> </div><div> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>0</span><span>);</span></div><div> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>1</span><span>);</span></div><div> </div><div> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>0</span><span>);</span></div><div> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>0</span><span>);</span></div><div> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>0</span><span>);</span></div><div> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>6</span><span>);</span></div><div> </div><div> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>0</span><span>);</span></div><div> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>0</span><span>);</span></div><div> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>0</span><span>);</span></div><div> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>1</span><span>);</span></div><div> </div><div> <span>// just for /n</span></div><div> <span>firstMessage</span><span>.</span><span>writeByte</span><span>(</span><span>'\n'</span><span>);</span> <span>// 1 byte</span></div><div> </div><div> <span>// footer 16 byte</span></div><div> <span>String</span> <span>welcome</span> <span>=</span> <span>"hello"</span><span>;</span> <span>// 5 byte</span></div><div> <span>firstMessage</span><span>.</span><span>writeBytes</span><span>(</span><span>welcome</span><span>.</span><span>getBytes</span><span>());</span></div><div> <span>}</span></div><div> </div><div> <span>@Override</span></div><div> <span>public</span> <span>void</span> <span>channelActive</span><span>(</span><span>ChannelHandlerContext</span> <span>ctx</span><span>)</span> <span>{</span></div><div> <span>ctx</span><span>.</span><span>writeAndFlush</span><span>(</span><span>firstMessage</span><span>);</span></div><div> <span>}</span></div><div> </div><div> <span>@Override</span></div><div> <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> <span>throws</span> <span>Exception</span> <span>{</span></div><div> <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> <span>@Override</span></div><div> <span>public</span> <span>void</span> <span>run</span><span>()</span> <span>{</span></div><div> <span>ctx</span><span>.</span><span>channel</span><span>().</span><span>writeAndFlush</span><span>(</span><span>msg</span><span>);</span></div><div> <span>}</span></div><div> <span>},</span> <span>1</span><span>,</span> <span>TimeUnit</span><span>.</span><span>SECONDS</span><span>);</span></div><div> <span>}</span></div><div> </div><div> <span>@Override</span></div><div> <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> <span>ctx</span><span>.</span><span>flush</span><span>();</span></div><div> <span>}</span></div><div> </div><div> <span>@Override</span></div><div> <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> <span>System</span><span>.</span><span>err</span><span>.</span><span>println</span><span>(</span><span>"Unexpected exception from downstream :"</span></div><div> <span>+</span> <span>cause</span><span>.</span><span>getMessage</span><span>());</span></div><div> <span>ctx</span><span>.</span><span>close</span><span>();</span></div><div> <span>}</span></div><div><span>}</span></div><div> </div><div><span>public</span> <span>class</span> <span>PingClient</span> <span>{</span></div><div> </div><div> <span>private</span> <span>final</span> <span>String</span> <span>host</span><span>;</span></div><div> <span>private</span> <span>final</span> <span>int</span> <span>port</span><span>;</span></div><div> </div><div> <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> <span>this</span><span>.</span><span>host</span> <span>=</span> <span>host</span><span>;</span></div><div> <span>this</span><span>.</span><span>port</span> <span>=</span> <span>port</span><span>;</span></div><div> <span>}</span></div><div> </div><div> <span>public</span> <span>void</span> <span>run</span><span>()</span> <span>throws</span> <span>Exception</span> <span>{</span></div><div> <span>EventLoopGroup</span> <span>group</span> <span>=</span> <span>new</span> <span>NioEventLoopGroup</span><span>();</span></div><div> <span>try</span> <span>{</span></div><div> <span>Bootstrap</span> <span>b</span> <span>=</span> <span>new</span> <span>Bootstrap</span><span>();</span></div><div> <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> <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> <span>.</span><span>handler</span><span>(</span><span>new</span> <span>ChannelInitializer</span><span><</span><span>SocketChannel</span><span>>()</span> <span>{</span></div><div> <span>@Override</span></div><div> <span>public</span> <span>void</span> <span>initChannel</span><span>(</span><span>SocketChannel</span> <span>ch</span><span>)</span></div><div> <span>throws</span> <span>Exception</span> <span>{</span></div><div> <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> <span>}</span></div><div> <span>});</span></div><div> </div><div> <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> </div><div> <span>f</span><span>.</span><span>channel</span><span>().</span><span>closeFuture</span><span>().</span><span>sync</span><span>();</span></div><div> <span>}</span> <span>finally</span> <span>{</span></div><div> <span>// Shut down the event loop to terminate all threads.</span></div><div> <span>group</span><span>.</span><span>shutdownGracefully</span><span>();</span></div><div> <span>}</span></div><div> <span>}</span></div><div> </div><div> <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> <span>String</span> <span>host</span> <span>=</span> <span>"127.0.0.1"</span><span>;</span></div><div> <span>int</span> <span>port</span> <span>=</span> <span>8080</span><span>;</span></div><div> </div><div> <span>if</span> <span>(</span><span>args</span><span>.</span><span>length</span> <span>==</span> <span>3</span><span>)</span> <span>{</span></div><div> <span>host</span> <span>=</span> <span>args</span><span>[</span><span>0</span><span>];</span></div><div> <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> <span>}</span></div><div> </div><div> <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> <span>}</span></div><div><span>}</span></div></span>

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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzzpfz.html