


C# network programming series of articles (5) Socket implements asynchronous UDP server
Introduction to this article
In .Net, the System.Net.Sockets namespace provides a managed implementation of the Windows Sockets (Winsock) interface for developers who need to tightly control network access. All other network access classes in the System.Net namespace are built on this socket implementation. The TCPClient, TCPListener, and UDPClient classes encapsulate detailed information about creating TCP and UDP connections to the Internet; the NetworkStream class provides For the basic data flow of network access, Socket can be seen in many common Internet services, such as Telnet, HTTP, Email, Echo, etc. Although these services have different definitions of communication protocols, their basic transmission uses Socket. In fact, Socket can be regarded as a data channel like a Stream. This channel is established between the application (client) and the remote server. Then, the reading (receiving) and writing (sending) of data are both for through this channel.
It can be seen that after creating a Socket object on the application side or server side, you can use the Send/SentTo method to send data to the connected Socket, or use the Receive/ReceiveFrom method to receive data from the connected Socket. Data;
For Socket programming, the .NET Framework's Socket class is a managed code version of the socket service provided by the Winsock32 API. There are a number of methods provided for implementing network programming, and in most cases the Socket class methods simply marshal the data to their native Win32 copies and handle any necessary security checks. If you are familiar with Winsock API functions, it will be very easy to use the Socket class to write network programs. Of course, if you have never been exposed to it, it will not be too difficult. Follow the explanation below and you will find that using the Socket class to develop windows
There are rules for web applications, and they follow roughly the same steps in most cases.
This section introduces the use of Socket to implement a high-performance asynchronous UDP server. In fact, UDP does not distinguish between clients and servers. , but sometimes we communicate with the server using UDP.
Socket Asynchronous UDP Server
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 { /// <summary> /// SOCKET实现异步UDP服务器 /// </summary> public class AsyncSocketUDPServer { #region Fields /// <summary> /// 服务器程序允许的最大客户端连接数 /// </summary> private int _maxClient; /// <summary> /// 当前的连接的客户端数 /// </summary> //private int _clientCount; /// <summary> /// 服务器使用的同步socket /// </summary> private Socket _serverSock; /// <summary> /// 客户端会话列表 /// </summary> //private List<AsyncUDPSocketState> _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 AsyncSocketUDPServer(int listenPort) : this(IPAddress.Any, listenPort,1024) { } /// <summary> /// 异步Socket UDP服务器 /// </summary> /// <param name="localEP">监听的终结点</param> public AsyncSocketUDPServer(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 AsyncSocketUDPServer(IPAddress localIPAddress, int listenPort, int maxClient) { this.Address = localIPAddress; this.Port = listenPort; this.Encoding = Encoding.Default; _maxClient = maxClient; //_clients = new List<AsyncUDPSocketState>(); _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)); //_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);*/ } } /// <summary> /// 停止服务器 /// </summary> public void Stop() { if (IsRunning) { IsRunning = false; _serverSock.Close(); //TODO 关闭对所有客户端的连接 } } /// <summary> /// 接收数据的方法 /// </summary> /// <param name="ar"></param> 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); } } /// <summary> /// 发送数据 /// </summary> /// <param name="msg"></param> /// <param name="remote"></param> 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 事件 /// <summary> /// 接收到数据事件 /// </summary> public event EventHandler<AsyncSocketUDPEventArgs> DataReceived; private void RaiseDataReceived(AsyncSocketUDPState state) { if (DataReceived != null) { DataReceived(this, new AsyncSocketUDPEventArgs(state)); } } /// <summary> /// 发送数据前的事件 /// </summary> public event EventHandler<AsyncSocketUDPEventArgs> PrepareSend; /// <summary> /// 触发发送数据前的事件 /// </summary> /// <param name="state"></param> private void RaisePrepareSend(AsyncSocketUDPState state) { if (PrepareSend != null) { PrepareSend(this, new AsyncSocketUDPEventArgs(state)); } } /// <summary> /// 数据发送完毕事件 /// </summary> public event EventHandler<AsyncSocketUDPEventArgs> CompletedSend; /// <summary> /// 触发数据发送完毕的事件 /// </summary> /// <param name="state"></param> private void RaiseCompletedSend(AsyncSocketUDPState state) { if (CompletedSend != null) { CompletedSend(this, new AsyncSocketUDPEventArgs(state)); } } /// <summary> /// 网络错误事件 /// </summary> public event EventHandler<AsyncSocketUDPEventArgs> NetError; /// <summary> /// 触发网络错误事件 /// </summary> /// <param name="state"></param> private void RaiseNetError(AsyncSocketUDPState state) { if (NetError != null) { NetError(this, new AsyncSocketUDPEventArgs(state)); } } /// <summary> /// 异常事件 /// </summary> public event EventHandler<AsyncSocketUDPEventArgs> OtherException; /// <summary> /// 触发异常事件 /// </summary> /// <param name="state"></param> 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 /// <summary> /// 关闭一个与客户端之间的会话 /// </summary> /// <param name="state">需要关闭的客户端会话对象</param> public void Close(AsyncSocketUDPState state) { if (state != null) { //_clients.Remove(state); //_clientCount--; //TODO 触发关闭事件 } } /// <summary> /// 关闭所有的客户端会话,与所有的客户端连接会断开 /// </summary> public void CloseAllClient() { //foreach (AsyncUDPSocketState 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 } }
Session Encapsulation Class
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 Asynchronous UDP Server Event Parameter Class
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace NetFrame.Net.UDP.Sock.Asynchronous { /// <summary> /// SOCKET 异步UDP 事件类 /// </summary> public class AsyncSocketUDPEventArgs : EventArgs { /// <summary> /// 提示信息 /// </summary> public string _msg; /// <summary> /// 客户端状态封装类 /// </summary> public AsyncSocketUDPState _state; /// <summary> /// 是否已经处理过了 /// </summary> 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; } } }
The above is the C# network programming series Article (5) Socket implements the content of asynchronous UDP server. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

.NETFrameworkisWindows-centric,while.NETCore/5/6supportscross-platformdevelopment.1).NETFramework,since2002,isidealforWindowsapplicationsbutlimitedincross-platformcapabilities.2).NETCore,from2016,anditsevolutions(.NET5/6)offerbetterperformance,cross-

