Program.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Text;
  3. using System.Threading.Channels;
  4. using System.Threading.Tasks;
  5. using DotNetty.Buffers;
  6. using DotNetty.Transport.Bootstrapping;
  7. using DotNetty.Transport.Channels;
  8. using DotNetty.Transport.Channels.Sockets;
  9. public class TcpClientHandler : ChannelHandlerAdapter
  10. {
  11. public override void ChannelActive(IChannelHandlerContext context)
  12. {
  13. // 发送初始消息
  14. string message = "{\r\n \"station\":\"Bengbu_South_Station\",\r\n \"device\":\"KX211\",\r\n\"timestamp\":\"2024-10-09 12:12:12\",\r\n\"reason\":1,\r\n\"param\":{\r\n \"type\":\"Version_Information\",\r\n\"paras\":\r\n {\r\n \"program_version\":\"V6.0.0931\",\r\n \"program_verification_code\":\"7D4B\",\r\n \"equipment_type\":\"KF6520V6\",\r\n\"database_version\":\"20B60001\",\r\n\"logic_version\":\"000135BC1670(V7.0)\",\r\n\"hardware_version\":\"KF6500V6-CPU-P10-V1.1-V1.3\",\r\n\"boot_version\":\"1.03\",\r\n\"IEC61850_version\":\"V1.8.1\",\r\n\"FPGA_version\":\"NULL\",\r\n\"cid_file_CRC\":\"0xeeaa33cc\",\r\n\"ccd_file_CRC\":0xCEE5D436\r\n}\r\n}\r\n}\r\n";
  15. byte[] messageBytes = Encoding.UTF8.GetBytes(message);
  16. IByteBuffer buffer = Unpooled.WrappedBuffer(messageBytes);
  17. context.WriteAndFlushAsync(buffer);
  18. Console.WriteLine("Sent: " + message);
  19. }
  20. public override void ChannelRead(IChannelHandlerContext context, object message)
  21. {
  22. // 读取从服务器接收到的消息
  23. var byteBuffer = message as IByteBuffer;
  24. string response = byteBuffer.ToString(Encoding.UTF8);
  25. Console.WriteLine("Received: " + response);
  26. }
  27. public override void ExceptionCaught(IChannelHandlerContext context, Exception exception)
  28. {
  29. Console.WriteLine($"Exception: {exception}");
  30. context.CloseAsync();
  31. }
  32. }
  33. public class TcpClient
  34. {
  35. private TcpClientHandler _handler = new TcpClientHandler();
  36. public async Task RunClientAsync(string host, int port)
  37. {
  38. var group = new MultithreadEventLoopGroup();
  39. try
  40. {
  41. var bootstrap = new Bootstrap();
  42. bootstrap
  43. .Group(group)
  44. .Channel<TcpSocketChannel>()
  45. .Option(ChannelOption.TcpNodelay, true)
  46. .Handler(new ActionChannelInitializer<IChannel>(channel =>
  47. {
  48. IChannelPipeline pipeline = channel.Pipeline;
  49. pipeline.AddLast(_handler);
  50. pipeline.AddLast(new TcpClientHandler());
  51. }));
  52. IChannel clientChannel = await bootstrap.ConnectAsync(host, port);
  53. Console.WriteLine($"Connected to {host}:{port}");
  54. // 等待关闭
  55. await clientChannel.CloseAsync();
  56. }
  57. finally
  58. {
  59. await group.ShutdownGracefullyAsync(TimeSpan.FromMilliseconds(100), TimeSpan.FromSeconds(1));
  60. }
  61. }
  62. public static async Task Main(string[] args)
  63. {
  64. var client = new TcpClient();
  65. string host = "127.0.0.1";
  66. int port = 7110;
  67. await client.RunClientAsync(host, port);
  68. }
  69. }