本文引见
在.Net中,System.Net.Sockets 定名空间为须要严密控制收集接见的开辟人员供应了 Windows Sockets (Winsock) 接口的托管完成。System.Net 定名空间中的一切其他收集接见类都建立在该套接字Socket完成之上,如TCPClient、TCPListener 和 UDPClient 类封装有关建立到 Internet 的 TCP 和 UDP 衔接的详细信息;NetworkStream类则供应用于收集接见的基本数据流等,罕见的很多Internet效劳都能够见到Socket的踪迹,如Telnet、Http、Email、Echo等,这些效劳只管通讯协定Protocol的定义差别,然则其基本的传输都是采纳的Socket。 实在,Socket能够象流Stream一样被视为一个数据通道,这个通道架设在应用顺序端(客户端)和长途效劳器端之间,然后,数据的读取(吸收)和写入(发送)均针对这个通道来举行。
可见,在应用顺序端或许效劳器端建立了Socket对象以后,就能够运用Send/SentTo要领将数据发送到衔接的Socket,或许运用Receive/ReceiveFrom要领吸收来自衔接Socket的数据;
针对Socket编程,.NET 框架的 Socket 类是 Winsock32 API 供应的套接字效劳的托管代码版本。个中为完成收集编程供应了大批的要领,大多数情况下,Socket 类要领只是将数据封送到它们的本机 Win32 副本中并处置惩罚任何必要的平安搜检。假如你熟习Winsock API函数,那末用Socket类编写收集顺序会非常轻易,固然,假如你未曾打仗过,也不会太难题,追随下面的说明注解,你会觉察运用Socket类开辟windows 收集应用顺序本来有规可寻,它们在大多数情况下遵照大抵雷同的步骤。
本节引见运用Socket来完成一个同步的UDP效劳器。
Socket同步UDP效劳器
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using System.Net; using System.Threading; namespace NetFrame.Net.UDP.Sock.Synchronous { /// <summary> /// Socket 完成同步UDP效劳器 /// </summary> public class SocketUDPServer { #region Fields /// <summary> /// 效劳器顺序许可的最大客户端衔接数 /// </summary> private int _maxClient; /// <summary> /// 当前的衔接的客户端数 /// </summary> private int _clientCount; /// <summary> /// 效劳器运用的同步socket /// </summary> private Socket _serverSock; /// <summary> /// 客户端会话列表 /// </summary> private List<SocketUDPState> _clients; private bool disposed = false; /// <summary> /// 数据接收缓冲区 /// </summary> private byte[] _recvBuffer; #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 组织函数 /// <summary> /// 异步Socket UDP效劳器 /// </summary> /// <param name="listenPort">监听的端口</param> public SocketUDPServer(int listenPort) : this(IPAddress.Any, listenPort,1024) { } /// <summary> /// 异步Socket UDP效劳器 /// </summary> /// <param name="localEP">监听的终结点</param> public SocketUDPServer(IPEndPoint localEP) : this(localEP.Address, localEP.Port,1024) { } /// <summary> /// 异步Socket UDP效劳器 /// </summary> /// <param name="localIPAddress">监听的IP地点</param> /// <param name="listenPort">监听的端口</param> /// <param name="maxClient">最大客户端数目</param> public SocketUDPServer(IPAddress localIPAddress, int listenPort, int maxClient) { this.Address = localIPAddress; this.Port = listenPort; this.Encoding = Encoding.Default; _maxClient = maxClient; _clients = new List<SocketUDPState>(); _serverSock = new Socket(localIPAddress.AddressFamily, SocketType.Dgram, ProtocolType.Udp); _recvBuffer=new byte[_serverSock.ReceiveBufferSize]; } #endregion #region Method /// <summary> /// 启动效劳器 /// </summary> /// <returns>异步TCP效劳器</returns> public void Start() { if (!IsRunning) { IsRunning = true; _serverSock.Bind(new IPEndPoint(this.Address, this.Port)); //启动一个线程监听数据 new Thread(ReceiveData).Start(); } } /// <summary> /// 住手效劳器 /// </summary> public void Stop() { if (IsRunning) { IsRunning = false; _serverSock.Close(); //TODO 封闭对一切客户端的衔接 CloseAllClient(); } } /// <summary> /// 同步数据吸收要领 /// </summary> private void ReceiveData() { int len = -1; EndPoint remote = null; while (true) { try { len = _serverSock.ReceiveFrom(_recvBuffer, ref remote); //if (!_clients.Contains(remote)) //{ // _clients.Add(remote); //} } catch (Exception) { //TODO 非常处置惩罚操纵 RaiseOtherException(null); } } } /// <summary> /// 同步发送数据 /// </summary> public void Send(string msg, EndPoint clientip) { byte[] data = Encoding.Default.GetBytes(msg); try { _serverSock.SendTo(data, clientip); //数据发送完成事宜 RaiseCompletedSend(null); } catch (Exception) { //TODO 非常处置惩罚 RaiseOtherException(null); } } #endregion #region 事宜 /// <summary> /// 吸收到数据事宜 /// </summary> public event EventHandler<SocketUDPEventArgs> DataReceived; private void RaiseDataReceived(SocketUDPState state) { if (DataReceived != null) { DataReceived(this, new SocketUDPEventArgs(state)); } } /// <summary> /// 数据发送终了事宜 /// </summary> public event EventHandler<SocketUDPEventArgs> CompletedSend; /// <summary> /// 触发数据发送终了的事宜 /// </summary> /// <param name="state"></param> private void RaiseCompletedSend(SocketUDPState state) { if (CompletedSend != null) { CompletedSend(this, new SocketUDPEventArgs(state)); } } /// <summary> /// 收集毛病事宜 /// </summary> public event EventHandler<SocketUDPEventArgs> NetError; /// <summary> /// 触发收集毛病事宜 /// </summary> /// <param name="state"></param> private void RaiseNetError(SocketUDPState state) { if (NetError != null) { NetError(this, new SocketUDPEventArgs(state)); } } /// <summary> /// 非常事宜 /// </summary> public event EventHandler<SocketUDPEventArgs> OtherException; /// <summary> /// 触发非常事宜 /// </summary> /// <param name="state"></param> private void RaiseOtherException(SocketUDPState state, string descrip) { if (OtherException != null) { OtherException(this, new SocketUDPEventArgs(descrip, state)); } } private void RaiseOtherException(SocketUDPState state) { RaiseOtherException(state, ""); } #endregion #region Close /// <summary> /// 封闭一个与客户端之间的会话 /// </summary> /// <param name="state">须要封闭的客户端会话对象</param> public void Close(SocketUDPState state) { if (state != null) { _clients.Remove(state); _clientCount--; //TODO 触发封闭事宜 } } /// <summary> /// 封闭一切的客户端会话,与一切的客户端衔接会断开 /// </summary> public void CloseAllClient() { foreach (SocketUDPState client in _clients) { Close(client); } _clientCount = 0; _clients.Clear(); } #endregion #region 开释 /// <summary> /// Performs application-defined tasks associated with freeing, /// releasing, or resetting unmanaged resources. /// </summary> public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } /// <summary> /// Releases unmanaged and - optionally - managed resources /// </summary> /// <param name="disposing"><c>true</c> to release /// both managed and unmanaged resources; <c>false</c> /// to release only unmanaged resources.</param> 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.Sockets; using System.Net; namespace NetFrame.Net.UDP.Sock.Synchronous { public class SocketUDPState { // 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); } }
效劳器事宜参数类
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace NetFrame.Net.UDP.Sock.Synchronous { /// <summary> /// Socket完成同步UDP效劳器 /// </summary> public class SocketUDPEventArgs:EventArgs { /// <summary> /// 提醒信息 /// </summary> public string _msg; /// <summary> /// 客户端状况封装类 /// </summary> public SocketUDPState _state; /// <summary> /// 是不是已处置惩罚过了 /// </summary> public bool IsHandled { get; set; } public SocketUDPEventArgs(string msg) { this._msg = msg; IsHandled = false; } public SocketUDPEventArgs(SocketUDPState state) { this._state = state; IsHandled = false; } public SocketUDPEventArgs(string msg, SocketUDPState state) { this._msg = msg; this._state = state; IsHandled = false; } } }
以上就是C#收集编程系列文章(六)之Socket完成同步UDP效劳器的内容,更多相关内容请关注ki4网(www.ki4.cn)!