The C#.NET developer community provides rich resources and support, including: 1. Microsoft's official documents, 2. Community forums such as StackOverflow and Reddit, and 3. Open source projects on GitHub. These resources help developers improve their programming skills from basic learning to advanced applications.

The advantages of C#.NET include: 1) Language features, such as asynchronous programming simplifies development; 2) Performance and reliability, improving efficiency through JIT compilation and garbage collection mechanisms; 3) Cross-platform support, .NETCore expands application scenarios; 4) A wide range of practical applications, with outstanding performance from the Web to desktop and game development.

C# is not always tied to .NET. 1) C# can run in the Mono runtime environment and is suitable for Linux and macOS. 2) In the Unity game engine, C# is used for scripting and does not rely on the .NET framework. 3) C# can also be used for embedded system development, such as .NETMicroFramework.

C# plays a core role in the .NET ecosystem and is the preferred language for developers. 1) C# provides efficient and easy-to-use programming methods, combining the advantages of C, C and Java. 2) Execute through .NET runtime (CLR) to ensure efficient cross-platform operation. 3) C# supports basic to advanced usage, such as LINQ and asynchronous programming. 4) Optimization and best practices include using StringBuilder and asynchronous programming to improve performance and maintainability.

C# is a programming language released by Microsoft in 2000, aiming to combine the power of C and the simplicity of Java. 1.C# is a type-safe, object-oriented programming language that supports encapsulation, inheritance and polymorphism. 2. The compilation process of C# converts the code into an intermediate language (IL), and then compiles it into machine code execution in the .NET runtime environment (CLR). 3. The basic usage of C# includes variable declarations, control flows and function definitions, while advanced usages cover asynchronous programming, LINQ and delegates, etc. 4. Common errors include type mismatch and null reference exceptions, which can be debugged through debugger, exception handling and logging. 5. Performance optimization suggestions include the use of LINQ, asynchronous programming, and improving code readability.

C# is a programming language, while .NET is a software framework. 1.C# is developed by Microsoft and is suitable for multi-platform development. 2..NET provides class libraries and runtime environments, and supports multilingual. The two work together to build modern applications.

C#.NET is a powerful development platform that combines the advantages of the C# language and .NET framework. 1) It is widely used in enterprise applications, web development, game development and mobile application development. 2) C# code is compiled into an intermediate language and is executed by the .NET runtime environment, supporting garbage collection, type safety and LINQ queries. 3) Examples of usage include basic console output and advanced LINQ queries. 4) Common errors such as empty references and type conversion errors can be solved through debuggers and logging. 5) Performance optimization suggestions include asynchronous programming and optimization of LINQ queries. 6) Despite the competition, C#.NET maintains its important position through continuous innovation.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
