C# Socket UDP 案例 3 异步

  1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Net.Sockets;
5 using System.Net;
6 using System.Threading;
7 using System.Runtime.InteropServices;
8
9 namespace ConsoleApplication2
10 {
11 class Program
12 {
13 static Socket m_sListen;
14
15 static void Main(string[] args)
16 {
17 new Thread(new ThreadStart(ListenThreadMothed)).Start();
18
19 Console.WriteLine("请按回车键结束!");
20 Console.ReadKey();
21 }
22
23 private static void ListenThreadMothed()
24 {
25 IPAddress ip = IPAddress.Parse("222.222.222.187");
26 IPEndPoint ipe = new IPEndPoint(ip, 60000);
27
28 m_sListen = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
29 m_sListen.Bind(ipe);
30
31 IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
32 EndPoint remote = (EndPoint)sender;
33
34 for (int i = 0; i < 5; i++)
35 {
36 PostRecv(remote);
37 }
38 }
39
40 private static void PostRecv(EndPoint endPoint)
41 {
42 byte[] buffer = new byte[516];
43 m_sListen.BeginReceiveFrom(buffer,
44 0,
45 buffer.Length,
46 SocketFlags.None,
47 ref endPoint,
48 EndRecv,
49 new AsyncState(buffer, 0, buffer.Length, endPoint));
50 }
51
52 private static void PostSend(byte[] buffer, int offset, int size, EndPoint endPoint)
53 {
54 m_sListen.BeginSendTo(buffer,
55 offset,
56 size,
57 SocketFlags.None,
58 endPoint,
59 EndSend,
60 new AsyncState(buffer, offset, size, endPoint));
61 }
62
63 private static void EndRecv(IAsyncResult asyncResult)
64 {
65 AsyncState state = (AsyncState)asyncResult.AsyncState;
66 int readBytes = m_sListen.EndReceiveFrom(asyncResult, ref state.EndPoint);
67
68 PostRecv(state.EndPoint);
69
70 //echo
71 //Console.WriteLine(Encoding.Default.GetString(state.buffer));
72 PostSend(state.Buffer, state.Offset, state.Size, state.EndPoint);
73 }
74
75 private static void EndSend(IAsyncResult asyncResult)
76 {
77 AsyncState state = (AsyncState)asyncResult.AsyncState;
78 byte[] buffer = state.Buffer;
79 int sendBytes = m_sListen.EndSendTo(asyncResult);
80 int remainBytes = state.Size - sendBytes;
81 if (remainBytes <= 0)
82 return;
83 PostSend(buffer, buffer.Length - remainBytes, remainBytes, state.EndPoint);
84 }
85
86 [Serializable, StructLayout(LayoutKind.Sequential)]
87 internal struct AsyncState
88 {
89 private readonly byte[] buffer;
90 private readonly int offset;
91 private readonly int size;
92
93 public AsyncState(byte[] buffer, EndPoint endPoint)
94 {
95 if (endPoint == null)
96 {
97 throw new ArgumentNullException("endPoint");
98 }
99 this.buffer = buffer;
100 this.offset = 0;
101 this.size = buffer.Length;
102 this.EndPoint = endPoint;
103 }
104
105 public AsyncState(byte[] buffer, int offset, int size, EndPoint endPoint)
106 {
107 if (buffer == null)
108 {
109 throw new ArgumentNullException("array");
110 }
111 if (offset < 0)
112 {
113 throw new ArgumentOutOfRangeException("offset", "ArgumentOutOfRange_NeedNonNegNum");
114 }
115 if (size < 0)
116 {
117 throw new ArgumentOutOfRangeException("count", "ArgumentOutOfRange_NeedNonNegNum");
118 }
119 if ((buffer.Length - offset) < size)
120 {
121 throw new ArgumentException("Argument_InvalidOffLen");
122 }
123 if (endPoint == null)
124 {
125 throw new ArgumentNullException("endPoint");
126 }
127 this.buffer = buffer;
128 this.offset = offset;
129 this.size = size;
130 this.EndPoint = endPoint;
131 }
132
133 public byte[] Buffer
134 {
135 get { return buffer; }
136 }
137
138 public int Offset
139 {
140 get { return offset; }
141 }
142
143 public int Size
144 {
145 get { return size; }
146 }
147
148 public EndPoint EndPoint;
149 }
150 }
151 }
posted @ 2012-04-07 13:56  garyChong  阅读(732)  评论(0编辑  收藏  举报