程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 高性能異步Socket服務器(UDP)

高性能異步Socket服務器(UDP)

編輯:C#入門知識

采用異步模式設計的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 {
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved