资源简介
最近有项目要做一个高性能网络服务器,去网络上搜到到的都是C++版本而且是英文或者简单的DEMO,所以自己动手写了C# 的DEMO。
网络上只写接收到的数据,没有说怎么处理缓冲区数据,本DEMO简单的介绍如何处理接收到的数据。简单易用,希望对大家有用.
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秒。
![](http://www.nz998.com/pic/35511.jpg)
代码片段和文件信息
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(2 1024);
public MainForm()
{
InitializeComponent();
setlistboxcallback = new SetListBoxCallBack(SetListBox);
}
private void startBtn_Click(object sender EventArgs e)
{
iocp.Start(1086);
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();
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2013-07-02 19:13 IocpServer\
目录 0 2013-07-02 19:13 IocpServer\IocpServer\
文件 872 2012-11-22 09:26 IocpServer\IocpServer.sln
文件 27136 2013-07-03 14:34 IocpServer\IocpServer.suo
目录 0 2013-07-02 19:13 IocpServer\IocpServer\bin\
目录 0 2013-07-03 14:34 IocpServer\IocpServer\bin\Debug\
文件 16384 2013-07-03 14:34 IocpServer\IocpServer\bin\Debug\IocpServer.exe
文件 34304 2013-07-03 14:34 IocpServer\IocpServer\bin\Debug\IocpServer.pdb
文件 11600 2013-07-03 14:25 IocpServer\IocpServer\bin\Debug\IocpServer.vshost.exe
文件 490 2010-03-17 22:39 IocpServer\IocpServer\bin\Debug\IocpServer.vshost.exe.manifest
文件 1560 2013-07-02 15:33 IocpServer\IocpServer\Form1.cs
文件 6992 2013-07-02 13:15 IocpServer\IocpServer\Form1.Designer.cs
文件 5817 2013-07-02 13:15 IocpServer\IocpServer\Form1.resx
文件 3477 2012-11-23 00:10 IocpServer\IocpServer\IoContextPool.cs
文件 3717 2012-11-22 19:20 IocpServer\IocpServer\IocpServer.csproj
文件 14576 2013-07-03 14:34 IocpServer\IocpServer\IoServer.cs
目录 0 2013-07-02 19:13 IocpServer\IocpServer\obj\
目录 0 2013-07-02 19:13 IocpServer\IocpServer\obj\x86\
目录 0 2013-07-03 14:34 IocpServer\IocpServer\obj\x86\Debug\
文件 4501 2013-07-02 17:45 IocpServer\IocpServer\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
文件 6291 2013-07-03 14:34 IocpServer\IocpServer\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 674 2013-07-03 14:34 IocpServer\IocpServer\obj\x86\Debug\GenerateResource.read.1.tlog
文件 1582 2013-07-03 14:34 IocpServer\IocpServer\obj\x86\Debug\GenerateResource.write.1.tlog
文件 2021 2013-07-03 14:34 IocpServer\IocpServer\obj\x86\Debug\IocpServer.csproj.FileListAbsolute.txt
文件 16384 2013-07-03 14:34 IocpServer\IocpServer\obj\x86\Debug\IocpServer.exe
文件 180 2013-07-03 14:34 IocpServer\IocpServer\obj\x86\Debug\IocpServer.MainForm.resources
文件 34304 2013-07-03 14:34 IocpServer\IocpServer\obj\x86\Debug\IocpServer.pdb
文件 180 2013-07-03 14:34 IocpServer\IocpServer\obj\x86\Debug\IocpServer.Properties.Resources.resources
目录 0 2013-07-02 13:13 IocpServer\IocpServer\obj\x86\Debug\TempPE\
文件 485 2012-11-22 22:50 IocpServer\IocpServer\Program.cs
目录 0 2013-07-02 19:13 IocpServer\IocpServer\Properties\
............此处省略5个文件信息
相关资源
- Quectel_EC200xEG912YEC600NEC600S系列_TCP(I
- 基于STM32F407的W5500 tcpserver(官网例程
- c#与西门子1500通讯
- 基于CS的TCP文件传输程序设计
- TCP/IP客户端和服务器端源代码,好用
- modbus tcp/ip 简单通讯
- libnet发送udp和tcp包
- socket tcp应用
- TCP/IP与串口调试
- Tcp自定义命令调试工具
- Qt TCP聊天室demo
- TCP服务端和TCP客户端通讯
- c++调用C# COM 参数是结构体数组
- CTP上期技术平台API及C#封装更新到CT
- C#神思二代身份证读卡程序 demo
- C#调用百度地图获取经纬度
- 03 C#与C++dll互相传递字符串.rar
- c# 华大HD900身份证阅读器操作类
- 即时通讯开发源码终极版C#.NETC++都有
- 基于TCP协议的C/S传输文件C++)
- C#C++混合编程C#调用非托管C++类
- c#图书管理系统+详细数据库设计文档
- Visual Studio 2010 Express 学习版
- C++调用C#库例程
- C语言实现TCP/IP协议通信和UDP协议通信
- c#与STK开发
- OPC Data Access 3.00 Source Code
- C#/C++麦克风插拔检测
- 使用C#创建webservice服务,并使用服务
- MFC下TCP服务器
评论
共有 条评论