资源简介
最近有项目要做一个高性能网络服务器,决定下功夫搞定完成端口(IOCP),最终花了一个星期终于把它弄清楚了,并用C++写了一个版本,效率很不错。
但,从项目的总体需求来考虑,最终决定上.net平台,因此又花了一天一夜弄出了一个C#版,在这与大家分享。
一些心得体会:
1、在C#中,不用去面对完成端口的操作系统内核对象,Microsoft已经为我们提供了SocketAsyncEventArgs类,它封装了IOCP的使用。请参考:http://msdn.microsoft.com/zh-cn/library/system.net.sockets.socketasynceventargs.aspx?cs-save-lang=1&cs-lang=cpp#code-snippet-1。
2、我的SocketAsyncEventArgsPool类使用List对象来存储对客户端来通信的SocketAsyncEventArgs对象,它相当于直接使用内核对象时的IoContext。我这样设计比用堆栈来实现的好处理是,我可以在SocketAsyncEventArgsPool池中找到任何一个与服务器连接的客户,主动向它发信息。而用堆栈来实现的话,要主动给客户发信息,则还要设计一个结构来存储已连接上服务器的客户。
3、对每一个客户端不管还发送还是接收,我使用同一个SocketAsyncEventArgs对象,对每一个客户端来说,通信是同步进行的,也就是说服务器高度保证同一个客户连接上要么在投递发送请求,并等待;或者是在投递接收请求,等待中。本例只做echo服务器,还未考虑由服务器主动向客户发送信息。
4、SocketAsyncEventArgs的UserToken被直接设定为被接受的客户端Socket。
5、没有使用BufferManager 类,因为我在初始化时给每一个SocketAsyncEventArgsPool中的对象分配一个缓冲区,发送时使用Arrary.Copy来进行字符拷贝,不去改变缓冲区的位置,只改变使用的长度,因此在下次投递接收请求时恢复缓冲区长度就可以了!如果要主动给客户发信息的话,可以new一个SocketAsyncEventArgs对象,或者在初始化中建立几个来专门用于主动发送信息,因为这种需求一般是进行信息群发,建立一个对象可以用于很多次信息发送,总体来看,这种花销不大,还减去了字符拷贝和消耗。
6、测试结果:(在我的笔记本上时行的,我的本本是T420 I7 8G内存)
100客户 100,000(十万次)不间断的发送接收数据(发送和接收之间没有Sleep,就一个一循环,不断的发送与接收)
耗时3004.6325 秒完成
总共 10,000,000 一千万次访问
平均每分完成 199,691.6 次发送与接收
平均每秒完成 3,328.2 次发送与接收
整个运行过程中,内存消耗在开始两三分种后就保持稳定不再增涨。
看了一下对每个客户端的延迟最多不超过2秒。

代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace IocpServer
{
public partial class MainForm : Form
{
public delegate void SetListBoxCallBack(string str);
public SetListBoxCallBack setlistboxcallback;
public void SetListBox(string str)
{
infoList.Items.Insert(0 str);
infoList.SelectedIndex = 0;
}
private IoServer iocp = new IoServer(10 1024);
public MainForm()
{
InitializeComponent();
setlistboxcallback = new SetListBoxCallBack(SetListBox);
}
private void startBtn_Click(object sender EventArgs e)
{
iocp.Start(9900);
iocp.mainForm = this;
startBtn.Enabled = false;
stopBtn.Enabled = true;
SetListBox(“监听开启...“);
}
private void stopBtn_Click(object sender EventArgs e)
{
iocp.Stop();
startBtn.Enabled = true;
stopBtn.Enabled = false;
SetListBox(“监听停止...“);
}
private void exitBtn_Click(object sender EventArgs e)
{
if (stopBtn.Enabled)
iocp.Stop();
this.Close();
}
private void clearBtn_Click(object sender EventArgs e)
{
infoList.Items.Clear();
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1561 2012-11-22 22:50 IocpServer\IocpServer\Form1.cs
文件 6990 2012-11-22 22:50 IocpServer\IocpServer\Form1.Designer.cs
文件 5817 2012-11-22 09:26 IocpServer\IocpServer\Form1.resx
文件 3477 2012-11-23 00:10 IocpServer\IocpServer\IoContextPool.cs
文件 3717 2012-11-22 19:20 IocpServer\IocpServer\IocpServer.csproj
文件 13943 2012-11-24 11:26 IocpServer\IocpServer\IoServer.cs
文件 485 2012-11-22 22:50 IocpServer\IocpServer\Program.cs
文件 1450 2012-11-22 09:26 IocpServer\IocpServer\Properties\AssemblyInfo.cs
文件 2848 2012-11-22 09:26 IocpServer\IocpServer\Properties\Resources.Designer.cs
文件 5612 2012-11-22 09:26 IocpServer\IocpServer\Properties\Resources.resx
文件 1095 2012-11-22 09:26 IocpServer\IocpServer\Properties\Settings.Designer.cs
文件 249 2012-11-22 09:26 IocpServer\IocpServer\Properties\Settings.settings
目录 0 2012-11-26 00:54 IocpServer\IocpServer\Properties
目录 0 2012-11-26 00:54 IocpServer\IocpServer
文件 872 2012-11-22 09:26 IocpServer\IocpServer.sln
目录 0 2012-11-26 00:54 IocpServer
----------- --------- ---------- ----- ----
48116 16
相关资源
- 移木块游戏,可以自编自玩,vc6.0编写
- C++纯文字DOS超小RPG游戏
- 安科瑞智能电能表MODBUS通讯程序 VC6
- 九齐单片机源码
- Qt画图工具源码(qgraphics draw)
- qt 串口助手源码
- modbus 主机源码
- 《LINUX C编程从初学到精通》光盘源码
- OLED驱动源码
- tm1650+stm32f103源码(board_tm1650.c)
- cheat engine 7.2源码
- CrySearch内存搜索器源码
- FTP客户端源码(c++)
- MFC视频播放器源码(支持avi/wma/mp3等格
- CreatBitmap图片合成源码
- vs2008 can总线通讯源码
- 宠物管理系统课程设计(源码+数据库
- Windows扩展命令程序(源码)
- c语言实现火车订票系统(控制台)源
- 鼠标连点器(附源码)
- c++ 简易贪吃蛇源码
- 杀毒软件源码
- 经典外汇智能交易程序Amazing3.1源码(
- 微型文件系统源码(FatFs)
- 海康私有流分析接口源码(附使用说
- VC6 USB开发源码
- SVM算法实现(源码+文档)
- 俄罗斯方块游戏源码(Tetris)
- 步进电机控制(源码+文档)
- c++ 定时关机程序源码
评论
共有 条评论