110,533
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace SocketUp
{
/// <summary>
/// 通知上层有数据到达委托
/// </summary>
/// <param name="stateObject"></param>
public delegate void DataArrivalHandler(StateObject stateObject);
/// <summary>
/// UDP服务器
/// </summary>
public class UDPServer
{
/// <summary>
/// 要监听的端口号
/// </summary>
private Int32 _Port;
/// <summary>
/// 缓存队列最大数量
/// </summary>
private readonly Int32 _QueueMaxCount = 1024;
/// <summary>
/// 缓存队列
/// </summary>
private Queue<IAsyncResult> _StateObjectQueue;
/// <summary>
/// 通知上层有数据到达事件
/// </summary>
public event DataArrivalHandler OnDataArrival;
/// <summary>
/// Socket
/// </summary>
private Socket _Socket;
/// <summary>
/// 是否开启服务
/// </summary>
private Boolean _Active;
/// <summary>
/// 创建一个UDPServer
/// </summary>
/// <param name="port">要监听的端口号</param>
public UDPServer(Int32 port)
{
// 初始化缓存队列
_StateObjectQueue = new Queue<IAsyncResult>(_QueueMaxCount);
// 端口号
_Port = port;
}
/// <summary>
/// 通知上层有数据到达事件
/// </summary>
/// <param name="stateObject"></param>
private void DataArrival(StateObject stateObject)
{
if (OnDataArrival != null)
{
OnDataArrival(stateObject);
}
}
/// <summary>
/// 启动
/// </summary>
public void Start()
{
if (!_Active)
{
_Socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Any, _Port);
_Socket.Bind(ipEndPoint);
StateObject stateObject = new StateObject();
stateObject.WorkSocket = _Socket;
EndPoint remoteIP = new IPEndPoint(IPAddress.Any, 0);
// 开始接收
_Socket.BeginReceiveFrom(stateObject.Buffer, 0, stateObject.Buffer.Length, SocketFlags.None, ref remoteIP, ProcessReceive, stateObject);
// 多线程处理数据
for (Int32 i = 0; i < System.Environment.ProcessorCount; i++)
{
Thread thread = new Thread(new ThreadStart(ProcessDataThread));
thread.IsBackground = true;
thread.Start();
}
// 服务启动
_Active = true;
}
}
/// <summary>
/// 接收数据,先扔缓存区
/// </summary>
/// <param name="ar"></param>
private void ProcessReceive(IAsyncResult ar)
{
lock (_StateObjectQueue)
{
if (_StateObjectQueue.Count < _QueueMaxCount)
{
_StateObjectQueue.Enqueue(ar);
}
}
}
/// <summary>
/// 处理数据,并且通知上层
/// </summary>
private void ProcessDataThread()
{
while (_Active)
{
IAsyncResult asyncResult = null;
lock (_StateObjectQueue)
{
if (_StateObjectQueue.Count > 0)
{
asyncResult = _StateObjectQueue.Dequeue();
}
}
System.Threading.Thread.Sleep(1);
if (asyncResult != null)
{
StateObject stateObject = asyncResult.AsyncState as StateObject;
EndPoint remoteIP = new IPEndPoint(IPAddress.Any, 0);
Int32 receiveLength = stateObject.WorkSocket.EndReceiveFrom(asyncResult, ref remoteIP);
stateObject.RemoteIP = remoteIP;
stateObject.ReceiveLength = receiveLength;
// 触发事件通知上层
DataArrival(stateObject);
// 继续接收
stateObject.WorkSocket.BeginReceiveFrom(stateObject.Buffer, 0, stateObject.Buffer.Length, SocketFlags.None, ref remoteIP, ProcessReceive, stateObject);
}
}
}
/// <summary>
/// 停止服务
/// </summary>
public void Stop()
{
if (_Active)
{
_Active = false;
if (_Socket != null)
{
_Socket.Close();
_Socket = null;
}
}
}
}
}
using System;
using System.Net;
using System.Net.Sockets;
namespace SocketUp
{
/// <summary>
/// 传输对象
/// </summary>
public class StateObject
{
public Socket WorkSocket = null;
public Int32 ReceiveLength = 0;
// 5K
public Byte[] Buffer = new Byte[5120];
public EndPoint RemoteIP = null;
}
}
using System;
using System.Windows.Forms;
using SocketUp;
namespace Server
{
public partial class FormMain : Form
{
UDPServer _UDPServer;
public FormMain()
{
InitializeComponent();
}
private void DataArrival(StateObject stateObject)
{
// 分析协议
}
private void btnStart_Click(object sender, EventArgs e)
{
if (_UDPServer == null)
{
_UDPServer = new UDPServer(1024);
_UDPServer.OnDataArrival += DataArrival;
_UDPServer.Start();
}
}
private void btnStop_Click(object sender, EventArgs e)
{
if (_UDPServer != null)
{
_UDPServer.Stop();
_UDPServer = null;
}
}
}
}