admin 管理员组

文章数量: 1184232


2024年1月16日发(作者:c语言二维数组赋值并输出)

10. } catch (NumberFormatException e) {

11. ("Invalid Port, Start Default Port: 9090");

12. }

13. }

14. try {

15. NettyCommandServer commandServer = new NettyCommandServer();

16. ("Start listen socket, port " + port + "rn");

17. (port);

18. } catch (Exception e) {

19. (sage());

20. }

21. }

22. }

Netty服务端初始化[java]

1. public class NettyCommandServer {

2.

3. public static InternalLogger logger = tance();

4.

5. public void bind(int port) throws Exception {

6. EventLoopGroup bossGroup = new NioEventLoopGroup();

7. EventLoopGroup workerGroup = new NioEventLoopGroup();

8. try {

9. ServerBootstrap serverBootstrap = new ServerBootstrap();

10. (bossGroup, workerGroup);

11. l();

12. (_BACKLOG, 1024);

13. r(new LoggingHandler());

14. andler(new NettyChannelHandler());

15. ChannelFuture channelFuture = (port).sync();

16. l().closeFuture().sync();

17. } finally {

18. wnGracefully();

19. wnGracefully();

20. }

21. }

22.

23. private class NettyChannelHandler extends ChannelInitializer {

24.

25. @Override

26. protected void initChannel(SocketChannel socketChannel)

27. throws Exception {

28. ne().addLast(new LengthFieldBasedFrameDecoder(_ENDIAN, 64 * 1024, 0, 4, 0, 4, true));

29. ne().addLast(new StringDecoder(e("UTF-8")));

30. ne().addLast(new NettyCommandHandler());

31. }

32. }

33. }

通讯协议解析数据[java]

1. public class NettyCommandHandler extends ChannelHandlerAdapter {

2. private int counter = 0;

3.

4. @Override

5. public void channelRead(ChannelHandlerContext ctx, Object msg) {

6. try {

7. String body = (String) msg;

8. JsonDataObject request = on(body, );

9.

10. counter = counter + 1;

11. JsonDataObject response = new JsonDataObject();

12. e(0);

13. ("Success");

14. a(counter+"");

15. String respJson = (response);

16.

17. byte[] respUtf8 = es("UTF-8");

18. int respLength = ;

19. ByteBuf respLengthBuf = (4);

20. nt(respLength);

21. (_ENDIAN);

22. (respLengthBuf);

23. ByteBuf resp = ();

24. ytes(respUtf8);

25. (resp);

26. } catch (Exception e) {

27. (sage() + "rn");

28. StringWriter sw = new StringWriter();

29. PrintWriter pw = new PrintWriter(sw);

30. tackTrace(pw);

31. ();

32. ();

33. (ng());

34. }

35. }

36.

37. @Override

38. public void channelReadComplete(ChannelHandlerContext ctx) {

39. ();

40. }

41.

42. @Override

43. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {

44. ();


本文标签: 语言 赋值 数组 解析