I am trying to send & receive a message from a server. The server has been built in a remote environment.
Server has some spesific signal codes, for example:
These codes are being sent as bytes . At this point, the problem begins.
When a client connects with a server, immediately server sends 100000050 and after a few ms, sends 10000051.
I should send 10000060 after receiving 10000051 but I don't know how.
public static void main(String[] args) throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap clientBootstrap = new Bootstrap();
clientBootstrap.group(group);
clientBootstrap.channel(NioSocketChannel.class);
clientBootstrap.remoteAddress(new InetSocketAddress("10.80.15.70", 55102));
clientBootstrap.handler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new ClientHandler());
}
});
ChannelFuture channelFuture = clientBootstrap.connect().sync();
channelFuture.channel().closeFuture().sync();
}
finally {
group.shutdownGracefully().sync();
}
}
public class ClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
private static final Logger LOGGER = Logger.getLogger( ClientHandler.class.getName() );
@Override
public void channelActive(ChannelHandlerContext channelHandlerContext){
//System.out.println("\nhelloo");
//channelHandlerContext.writeAndFlush(Unpooled.copiedBuffer("Netty Rocks!", CharsetUtil.UTF_8));
byte [] test = new byte[9];
test[0] = 0;
test[1] = 6;
test[2] = 0;
test[3] = 0;
test[4] = 0;
test[5] = 0;
test[6] = 0;
test[7] = 0;
test[8] = 1;
//byte [] message = "100000060".getBytes();
channelHandlerContext.writeAndFlush(test);
}
@Override
public void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf in) {
System.out.println("Client received: " + in.toString(CharsetUtil.UTF_8));
}
@Override
public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable cause){
cause.printStackTrace();
LOGGER.info("Exception Occurred in ChannelHandler A");
channelHandlerContext.close();
}
/*@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
System.out.println("Client received: " + o.toString());
}*/
}
Thanks.
You can do that by updating your channelRead0
method to check for message received and then replying when you get the message you were looking for.
@Override
public void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf in) {
String message = in.toString(CharsetUtil.UTF_8);
if (message.equals("10000051")) { //Now do what you were doing in channel active
byte [] test = new byte[9];
test[0] = 0;
test[1] = 6;
test[2] = 0;
test[3] = 0;
test[4] = 0;
test[5] = 0;
test[6] = 0;
test[7] = 0;
test[8] = 1;
//byte [] message = "100000060".getBytes();
channelHandlerContext.writeAndFlush(test);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.