Skip to content

Instantly share code, notes, and snippets.

@roceys
Forked from yongboy/PingClient.java
Created March 22, 2016 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roceys/fd03d2ee99da9436d962 to your computer and use it in GitHub Desktop.
Save roceys/fd03d2ee99da9436d962 to your computer and use it in GitHub Desktop.
Java Ping Client
/**
* Ping Client
* @author nieyong
*/
package com.learn;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import java.util.concurrent.TimeUnit;
class PingClientHandler extends ChannelInboundHandlerAdapter {
private final ByteBuf firstMessage;
public PingClientHandler() {
firstMessage = PooledByteBufAllocator.DEFAULT.buffer(22);
// weixin 16 byte's header
firstMessage.writeByte(0);
firstMessage.writeByte(0);
firstMessage.writeByte(0);
firstMessage.writeByte(16);
firstMessage.writeByte(0);
firstMessage.writeByte(16);
firstMessage.writeByte(0);
firstMessage.writeByte(1);
firstMessage.writeByte(0);
firstMessage.writeByte(0);
firstMessage.writeByte(0);
firstMessage.writeByte(6);
firstMessage.writeByte(0);
firstMessage.writeByte(0);
firstMessage.writeByte(0);
firstMessage.writeByte(1);
// just for /n
firstMessage.writeByte('\n'); // 1 byte
// footer 16 byte
String welcome = "hello"; // 5 byte
firstMessage.writeBytes(welcome.getBytes());
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.writeAndFlush(firstMessage);
}
@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg)
throws Exception {
ctx.executor().schedule(new Runnable() {
@Override
public void run() {
ctx.channel().writeAndFlush(msg);
}
}, 1, TimeUnit.SECONDS);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
System.err.println("Unexpected exception from downstream :"
+ cause.getMessage());
ctx.close();
}
}
public class PingClient {
private final String host;
private final int port;
public PingClient(String host, int port) {
this.host = host;
this.port = port;
}
public void run() throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch)
throws Exception {
ch.pipeline().addLast(new PingClientHandler());
}
});
ChannelFuture f = b.connect(host, port).sync();
f.channel().closeFuture().sync();
} finally {
// Shut down the event loop to terminate all threads.
group.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
String host = "127.0.0.1";
int port = 8080;
if (args.length == 3) {
host = args[0];
port = Integer.parseInt(args[1]);
}
new PingClient(host, port).run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment