轻量级通信引擎StriveEngine —— C/S通信demo(附源码)
前段时间,有几个研究ESFramework网络通讯框架的朋友对我说,ESFramework有点庞大,对于他们目前的项目来说有点“杀鸡用牛刀”的意思,因为他们的项目不需要文件传送、不需要P2P、不存在好友关系、也不存在组广播、不需要服务器均衡、不需要跨服务器网络通讯、甚至都不需要使用UserID,只要一个客户端能与服务端进行简单的稳定高效的C#网络通信组件就可以了。于是,他们建议我,整一个轻量级的C#网络通信组件来满足类似他们这种项目的需求。我觉得这个建议是有道理的,于是,花了几天时间,我将ESFramework的内核抽离出来,经过修改封装后,形成了StriveEngineC#网络通信组件,其最大的特点就是稳定高效、易于使用。通过下面这个简单的demo,我们应该就能上手了。文末有demo源码下载,我们先上Demo截图:

1.StriveEngineC#网络通信组件Demo简介
该Demo总共包括三个项目:
1.StriveEngine.SimpleDemoServer:基于StriveEngine开发的服务端。
2.StriveEngine.SimpleDemoClient:基于StriveEngine开发的客户端。
3.StriveEngine.SimpleDemo:直接基于.NET的Socket开发的客户端,其目的是为了演示:在客户端不使用StriveEngine的情况下,如何与基于StriveEngine的服务端进行网络通讯。
StriveEngine 内置支持TCP/UDP、文本协议/二进制协议,该Demo我们使用TCP、文本格式的消息协议,消息的结束符为"\0"。
2.StriveEngineC#网络通信组件Demo服务端
private ITcpServerEngine tcpServerEngine;
private void button1_Click(object sender, EventArgs e)
{
try
{
//初始化并启动服务端引擎(TCP、文本协议)
this.tcpServerEngine = NetworkEngineFactory.CreateTextTcpServerEngine(int.Parse(this.textBox_port.Text), new DefaultTextContractHelper("\0"));
this.tcpServerEngine.ClientCountChanged += new CbDelegate<int>(tcpServerEngine_ClientCountChanged);
this.tcpServerEngine.ClientConnected += new CbDelegate<System.Net.IPEndPoint>(tcpServerEngine_ClientConnected);
this.tcpServerEngine.ClientDisconnected += new CbDelegate<System.Net.IPEndPoint>(tcpServerEngine_ClientDisconnected);
this.tcpServerEngine.MessageReceived += new CbDelegate<IPEndPoint, byte[]>(tcpServerEngine_MessageReceived);
this.tcpServerEngine.Initialize(); this.button1.Enabled = false;
this.textBox_port.ReadOnly = true;
this.button2.Enabled = true;
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
} void tcpServerEngine_MessageReceived(IPEndPoint client, byte[] bMsg)
{
string msg = System.Text.Encoding.UTF8.GetString(bMsg); //消息使用UTF-8编码
msg = msg.Substring(, msg.Length - ); //将结束标记"\0"剔除
this.ShowClientMsg(client, msg);
} void tcpServerEngine_ClientDisconnected(System.Net.IPEndPoint ipe)
{
string msg = string.Format("{0} 下线", ipe);
this.ShowEvent(msg);
} void tcpServerEngine_ClientConnected(System.Net.IPEndPoint ipe)
{
string msg = string.Format("{0} 上线" ,ipe);
this.ShowEvent(msg);
} void tcpServerEngine_ClientCountChanged(int count)
{
this.ShowConnectionCount(count);
} private void ShowEvent(string msg)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new CbDelegate<string>(this.ShowEvent), msg);
}
else
{
this.toolStripLabel_event.Text = msg;
}
} private void ShowClientMsg(IPEndPoint client, string msg)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new CbDelegate<IPEndPoint,string>(this.ShowClientMsg),client, msg);
}
else
{
ListViewItem item = new ListViewItem(new string[] { DateTime.Now.ToString(), client.ToString(), msg });
this.listView1.Items.Insert(, item);
}
} private void ShowConnectionCount(int clientCount)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new CbDelegate<int>(this.ShowConnectionCount), clientCount);
}
else
{
this.toolStripLabel_clientCount.Text = "在线数量: " + clientCount.ToString();
}
} private void comboBox1_DropDown(object sender, EventArgs e)
{
List<IPEndPoint> list = this.tcpServerEngine.GetClientList();
this.comboBox1.DataSource = list;
} private void button2_Click(object sender, EventArgs e)
{
try
{
IPEndPoint client = (IPEndPoint)this.comboBox1.SelectedItem;
if (client == null)
{
MessageBox.Show("没有选中任何在线客户端!");
return;
} if (!this.tcpServerEngine.IsClientOnline(client))
{
MessageBox.Show("目标客户端不在线!");
return;
} string msg = this.textBox_msg.Text + "\0";// "\0" 表示一个消息的结尾
byte[] bMsg = System.Text.Encoding.UTF8.GetBytes(msg);//消息使用UTF-8编码
this.tcpServerEngine.SendMessageToClient(client, bMsg);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
关于服务端引擎的使用,主要就以下几点:
(1)首先调用NetworkEngineFactory的CreateTextTcpServerEngine方法创建引擎(服务端、TCP、Text协议)。
(2)根据需要,预定引擎实例的某些事件(如MessageReceived事件)。
(3)调用引擎实例的Initialize方法启动网络通讯引擎。
(4)调用服务端引擎的SendMessageToClient方法,发送消息给客户端。
3.StriveEngine C#网络通信组件Demo客户端
private ITcpPassiveEngine tcpPassiveEngine;
private void button3_Click(object sender, EventArgs e)
{
try
{
//初始化并启动客户端引擎(TCP、文本协议)
this.tcpPassiveEngine = NetworkEngineFactory.CreateTextTcpPassiveEngine(this.textBox_IP.Text, int.Parse(this.textBox_port.Text), new DefaultTextContractHelper("\0"));
this.tcpPassiveEngine.MessageReceived += new CbDelegate<System.Net.IPEndPoint, byte[]>(tcpPassiveEngine_MessageReceived);
this.tcpPassiveEngine.AutoReconnect = true;//启动掉线自动重连
this.tcpPassiveEngine.ConnectionInterrupted += new CbDelegate(tcpPassiveEngine_ConnectionInterrupted);
this.tcpPassiveEngine.ConnectionRebuildSucceed += new CbDelegate(tcpPassiveEngine_ConnectionRebuildSucceed);
this.tcpPassiveEngine.Initialize(); this.button2.Enabled = true;
this.button3.Enabled = false;
MessageBox.Show("连接成功!");
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
} void tcpPassiveEngine_ConnectionRebuildSucceed()
{
if (this.InvokeRequired)
{
this.BeginInvoke(new CbDelegate(this.tcpPassiveEngine_ConnectionInterrupted));
}
else
{
this.button2.Enabled = true;
MessageBox.Show("重连成功。");
}
} void tcpPassiveEngine_ConnectionInterrupted()
{
if (this.InvokeRequired)
{
this.BeginInvoke(new CbDelegate(this.tcpPassiveEngine_ConnectionInterrupted));
}
else
{
this.button2.Enabled = false;
MessageBox.Show("您已经掉线。");
}
} void tcpPassiveEngine_MessageReceived(System.Net.IPEndPoint serverIPE, byte[] bMsg)
{
string msg = System.Text.Encoding.UTF8.GetString(bMsg); //消息使用UTF-8编码
msg = msg.Substring(, msg.Length - ); //将结束标记"\0"剔除
this.ShowMessage(msg);
} private void ShowMessage(string msg)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new CbDelegate<string>(this.ShowMessage), msg);
}
else
{
ListViewItem item = new ListViewItem(new string[] { DateTime.Now.ToString(), msg });
this.listView1.Items.Insert(, item);
}
} private void button2_Click(object sender, EventArgs e)
{
string msg = this.textBox_msg.Text + "\0";// "\0" 表示一个消息的结尾
byte[] bMsg = System.Text.Encoding.UTF8.GetBytes(msg);//消息使用UTF-8编码
this.tcpPassiveEngine.SendMessageToServer(bMsg);
}
关于客户端引擎的使用,与服务端类似:
(1)首先调用NetworkEngineFactory的CreateTextTcpPassiveEngine方法创建引擎(客户端、TCP、Text协议)。
(2)根据需要,预定引擎实例的某些事件(如MessageReceived、ConnectionInterrupted 事件)。
(3)根据需要,设置引擎实例的某些属性(如AutoReconnect属性)。
(4)调用引擎实例的Initialize方法启动网络通讯引擎。
(5)调用客户端引擎的SendMessageToServer方法,发送消息给服务端。
4.基于Socket的客户端
这个客户端直接基于.NET的Socket进行开发,其目演示了:在客户端不使用StriveEngineC#网络通信组件的情况下(比如客户端是异构系统),如何与基于StriveEngine的服务端进行网络通信。该客户端只是粗糙地实现了基本目的,很多细节问题都被忽略,像粘包问题、消息重组、掉线检测等等。而这些问题在实际的应用中,是必需要处理的。(StriveEngineC#网络通信组件中的客户端和服务端引擎都内置解决了这些问题)。
该客户端的代码就不贴了,大家可以在源码中看到。
5.StriveEngine C#网络通信组件Demo源码下载
附相关系列: C#网络通信组件二进制网络通讯demo源码及说明文档
C#网络通信组件打通B/S与C/S网络通讯demo源码与说明文档
版权声明:本文为博主原创文章,未经博主允许不得转载。
轻量级通信引擎StriveEngine —— C/S通信demo(附源码)的更多相关文章
- 轻量级通信引擎StriveEngine —— C/S通信demo(2) —— 使用二进制协议 (附源码)
在网络上,交互的双方基于TCP或UDP进行通信,通信协议的格式通常分为两类:文本消息.二进制消息. 文本协议相对简单,通常使用一个特殊的标记符作为一个消息的结束. 二进制协议,通常是由消息头(Head ...
- 一个简单的IM系统(Demo附源码)-- ESFramework 4.0 快速上手(08)
前面的文章已经介绍完了基于ESFramework/ESPlus进行二次开发的所有要点,现在,我们可以开始小试牛刀了. 本文将介绍使用ESFramework的Rapid引擎开发的两个最简单的Demo,E ...
- C#轻量级通通讯组件StriveEngine —— C/S通信开源demo(2) —— 使用二进制协议 (附源码)
前段时间,有几个研究ESFramework通信框架的朋友对我说,ESFramework有点庞大,对于他们目前的项目来说有点“杀鸡用牛刀”的意思,因为他们的项目不需要文件传送.不需要P2P.不存在好友关 ...
- 微服务8:通信之RPC实践篇(附源码)
★微服务系列 微服务1:微服务及其演进史 微服务2:微服务全景架构 微服务3:微服务拆分策略 微服务4:服务注册与发现 微服务5:服务注册与发现(实践篇) 微服务6:通信之网关 微服务7:通信之RPC ...
- 聊天系统Demo,增加Silverlight客户端(附源码)-- ESFramework 4.0 快速上手(09)
在ESFramework 4.0 快速上手 -- 入门Demo,一个简单的IM系统(附源码)一文中,我们介绍了使用ESFramework的Rapid引擎开发的winform聊天程序,本文我们将在之前d ...
- Managed DirectX中的DirectShow应用(简单Demo及源码)
阅读目录 介绍 准备工作 环境搭建 简单Demo 显示效果 其他 Demo下载 介绍 DirectX是Microsoft开发的基于Windows平台的一组API,它是为高速的实时动画渲染.交互式音乐和 ...
- 聊天系统Demo,增加文件传送功能(附源码)-- ESFramework 4.0 快速上手(14)
本文我们将介绍在ESFramework 4.0 快速上手(08) -- 入门Demo,一个简单的IM系统(附源码)的基础上,增加文件传送的功能.如果不了解如何使用ESFramework提供的文件传送功 ...
- 轻量级C#网络通信组件StriveEngine —— C/S通信开源demo(附源码)
前段时间,有几个研究ESFramework网络通讯框架的朋友对我说,ESFramework有点庞大,对于他们目前的项目来说有点“杀鸡用牛刀”的意思,因为他们的项目不需要文件传送.不需要P2P.不存在好 ...
- 通信服务器群集——跨服务器通信Demo(源码)
对于一些基于TCP Socket的大型C/S应用来说,能进行跨服务器通信可能是一个绕不开的功能性需求.出现这种需求的场景类似于下面描述的这种情况. 假设,我们一台TCP应用服务器能同时承载10000人 ...
随机推荐
- Scrapy框架爬虫初探——中关村在线手机参数数据爬取
关于Scrapy如何安装部署的文章已经相当多了,但是网上实战的例子还不是很多,近来正好在学习该爬虫框架,就简单写了个Spider Demo来实践.作为硬件数码控,我选择了经常光顾的中关村在线的手机页面 ...
- SSH实战 · 唯唯乐购项目(下)
后台模块 一:后台用户模块 引入后台管理页面 创建adminuser表: CREATE TABLE `adminuser` ( `uid` int(11) NOT NULL AUTO_INCREM ...
- 逆天Kali带你游遍大江南北~安全之前人铺路!
0.Linux基础学习(基本指令) http://www.cnblogs.com/dunitian/p/4822807.html 1.Kali安装到移动硬盘或者U盘中~Linux系列通用方法(包括An ...
- 【SQLServer】记一次数据迁移-标识重复的简单处理
汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 今天在数据迁移的时候因为手贱遇到一个坑爹问题,发来大家乐乐,也传授新手点经验 迁移惯用就 ...
- $.extend()的实现源码 --(源码学习1)
目标: $.extend({ add:function(a,b){ return a + b; } }) console.log($.a ...
- Angular2开发笔记
Problem 使用依赖注入应该注意些什么 服务一般用来做什么 指令一般用来做什么 angular2如何提取公共组件 angular2为什么不需要提公共组件 父组件与子组件之间如何通讯 什么时候应该使 ...
- zookeeper源码分析之五服务端(集群leader)处理请求流程
leader的实现类为LeaderZooKeeperServer,它间接继承自标准ZookeeperServer.它规定了请求到达leader时需要经历的路径: PrepRequestProcesso ...
- C#关于分页显示
---<PS:本人菜鸟,大手子还请高台贵手> 以下是我今天在做分页时所遇到的一个分页显示问题,使用拼写SQL的方式写的,同类型可参考哦~ ------------------------- ...
- Autofac - 属性注入
属性注入不同于通过构造函数方式传入参数. 这里是通过注入的方式, 在类创建完毕之后, 资源释放之前, 给属性赋值. 这里, 我重新弄一些类来演示这一篇吧. public class ClassA { ...
- c#比较两个数组的差异
将DataTable中某一列数据直接转换成数组进行比较,使用的Linq,要引用命名空间using System.Linq; string[] arrRate = dtRate.AsEnumerable ...