标签:readline none received 其它 ntc 情况 var 版本 change
文章中的StriveEngine.dll版本为V3.9.0.0,源码下载请到 https://download.csdn.net/download/hanghangz/10966335
先上代码,建立2个控制台程序,分别为SEClient,SEServer,其中SEClient中增加一个类
class Client { private ITcpPassiveEngine tcpPassiveEngine; public Client(string ip,string port) { this.tcpPassiveEngine = NetworkEngineFactory.CreateTextTcpPassiveEngine(ip, int.Parse(port), new TextContractHelper()); } public void Start() { this.tcpPassiveEngine.MessageReceived += new CbDelegate<System.Net.IPEndPoint, byte[]>(tcpPassiveEngine_MessageReceived); this.tcpPassiveEngine.AutoReconnect = true;//启动掉线自动重连 this.tcpPassiveEngine.ConnectionInterrupted += () => { Console.WriteLine("Offline"); }; this.tcpPassiveEngine.ConnectionRebuildSucceed += () => { Console.WriteLine("Re-Collect OK"); }; this.tcpPassiveEngine.Initialize(); } public void Send(string message) { byte[] bytes = System.Text.Encoding.UTF8.GetBytes(message); this.tcpPassiveEngine.SendMessageToServer(bytes); } private void tcpPassiveEngine_MessageReceived(System.Net.IPEndPoint obj1, byte[] obj2) { string message = System.Text.Encoding.UTF8.GetString(obj2); Console.WriteLine("get message from [{0}]:{1}", obj1.ToString(), message); } }
Main函数如下
static void Main(string[] args) { Client c = new Client("127.0.0.1", "1003"); c.Start(); while (true) { string m = Console.ReadLine(); c.Send(m); } }
在SEServer添加类
class Server { private ITcpServerEngine tcpServerEngine; public Server(int port) { this.tcpServerEngine = NetworkEngineFactory.CreateTextTcpServerEngine(port, new TextContractHelper()); } public void Start() { this.tcpServerEngine.ClientCountChanged += (a) => { Console.WriteLine("Client count: " + a.ToString()); }; this.tcpServerEngine.ClientConnected += (a) => { Console.WriteLine("Online " + a.ToString()); }; this.tcpServerEngine.ClientDisconnected += (a) => { Console.WriteLine("Offline " + a.ToString()); }; this.tcpServerEngine.MessageReceived += new CbDelegate<IPEndPoint, byte[]>(tcpServerEngine_MessageReceived); this.tcpServerEngine.Initialize(); } private void tcpServerEngine_MessageReceived(IPEndPoint obj1, byte[] obj2) { string message = System.Text.Encoding.UTF8.GetString(obj2); Console.WriteLine("get message from [{0}]:{1}", obj1.ToString(), message); this.tcpServerEngine.SendMessageToClient(obj1, obj2); } }
Main函数如下:
static void Main(string[] args) { Server s = new Server(1003); s.Start(); Console.ReadKey(); }
将下面的类增加到客户端和服务端中,也可直接添加链接(不知道怎么弄?那就都添加吧):
public class TextContractHelper : ITextContractHelper { public List<byte[]> EndTokens { get { return null; } } }
然后先启动服务端,再启动客户端.在客户端输入任意字符回车,服务端将会收到,并直接将其返回客户端. 所有的连接,断线,重连,收发消息,显示客户端数量,全部搞定,惊喜不?
然后,我们来捣乱,把TextContractHelper修改了
public class TextContractHelper : ITextContractHelper { public List<byte[]> EndTokens { get { var token = new byte[][] { new byte[] { (byte)78, (byte)79 } };//表示字符串"NO" return token.ToList(); } } }
然后再运行程序,在客户端输入,发现服务端收不到数据了.喔喝.怎么肥事?
查看EndTokens的解释
消息结束标识符(经过编码后得到的字节数组)的集合。 比如一般应用使用"\0"作为消息结束标志,那么,集合中只有一个元素("\0"的二进制)。 有的应用可能有多个标识符(如"\0"、"\n"及其它)都可以作为消息的结束标志,则集合中就有多个元素。 如果设置为null,引擎则不进行消息完整性识别及构造,每次接收到数据,就直接触发MessageReceived事件。
所以,在客户端输入结尾输入NO,在回车,就能收到信息了.还可以输入多行,在最后才输入NO,所有的输入将会作为一条消息.
上面的代码中用的CreateTextTcpPassiveEngine和CreateTextTcpServerEngine来获得tcpServerEngine,它还有另外一组方法CreateStreamTcpServerEngine和CreateStreamTcpPassivEngine,而后面的参数是IStreamContractHelper.
区别:
还是来试一下.这里先定义一个报文的格式
报文构造类:
class Message { public byte[] Head; public byte[] Body; public Message(byte func, string body) { Body = System.Text.Encoding.UTF8.GetBytes(body); Head = new byte[4]; Head[0] = 0xff; Head[1] = (byte)Body.Length; Head[2] = func; Head[3] = 0x00; } public byte[] ToBytes() { List<byte> list = new List<byte>(); list.AddRange(Head); list.AddRange(Body); return list.ToArray(); } }
报文解析类:
ublic class StreamContractHelper : IStreamContractHelper { public int MessageHeaderLength//长度为4 { get { return 4; } } public int ParseMessageBodyLength(byte[] head)// { byte[] len = new byte[] { head[1] ,0,0,0};//head中第二位表示body的长度,添加3个字节,转换为int return BitConverter.ToInt32(len, 0); } }
服务端中:
this.tcpServerEngine = NetworkEngineFactory.CreateStreamTcpServerEngine(port, new StreamContractHelper());
客户端中:
this.tcpPassiveEngine = NetworkEngineFactory.CreateStreamTcpPassivEngine(ip, int.Parse(port), new StreamContractHelper());
客户端的Send方法修改
public void Send(string message) { Message msg = new Message((byte)0x01, message); this.tcpPassiveEngine.SendMessageToServer(msg.ToBytes()); }
服务端中收到消息处理的解析:
private void tcpServerEngine_MessageReceived(IPEndPoint obj1, byte[] obj2) { //string message = System.Text.Encoding.UTF8.GetString(obj2); //Console.WriteLine("get message from [{0}]:{1}", obj1.ToString(), message); //this.tcpServerEngine.SendMessageToClient(obj1, obj2); string message= System.Text.Encoding.UTF8.GetString(obj2, 4, obj2.Length - 4); Console.WriteLine(message); }
运行后,又可以收服务端的消息了.非常NICE.
需要注意的问题:
需要注意的问题2:
Message的构造函数修改为如下,就是长度用int,4个字节来表示
public Message(byte func, string body) { Body = System.Text.Encoding.UTF8.GetBytes(body); Head = new byte[7]; Head[0] = 0xff; byte[] len = BitConverter.GetBytes(body.Length); Head[1] = len[0]; Head[2] = len[1]; Head[3] = len[2]; Head[4] = len[3]; Head[5] = func; Head[6] = 0x00; }
同时,StreamContractHelper修改为如下
public class StreamContractHelper : IStreamContractHelper { public int MessageHeaderLength { get { return 7; }//head长度为7了 } public int ParseMessageBodyLength(byte[] head) { int l = BitConverter.ToInt32(head, 1);//head[1,2,3,4]4个字节表示body的LEN,由于是TOINT32,方法自己从1开始,取4个字节来转换 return l; } }
标签:readline none received 其它 ntc 情况 var 版本 change
原文地址:https://www.cnblogs.com/birds-zhu/p/10405693.html