原创性申明
本文作者: 小竹zz 本文地址:http://blog.csdn.net/zhujunxxxxx 转载请注明出处。
介绍
我之前写过一篇IOCP的文章:
http://blog.csdn.net/zhujunxxxxx/article/details/43573879 这个比异步socket性能好,因为它复用对象了。
在c#中微软已经提供了TcpListener和TcpClient来实现Tcp的通讯,这部分已经有人写了比较好的异步服务器代码 http://www.cnblogs.com/gaochundong/archive/2013/04/14/csharp_async_tcp_server.html 这位博主写的博客质量真是高,我经常浏览他的博客总是有很多让我惊喜的地方,他使用的是TcpListener来实现的异步服务器的。
我的socket版本其实本质上和他的没有区别,就只是改写了一点点,所以在这里贴一份代码就是了,多的不解释了
代码
服务器核心代码 AsyncServer.cs
/// <summary>
/// 异步SOCKET 服务器
/// </summary>
public class AsyncServer : IDisposable
{
#region Fields
/// <summary>
/// 服务器程序允许的最大客户端连接数
/// </summary>
private int _maxClient;
/// <summary>
/// 当前的连接的客户端数
/// </summary>
private int _clientCount;
/// <summary>
/// 服务器使用的异步socket
/// </summary>
private Socket _serverSock;
/// <summary>
/// 客户端会话列表
/// </summary>
private List<Session> _clients;
private bool disposed = false;
#endregion
#region Properties
/// <summary>
/// 服务器是否正在运行
/// </summary>
public bool IsRunning { get; private set; }
/// <summary>
/// 监听的IP地址
/// </summary>
public IPAddress Address { get; private set; }
/// <summary>
/// 监听的端口
/// </summary>
public int Port { get; private set; }
/// <summary>
/// 通信使用的编码
/// </summary>
public Encoding Encoding { get; set; }
#endregion
#region Ctors
/// <summary>
/// 异步Socket TCP服务器
/// </summary>
/// <param name="listenPort">监听的端口</param>
public AsyncServer(int listenPort)
: this(IPAddress.Any, listenPort,1024)
{
}
/// <summary>
/// 异步Socket TCP服务器
/// </summary>
/// <param name="localEP">监听的终结点</param>
public AsyncServer(IPEndPoint localEP)
: this(localEP.Address, localEP.Port,1024)
{
}
/// <summary>
/// 异步Socket TCP服务器
/// </summary>
/// <param name="localIPAddress">监听的IP地址</param>
/// <param name="listenPort">监听的端口</param>
/// <param name="maxClient">最大客户端数量</param>
public AsyncServer(IPAddress localIPAddress, int listenPort,int maxClient)
{
this.Address = localIPAddress;
this.Port = listenPort;
this.Encoding = Encoding.Default;
_maxClient = maxClient;
_clients = new List<Session>();
_serverSock = new Socket(localIPAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
}
#endregion
#region Server
/// <summary>
/// 启动服务器
/// </summary>
/// <returns>异步TCP服务器</returns>
public AsyncServer Start()
{
if (!IsRunning)
{
IsRunning = true;
_serverSock.Bind(new IPEndPoint(this.Address, this.Port));
_serverSock.Listen(1024);
_serverSock.BeginAccept(new AsyncCallback(HandleAcceptConnected), _serverSock);
}
return this;
}
/// <summary>
/// 启动服务器
/// </summary>
/// <param name="backlog">
/// 服务器所允许的挂起连接序列的最大长度
/// </param>
/// <returns>异步TCP服务器</returns>
public AsyncServer Start(int backlog)
{
if (!IsRunning)
{
IsRunning = true;
_serverSock.Bind(new IPEndPoint(this.Address, this.Port));
_serverSock.Listen(backlog);
_serverSock.BeginAccept(new AsyncCallback(HandleAcceptConnected), _serverSock);
}
return this;
}
/// <summary>
/// 停止服务器
/// </summary>
/// <returns>异步TCP服务器</returns>
public AsyncServer Stop()
{
if (IsRunning)
{
IsRunning = false;
_serverSock.Close();
//TODO 关闭对所有客户端的连接
}
return this;
}
#endregion
#region Receive
/// <summary>
/// 处理客户端连接
/// </summary>
/// <param name="ar"></param>
private void HandleAcceptConnected(IAsyncResult ar)
{
if (IsRunning)
{
Socket server = (Socket)ar.AsyncState;
Socket client = server.EndAccept(ar);
//检查是否达到最大的允许的客户端数目
if (_clientCount == _maxClient)
{
//TODO 触发事件
RaiseServerException(null);
}
else
{
Sess