基于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
2.2、vs2008
工程介绍:
Java
工程截图:
重点代码:
代码中,重点提示了编码、转码过程中统一使用UTF-8
- package com.itstack.netty;
- import java.nio.charset.Charset;
- import io.netty.channel.ChannelInitializer;
- import io.netty.channel.socket.SocketChannel;
- import io.netty.handler.codec.LineBasedFrameDecoder;
- import io.netty.handler.codec.string.StringDecoder;
- import io.netty.handler.codec.string.StringEncoder;
- public class ChildChannelHandler extends ChannelInitializer<SocketChannel> {
- @Override
- protected void initChannel(SocketChannel e) throws Exception {
- System.out.println("报告");
- System.out.println("信息:有一客户端链接到本服务端");
- System.out.println("IP:" + e.localAddress().getHostName());
- System.out.println("Port:" + e.localAddress().getPort());
- System.out.println("报告完毕");
- //字符串类解析
- e.pipeline().addLast(new LineBasedFrameDecoder(1024));
- //设置解码为UTF-8
- e.pipeline().addLast(new StringDecoder(Charset.forName("utf-8")));
- //设置编码为UTF-8
- e.pipeline().addLast(new StringEncoder(Charset.forName("utf-8")));
- // 在管道中添加我们自己的接收数据实现方法
- e.pipeline().addLast(new MyServerHanlder());
- }
- }
C#
工程截图:
重点代码:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Net;
- using System.Net.Sockets;
- namespace TestSocketSendAndReceiveProtobufByJavaNettyServer
- {
- public class Program
- {
- private Thread _ReceiveThread = null;
- private Socket clientSocket = null;
- private static byte[] result = new byte[1024];
- public Program()
- {
- //温馨提示
- Console.WriteLine("按任意键链接服务端IP:127.0.0.1 PORT:7397");
- Console.ReadKey();
- Console.WriteLine("连续向服务端发送十次信息\r\n");
- //设定服务器IP地址
- IPAddress ip = IPAddress.Parse("127.0.0.1");
- //配置服务器IP与端口
- clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- //链接服务端
- clientSocket.Connect(new IPEndPoint(ip, 7397));
- //初始化线程
- _ReceiveThread = new Thread(new ThreadStart(Receive));
- //开启线程[用于接收数据]
- _ReceiveThread.Start();
- //发送数据
- Send();
- //停留
- Console.ReadKey();
- }
- /// <summary>
- /// 发送数据
- /// </summary>
- public void Send()
- {
- for (int i = 0; i < 10; i++)
- {
- Thread.Sleep(1000);
- //默认编码
- //clientSocket.Send(System.Text.Encoding.Default.GetBytes("你好\r\n"));
- //UTF8编码
- clientSocket.Send(System.Text.Encoding.UTF8.GetBytes((i+1)+"=> Netty服务端您好\r\n"));
- }
- }
- /// <summary>
- /// 接收数据线程
- /// </summary>
- public void Receive()
- {
- int receiveLength = 0;
- try
- {
- while ((receiveLength = clientSocket.Receive(result)) > 0)
- {
- try
- {
- //UTF8解码
- Console.WriteLine("接收服务器消息:{0}", Encoding.UTF8.GetString(result, 0, receiveLength));
- }
- catch (Exception ex)
- {
- }
- }
- Console.WriteLine(receiveLength);
- }
- catch (Exception)
- {
- throw;
- }
- }
- static void Main(string[] args)
- {
- new Program();
- }
- }
- }
运行截图:
源码下载: