• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#SocketUDP案例3异步

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
  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 }

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#HttpClient类库发布时间:2022-07-18
下一篇:
C#中引用第三方ocx控件引发的问题以及解决办法发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap