当前位置:编程学习 > C#/ASP.NET >>

高性能异步Socket服务器(UDP)

采用异步模式设计的UDP服务器,源码如下:

 

1 using System;
2  using System.Net;
3  using System.Net.Sockets;
4 using System.ServiceProcess;
5 using System.Threading;
6
7 namespace TestUdpServer
8 {
9 // this class encapsulates a single packet that
10 // is either sent or received by a UDP socket
11 public class UDPPacketBuffer
12 {
13 // size of the buffer
14 public const int BUFFER_SIZE = 4096;
15
16 // the buffer itself
17 public byte[] Data;
18
19 // length of data to transmit
20 public int DataLength;
21
22 // the (IP)Endpoint of the remote host
23 public EndPoint RemoteEndPoint;
24
25 public UDPPacketBuffer()
26 {
27 this.Data = new byte[BUFFER_SIZE];
28
29 // this will be filled in by the call to udpSocket.BeginReceiveFrom
30 RemoteEndPoint = (EndPoint)new IPEndPoint(IPAddress.Any, 0);
31 }
32
33 public UDPPacketBuffer(byte[] data, EndPoint remoteEndPoint)
34 {
35 this.Data = data;
36 this.DataLength = data.Length;
37 this.RemoteEndPoint = remoteEndPoint;
38 }
39 }
40
41 public abstract class UDPServer
42 {
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,