Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ServerHandler extends ChannelInboundHandlerAdapter
- {
- @Override
- public void channelRead(ChannelHandlerContext context, Object msg)
- {
- ByteBuf in = (ByteBuf) msg;
- try
- {
- while (in.isReadable())
- {
- System.out.println((char) in.readByte());
- System.out.flush();
- }
- ByteBuf buf = createBuf(Server.getServerData());
- this.sendBuf(context, buf);
- }
- finally
- {
- ReferenceCountUtil.release(msg);
- }
- }
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
- {
- cause.printStackTrace();
- ctx.close();
- }
- private void sendBuf(ChannelHandlerContext context, ByteBuf buf)
- {
- context.pipeline().firstContext().writeAndFlush(buf).addListener(ChannelFutureListener.CLOSE);
- }
- private ByteBuf createBuf(String data)
- {
- ByteBuf buf = Unpooled.buffer();
- buf.writeByte(255);
- char[] characters = data.toCharArray();
- buf.writeShort(characters.length);
- char[] chars = characters;
- int length = characters.length;
- for (int i = 0; i < length; ++i)
- {
- char character = chars[i];
- buf.writeChar(character);
- }
- return buf;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement