本文作者:小竹zz 本文地址http://blog.csdn.net/zhujunxxxxx/article/details/44258719 转载请注明出处
C#网络编程系列文章(一)之Socket实现异步TCP服务器
C#网络编程系列文章(二)之Socket实现同步TCP服务器
C#网络编程系列文章(三)之TcpListener实现异步TCP服务器
C#网络编程系列文章(四)之TcpListener实现同步TCP服务器
C#网络编程系列文章(五)之Socket实现异步UDP服务器
C#网络编程系列文章(六)之Socket实现同步UDP服务器
C#网络编程系列文章(七)之UdpClient实现异步UDP服务器
C#网络编程系列文章(八)之UdpClient实现同步UDP服务器
http://download.csdn.net/detail/zhujunxxxxx/8510991
本人因为对于网络编程的喜爱,经常性的使用c#编写各类服务器(e.g TCP服务器,UDP服务器),但是基本上都是搞着玩,网上也有很多讲c#网络编程的文章,当然我也参考了很多作者写的文章。看了这篇文章以后再也不用导出找资料了。
本系列文章会依次介绍使用Socket实现的异步TCP服务器、同步TCP服务器、异步UDP服务器、同步UDP服务器 and 使用TcpListener和UdpClient实现的异步TCP服务器、同步TCP服务器、异步UDP服务器、同步UDP服务器。
相信搞过网络编程的人来说这个TCP一点也不陌生吧,在C#中微软已经帮我们封装过了一个TcpListener和TcpClient这两个类了,实现了对于套接字的封装,但是呢实际上还是不怎么好用,所以我们用Socket来实现一个异步的TCP服务器。
在本文中我只给出服务器端代码,客户端代码自己可以找找别处,毕竟我只是为了写出一个好的服务器端
下面是代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace NetFrame.Net.TCP.Sock.Asynchronous
{
///
/// Socket实现的异步TCP服务器
///
public class AsyncSocketTCPServer : IDisposable
{
#region Fields
///
/// 服务器程序允许的最大客户端连接数
///
private int _maxClient;
///
/// 当前的连接的客户端数
///
private int _clientCount;
///
/// 服务器使用的异步socket
///
private Socket _serverSock;
///
/// 客户端会话列表
///
private List _clients;
private bool disposed = false;
#endregion
#region Properties
///
/// 服务器是否正在运行
///
public bool IsRunning { get; private set; }
///
/// 监听的IP地址
///
public IPAddress Address { get; private set; }
///
/// 监听的端口
///
public int Port { get; private set; }
///
/// 通信使用的编码
///
public Encoding Encoding { get; set; }
#endregion
#region 构造函数
///
/// 异步Socket TCP服务器
///
/// 监听的端口
public AsyncSocketTCPServer(int listenPort)
: this(IPAddress.Any, listenPort,1024)
{
}
///
/// 异步Socket TCP服务器
///
/// 监听的终结点
public AsyncSocketTCPServer(IPEndPoint localEP)
: this(localEP.Address, localEP.Port,1024)
{
}
///
/// 异步Socket TCP服务器
///
/// 监听的IP地址
/// 监听的端口
/// 最大客户端数量
public AsyncSocketTCPServer(IPAddress localIPAddress, int listenPort,int maxClient)
{
this.Address = localIPAddress;
this.Port = listenPort;
this.Encoding = Encoding.Default;
_maxClient = maxClient;
_clients = new List();
_serverSock = new Socket(localIPAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
}
#endregion
#region Method
///
/// 启动服务器
///
public void Start()
{
if (!IsRunning)
{
IsRunning = true;
_serverSock.Bind(new IPEndPoint(this.Address, this.Port));
_serverSock.Listen(1024);
_serverSock.BeginAccept(new AsyncCallback(HandleAcceptConnected), _serverSock);
}
}
///
/// 启动服务器
///
///
/// 服务器所允许的挂起连接序列的最大长度
///
public void Start(int backlog)
{
if (!IsRunning)
{
IsRunning = true;
_serverSock.Bind(new IPEndPoint(this.Address, this.Port));
_serverSock.Listen(backlog);
_serverSock.BeginAccept(new AsyncCallback(HandleAcceptConnected), _serverSock);
}
}
///
/// 停止服务器
///
public void Stop()
{
if (IsRunning)
{
IsRunning = false;
_serverSock.Close();
//TODO 关闭对所有客户端的连接
}
}
///
/// 处理客户端连接
///
///
private void HandleAcceptConnected(IAsyncResult ar)
{
if (IsRunning)
{
Socket server = (Socket)ar.AsyncState;
Socket client = server.EndAccept(ar);
//检查是否达到最大的允许的客户端数目
if (_clientCount >= _maxClient)
{
//C-TODO 触发事件
RaiseOtherException(null);
}
else
{
AsyncSocketState state = new AsyncSocketState(client);
lock (_clients)
{
_clients.Add(state);
_clientCount++;
RaiseClientConnected(state); //触发客户端连接事件
}
state.RecvDataBuffer = new byte[client.ReceiveBufferSize];
//开始接受来自该客户端的数据
client.BeginReceive(state.RecvDataBuffer, 0, state.RecvDataBuffer.Length, SocketFlags.None,
new AsyncCallback(HandleDataReceived), state);
}
//接受下一个请求
server.BeginAccept(new AsyncCallback(HandleAcceptConnected), ar.AsyncState);
}
}
///
/// 处理客户端数据
///
///
private void HandleDataReceived(IAsyncResult ar)
{
if (IsRunning)
{
AsyncSocketState state = (AsyncSocketState)ar.AsyncState;
Socket client = state.ClientSocket;
try
{
//如果两次开始了异步的接收,所以当客户端退出的时候
//会两次执行EndReceive
int recv = client.EndReceive(ar);
if (recv == 0)
{
//C- TODO 触发事件 (关闭客户端)
Close(state);
RaiseNetError(state);
return;
}
//TODO 处理已经读取的数据 ps:数据在state的RecvDataBuffer中
//C- TODO 触发数据接收事件
RaiseDataReceived(state);
}
catch (SocketException)
{
//C- TODO 异常处理
RaiseNetError(state);
}
finally
{
//继续接收来自来客户端的数据
client.BeginReceive(state.RecvDataBuffer, 0, state.RecvDataBuffer.Length, SocketFlags.None,
new AsyncCallback(HandleDataReceived), state);
}
}
}
///
/// 发送数据
///
/// 接收数据的客户端会话
/// 数据报文
public void Send(AsyncSocketState state, byte[] data)
{
RaisePrepareSend(state);
Send(state.ClientSocket, data);
}
///
/// 异步发送数据至指定的客户端
///
/// 客户端
/// 报文
public void Send(Socket client, byte[] data)
{
if (!IsRunning)
throw new InvalidProgramException("This TCP Scoket server has not been started.");
if (client == null)
throw new ArgumentNullException("client");
if (data == null)
throw new ArgumentNullException("data");
client.BeginSend(data, 0, data.Length, SocketFlags.None,
new AsyncCallback(SendDataEnd), client);
}
///
/// 发送数据完成处理函数
///
/// 目标客户端Socket
private void SendDataEnd(IAsyncResult ar)
{
((Socket)ar.AsyncState).EndSend(ar);
RaiseCompletedSend(null);
}
#endregion
#region 事件
///
/// 与客户端的连接已建立事件
///
public event EventHandler ClientConnected;
///
/// 与客户端的连接已断开事件
///
public event EventHandler ClientDisconnected;
///
/// 触发客户端连接事件
///
///
private void RaiseClientConnected(AsyncSocketState state)
{
if (ClientConnected != null)
{
ClientConnected(this, new AsyncSocketEventArgs(state));
}
}
///
/// 触发客户端连接断开事件
///
///
private void RaiseClientDisconnected(Socket client)
{
if (ClientDisconnected != null)
{
ClientDisconnected(this, new AsyncSocketEventArgs("连接断开"));
}
}
///
/// 接收到数据事件
///
public event EventHandler DataReceived;
private void RaiseDataReceived(AsyncSocketState state)
{
if (DataReceived != null)
{
DataReceived(this, new AsyncSocketEventArgs(state));
}
}
///
/// 发送数据前的事件
///
public event EventHandler PrepareSend;
///
/// 触发发送数据前的事件
///
///
private void RaisePrepareSend(AsyncSocketState state)
{
if (PrepareSend != null)
{
PrepareSend(this, new AsyncSocketEventArgs(state));
}
}
///
/// 数据发送完毕事件
///
public event EventHandler CompletedSend;
///
/// 触发数据发送完毕的事件
///
///
private void RaiseCompletedSend(AsyncSocketState state)
{
if (CompletedSend != null)
{
CompletedSend(this, new AsyncSocketEventArgs(state));
}
}
///
/// 网络错误事件
///
public event EventHandler NetError;
///
/// 触发网络错误事件
///
///
private void RaiseNetError(AsyncSocketState state)
{
if (NetError != null)
{
NetError(this, new AsyncSocketEventArgs(state));
}
}
///
/// 异常事件
///
public event EventHandler OtherException;
///
/// 触发异常事件
///
///
private void RaiseOtherException(AsyncSocketState state, string descrip)
{
if (OtherException != null)
{
OtherException(this, new AsyncSocketEventArgs(descrip, state));
}
}
private void RaiseOtherException(AsyncSocketState state)
{
RaiseOtherException(state, "");
}
#endregion
#region Close
///
/// 关闭一个与客户端之间的会话
///
/// 需要关闭的客户端会话对象
public void Close(AsyncSocketState state)
{
if (state != null)
{
state.Datagram = null;
state.RecvDataBuffer = null;
_clients.Remove(state);
_clientCount--;
//TODO 触发关闭事件
state.Close();
}
}
///
/// 关闭所有的客户端会话,与所有的客户端连接会断开
///
public void CloseAllClient()
{
foreach (AsyncSocketState client in _clients)
{
Close(client);
}
_clientCount = 0;
_clients.Clear();
}
#endregion
#region 释放
///
/// Performs application-defined tasks associated with freeing,
/// releasing, or resetting unmanaged resources.
///
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Releases unmanaged and - optionally - managed resources
///
/// true to release
/// both managed and unmanaged resources; false
/// to release only unmanaged resources.
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
try
{
Stop();
if (_serverSock != null)
{
_serverSock = null;
}
}
catch (SocketException)
{
//TODO
RaiseOtherException(null);
}
}
disposed = true;
}
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NetFrame.Net.TCP.Sock.Asynchronous
{
///
/// 异步Socket TCP事件参数类
///
public class AsyncSocketEventArgs:EventArgs
{
///
/// 提示信息
///
public string _msg;
///
/// 客户端状态封装类
///
public AsyncSocketState _state;
///
/// 是否已经处理过了
///
public bool IsHandled { get; set; }
public AsyncSocketEventArgs(string msg)
{
this._msg = msg;
IsHandled = false;
}
public AsyncSocketEventArgs(AsyncSocketState state)
{
this._state = state;
IsHandled = false;
}
public AsyncSocketEventArgs(string msg, AsyncSocketState state)
{
this._msg = msg;
this._state = state;
IsHandled = false;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
namespace NetFrame.Net.TCP.Sock.Asynchronous
{
///
/// 异步SOCKET TCP 中用来存储客户端状态信息的类
///
public class AsyncSocketState
{
#region 字段
///
/// 接收数据缓冲区
///
private byte[] _recvBuffer;
///
/// 客户端发送到服务器的报文
/// 注意:在有些情况下报文可能只是报文的片断而不完整
///
private string _datagram;
///
/// 客户端的Socket
///
private Socket _clientSock;
#endregion
#region 属性
///
/// 接收数据缓冲区
///
public byte[] RecvDataBuffer
{
get
{
return _recvBuffer;
}
set
{
_recvBuffer = value;
}
}
///
/// 存取会话的报文
///
public string Datagram
{
get
{
return _datagram;
}
set
{
_datagram = value;
}
}
///
/// 获得与客户端会话关联的Socket对象
///
public Socket ClientSocket
{
get
{
return _clientSock;
}
}
#endregion
///
/// 构造函数
///
/// 会话使用的Socket连接
public AsyncSocketState(Socket cliSock)
{
_clientSock = cliSock;
}
///
/// 初始化数据缓冲区
///
public void InitBuffer()
{
if (_recvBuffer == null&&_clientSock!=null)
{
_recvBuffer=new byte[_clientSock.ReceiveBufferSize];
}
}
///
/// 关闭会话
///
public void Close()
{
//关闭数据的接受和发送
_clientSock.Shutdown(SocketShutdown.Both);
//清理资源
_clientSock.Close();
}
}
}