基于Netty5.0中级案例五之Netty与C#Socket收发字符串进行通信

转自:http://www.itstack.org/?post=18

前言介绍:

    本案例主要介绍如何在JavaNetty与C#Sokcet进行字符串通信,Java服务端,C#客户端。

    重点提示:网络通信中都是byte字节,两边通信一定要统一编码,尽量避免乱码与接收不到的问题。

环境需求:【一下内容下文提供下载】

    1、Java

        1.1、jdk1.7

        1.2、Eclipse

    2、C#

        2.1、.net 3.5

扫描二维码关注公众号,回复: 3573709 查看本文章

        2.2、vs2008

工程介绍

Java

工程截图:

   

重点代码:

代码中,重点提示了编码、转码过程中统一使用UTF-8

 
  1. package com.itstack.netty;
  2.  
  3. import java.nio.charset.Charset;
  4.  
  5. import io.netty.channel.ChannelInitializer;
  6. import io.netty.channel.socket.SocketChannel;
  7. import io.netty.handler.codec.LineBasedFrameDecoder;
  8. import io.netty.handler.codec.string.StringDecoder;
  9. import io.netty.handler.codec.string.StringEncoder;
  10.  
  11. public class ChildChannelHandler extends ChannelInitializer<SocketChannel> {
  12.  
  13. @Override
  14. protected void initChannel(SocketChannel e) throws Exception {
  15.  
  16. System.out.println("报告");
  17. System.out.println("信息:有一客户端链接到本服务端");
  18. System.out.println("IP:" + e.localAddress().getHostName());
  19. System.out.println("Port:" + e.localAddress().getPort());
  20. System.out.println("报告完毕");
  21.  
  22. //字符串类解析
  23. e.pipeline().addLast(new LineBasedFrameDecoder(1024));
  24. //设置解码为UTF-8
  25. e.pipeline().addLast(new StringDecoder(Charset.forName("utf-8")));
  26. //设置编码为UTF-8
  27. e.pipeline().addLast(new StringEncoder(Charset.forName("utf-8")));
  28.  
  29. // 在管道中添加我们自己的接收数据实现方法
  30. e.pipeline().addLast(new MyServerHanlder());
  31.  
  32. }
  33.  
  34. }

C#

工程截图:

重点代码:

 
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Net;
  7. using System.Net.Sockets;
  8.  
  9. namespace TestSocketSendAndReceiveProtobufByJavaNettyServer
  10. {
  11.  
  12. public class Program
  13. {
  14. private Thread _ReceiveThread = null;
  15. private Socket clientSocket = null;
  16. private static byte[] result = new byte[1024];
  17.  
  18. public Program()
  19. {
  20. //温馨提示
  21. Console.WriteLine("按任意键链接服务端IP:127.0.0.1 PORT:7397");
  22. Console.ReadKey();
  23. Console.WriteLine("连续向服务端发送十次信息\r\n");
  24.  
  25. //设定服务器IP地址
  26. IPAddress ip = IPAddress.Parse("127.0.0.1");
  27. //配置服务器IP与端口
  28. clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  29. //链接服务端
  30. clientSocket.Connect(new IPEndPoint(ip, 7397));
  31.  
  32. //初始化线程
  33. _ReceiveThread = new Thread(new ThreadStart(Receive));
  34. //开启线程[用于接收数据]
  35. _ReceiveThread.Start();
  36.  
  37. //发送数据
  38. Send();
  39.  
  40. //停留
  41. Console.ReadKey();
  42. }
  43.  
  44. /// <summary>
  45. /// 发送数据
  46. /// </summary>
  47. public void Send()
  48. {
  49. for (int i = 0; i < 10; i++)
  50. {
  51. Thread.Sleep(1000);
  52. //默认编码
  53. //clientSocket.Send(System.Text.Encoding.Default.GetBytes("你好\r\n"));
  54. //UTF8编码
  55. clientSocket.Send(System.Text.Encoding.UTF8.GetBytes((i+1)+"=> Netty服务端您好\r\n"));
  56. }
  57. }
  58.  
  59. /// <summary>
  60. /// 接收数据线程
  61. /// </summary>
  62. public void Receive()
  63. {
  64. int receiveLength = 0;
  65.  
  66. try
  67. {
  68. while ((receiveLength = clientSocket.Receive(result)) > 0)
  69. {
  70. try
  71. {
  72. //UTF8解码
  73. Console.WriteLine("接收服务器消息:{0}", Encoding.UTF8.GetString(result, 0, receiveLength));
  74. }
  75. catch (Exception ex)
  76. {
  77.  
  78. }
  79. }
  80.  
  81. Console.WriteLine(receiveLength);
  82. }
  83. catch (Exception)
  84. {
  85.  
  86. throw;
  87. }
  88. }
  89.  
  90. static void Main(string[] args)
  91. {
  92. new Program();
  93. }
  94. }
  95. }

运行截图:

源码下载:

TestNettyServerStrDemo.zip

TestSocketSendAndReceiveStrByJavaNettyServer.zip

猜你喜欢

转载自blog.csdn.net/hemeinvyiqiluoben/article/details/82941957