本文作者:小竹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服务器
本节介绍使用Socket来实现一个高性能的异步UDP服务器,实际上UDP是不分客户机和服务器的,但是我们有的时候和服务器通讯就是使用UDP来进行的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace NetFrame.Net.UDP.Sock.Asynchronous
{
///
/// SOCKET实现异步UDP服务器
///
public class AsyncSocketUDPServer
{
#region Fields
///
/// 服务器程序允许的最大客户端连接数
///
private int _maxClient;
///
/// 当前的连接的客户端数
///
//private int _clientCount;
///
/// 服务器使用的同步socket
///
private Socket _serverSock;
///
/// 客户端会话列表
///
//private List _clients;
private bool disposed = false;
///
/// 数据接受缓冲区
///
private byte[] _recvBuffer;
#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 UDP服务器
///
/// 监听的端口
public AsyncSocketUDPServer(int listenPort)
: this(IPAddress.Any, listenPort,1024)
{
}
///
/// 异步Socket UDP服务器
///
/// 监听的终结点
public AsyncSocketUDPServer(IPEndPoint localEP)
: this(localEP.Address, localEP.Port,1024)
{
}
///
/// 异步Socket UDP服务器
///
/// 监听的IP地址
/// 监听的端口
/// 最大客户端数量
public AsyncSocketUDPServer(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.Dgram, ProtocolType.Udp);
_recvBuffer=new byte[_serverSock.ReceiveBufferSize];
}
#endregion
#region Method
///
/// 启动服务器
///
/// 异步TCP服务器
public void Start()
{
if (!IsRunning)
{
IsRunning = true;
_serverSock.Bind(new IPEndPoint(this.Address, this.Port));
//_serverSock.Connect(new IPEndPoint(IPAddress.Any, 0));
AsyncSocketUDPState so = new AsyncSocketUDPState();
so.workSocket = _serverSock;
_serverSock.BeginReceiveFrom(so.buffer, 0, so.buffer.Length, SocketFlags.None,
ref so.remote, new AsyncCallback(ReceiveDataAsync), null);
//EndPoint sender = new IPEndPoint(IPAddress.Any, 0);
//_serverSock.BeginReceiveFrom(_recvBuffer, 0, _recvBuffer.Length, SocketFlags.None,
// ref sender, new AsyncCallback(ReceiveDataAsync), sender);
//BeginReceive 和 BeginReceiveFrom的区别是什么
/*_serverSock.BeginReceive(_recvBuffer, 0, _recvBuffer.Length, SocketFlags.None,
new AsyncCallback(ReceiveDataAsync), null);*/
}
}
///
/// 停止服务器
///
public void Stop()
{
if (IsRunning)
{
IsRunning = false;
_serverSock.Close();
//TODO 关闭对所有客户端的连接
}
}
///
/// 接收数据的方法
///
///
private void ReceiveDataAsync(IAsyncResult ar)
{
AsyncSocketUDPState so = ar.AsyncState as AsyncSocketUDPState;
//EndPoint sender = new IPEndPoint(IPAddress.Any, 0);
int len = -1;
try
{
len = _serverSock.EndReceiveFrom(ar, ref so.remote);
//len = _serverSock.EndReceiveFrom(ar, ref sender);
//EndReceiveFrom 和 EndReceive区别
//len = _serverSock.EndReceive(ar);
//TODO 处理数据
//触发数据收到事件
RaiseDataReceived(so);
}
catch (Exception)
{
//TODO 处理异常
RaiseOtherException(so);
}
finally
{
if (IsRunning && _serverSock != null)
_serverSock.BeginReceiveFrom(so.buffer, 0, so.buffer.Length, SocketFlags.None,
ref so.remote, new AsyncCallback(ReceiveDataAsync), so);
}
}
///
/// 发送数据
///
///
///
public void Send(string msg,EndPoint remote)
{
byte[] data = Encoding.Default.GetBytes(msg);
try
{
RaisePrepareSend(null);
_serverSock.BeginSendTo(data, 0, data.Length, SocketFlags.None, remote, new AsyncCallback(SendDataEnd), _serverSock);
}
catch (Exception)
{
//TODO 异常处理
RaiseOtherException(null);
}
}
private void SendDataEnd(IAsyncResult ar)
{
((Socket)ar.AsyncState).EndSendTo(ar);
RaiseCompletedSend(null);
}
#endregion
#region 事件
///
/// 接收到数据事件
///
public event EventHandler DataReceived;
private void RaiseDataReceived(AsyncSocketUDPState state)
{
if (DataReceived != null)
{
DataReceived(this, new AsyncSocketUDPEventArgs(state));
}
}
///
/// 发送数据前的事件
///
public event EventHandler PrepareSend;
///
/// 触发发送数据前的事件
///
///
private void RaisePrepareSend(AsyncSocketUDPState state)
{
if (PrepareSend != null)
{
PrepareSend(this, new AsyncSocketUDPEventArgs(state));
}
}
///
/// 数据发送完毕事件
///
public event EventHandler CompletedSend;
///
/// 触发数据发送完毕的事件
///
///
private void RaiseCompletedSend(AsyncSocketUDPState state)
{
if (CompletedSend != null)
{
CompletedSend(this, new AsyncSocketUDPEventArgs(state));
}
}
///
/// 网络错误事件
///
public event EventHandler NetError;
///
/// 触发网络错误事件
///
///
private void RaiseNetError(AsyncSocketUDPState state)
{
if (NetError != null)
{
NetError(this, new AsyncSocketUDPEventArgs(state));
}
}
///
/// 异常事件
///
public event EventHandler OtherException;
///
/// 触发异常事件
///
///
private void RaiseOtherException(AsyncSocketUDPState state, string descrip)
{
if (OtherException != null)
{
OtherException(this, new AsyncSocketUDPEventArgs(descrip, state));
}
}
private void RaiseOtherException(AsyncSocketUDPState state)
{
RaiseOtherException(state, "");
}
#endregion
#region Close
///
/// 关闭一个与客户端之间的会话
///
/// 需要关闭的客户端会话对象
public void Close(AsyncSocketUDPState state)
{
if (state != null)
{
//_clients.Remove(state);
//_clientCount--;
//TODO 触发关闭事件
}
}
///
/// 关闭所有的客户端会话,与所有的客户端连接会断开
///
public void CloseAllClient()
{
//foreach (AsyncUDPSocketState 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;
using System.Net;
using System.Net.Sockets;
namespace NetFrame.Net.UDP.Sock.Asynchronous
{
public class AsyncSocketUDPState
{
// Client socket.
public Socket workSocket = null;
// Size of receive buffer.
public const int BufferSize = 1024;
// Receive buffer.
public byte[] buffer = new byte[BufferSize];
// Received data string.
public StringBuilder sb = new StringBuilder();
public EndPoint remote = new IPEndPoint(IPAddress.Any, 0);
}
}
Socket异步UDP服务器事件参数类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NetFrame.Net.UDP.Sock.Asynchronous
{
///
/// SOCKET 异步UDP 事件类
///
public class AsyncSocketUDPEventArgs : EventArgs
{
///
/// 提示信息
///
public string _msg;
///
/// 客户端状态封装类
///
public AsyncSocketUDPState _state;
///
/// 是否已经处理过了
///
public bool IsHandled { get; set; }
public AsyncSocketUDPEventArgs(string msg)
{
this._msg = msg;
IsHandled = false;
}
public AsyncSocketUDPEventArgs(AsyncSocketUDPState state)
{
this._state = state;
IsHandled = false;
}
public AsyncSocketUDPEventArgs(string msg, AsyncSocketUDPState state)
{
this._msg = msg;
this._state = state;
IsHandled = false;
}
}
}