[Cleanup] Removed TARGET_JVM
[mono.git] / mcs / class / System / Test / System.Net.Sockets / UdpClientTest.cs
1 // System.Net.Sockets.UdpClientTest.cs
2 //
3 // Authors:
4 //      Chris Bacon <chris.bacon@docobo.co.uk>
5 //      Gert Driesen <drieseng@users.sourceforge.net>
6 //
7
8 using System;
9 using System.Net;
10 using System.Net.Sockets;
11 using System.Threading;
12
13 using NUnit.Framework;
14
15 namespace MonoTests.System.Net.Sockets {
16         [TestFixture]
17         public class UdpClientTest {
18                 [Test] // .ctor ()
19                 public void Constructor1 ()
20                 {
21                         MyUdpClient client;
22                         Socket s;
23
24                         client = new MyUdpClient ();
25                         s = client.Client;
26                         Assert.IsNotNull (s, "Client");
27                         Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "Client:AddressFamily");
28                         Assert.IsFalse (s.Connected, "Client:Connected");
29 #if NET_2_0
30                         Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
31 #endif
32                         Assert.IsNull (s.LocalEndPoint, "Client:LocalEndPoint");
33                         Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "Client:ProtocolType");
34                         Assert.IsNull (s.RemoteEndPoint, "Client:RemoteEndPoint");
35                         Assert.AreEqual (SocketType.Dgram, s.SocketType, "Client:SocketType");
36                         Assert.IsFalse (client.Active, "Active");
37 #if NET_2_0
38                         Assert.IsFalse (client.DontFragment, "DontFragment");
39                         Assert.IsFalse (client.EnableBroadcast, "EnableBroadcast");
40                         //Assert.IsFalse (client.ExclusiveAddressUse, "ExclusiveAddressUse");
41                         Assert.IsTrue (client.MulticastLoopback, "MulticastLoopback");
42                         //Assert.AreEqual (32, client.Ttl, "Ttl");
43 #endif
44                         client.Close ();
45                 }
46
47                 [Test] // .ctor (AddressFamily)
48                 public void Constructor2 ()
49                 {
50                         MyUdpClient client;
51                         Socket s;
52
53                         client = new MyUdpClient (AddressFamily.InterNetwork);
54                         s = client.Client;
55                         Assert.IsNotNull (s, "#A:Client");
56                         Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
57                         Assert.IsFalse (s.Connected, "#A:Client:Connected");
58 #if NET_2_0
59                         Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
60 #endif
61                         Assert.IsNull (s.LocalEndPoint, "#A:Client:LocalEndPoint");
62                         Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
63                         Assert.IsNull (s.RemoteEndPoint, "#A:Client:RemoteEndPoint");
64                         Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
65                         Assert.IsFalse (client.Active, "#A:Active");
66 #if NET_2_0
67                         //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
68                         Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
69                         //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
70                         Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
71                         //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
72 #endif
73
74 #if NET_2_0
75                         if (!Socket.OSSupportsIPv6)
76 #else
77                         if (!Socket.SupportsIPv6)
78 #endif
79                                 Assert.Ignore ("IPv6 not enabled.");
80
81                         client = new MyUdpClient (AddressFamily.InterNetworkV6);
82                         s = client.Client;
83                         Assert.IsNotNull (s, "#B:Client");
84                         Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
85                         Assert.IsFalse (s.Connected, "#B:Client:Connected");
86 #if NET_2_0
87                         Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
88 #endif
89                         Assert.IsNull (s.LocalEndPoint, "#B:Client:LocalEndPoint");
90                         Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
91                         Assert.IsNull (s.RemoteEndPoint, "#B:Client:RemoteEndPoint");
92                         Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
93                         Assert.IsFalse (client.Active, "#B:Active");
94 #if NET_2_0
95                         //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
96                         Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
97                         //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
98                         Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
99                         //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
100 #endif
101                         client.Close ();
102                 }
103
104                 [Test] // .ctor (AddressFamily)
105                 public void Constructor2_Family_Invalid ()
106                 {
107                         try {
108                                 new UdpClient (AddressFamily.NetBios);
109                                 Assert.Fail ("#A1");
110                         } catch (ArgumentException ex) {
111                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
112                                 Assert.IsNull (ex.InnerException, "#A3");
113 #if NET_2_0
114                                 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
115                                 // addresses
116                                 Assert.IsNotNull (ex.Message, "#A4");
117                                 Assert.AreEqual ("family", ex.ParamName, "#A5");
118 #else
119                                 Assert.AreEqual ("family", ex.Message, "#A4");
120                                 Assert.IsNull (ex.ParamName, "#A5");
121 #endif
122                         }
123
124                         try {
125                                 new UdpClient ((AddressFamily) 666);
126                                 Assert.Fail ("#B1");
127                         } catch (ArgumentException ex) {
128                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
129                                 Assert.IsNull (ex.InnerException, "#B3");
130 #if NET_2_0
131                                 Assert.IsNotNull (ex.Message, "#B4");
132                                 Assert.AreEqual ("family", ex.ParamName, "#B5");
133 #else
134                                 Assert.AreEqual ("family", ex.Message, "#B4");
135                                 Assert.IsNull (ex.ParamName, "#B5");
136 #endif
137                         }
138                 }
139
140                 [Test] // .ctor (Int32)
141                 public void Constructor3 ()
142                 {
143                         Socket s;
144                         IPEndPoint localEP;
145
146                         using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort)) 
147                         {
148                                 s = client.Client;
149                                 Assert.IsNotNull (s, "#A:Client");
150                                 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
151                                 Assert.IsFalse (s.Connected, "#A:Client:Connected");
152         #if NET_2_0
153                                 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
154         #endif
155                                 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
156                                 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
157                                 Assert.IsFalse (client.Active, "#A:Active");
158         #if NET_2_0
159                                 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
160                                 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
161                                 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
162                                 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
163                                 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
164         #endif
165                                 localEP = s.LocalEndPoint as IPEndPoint;
166                                 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
167                                 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
168                                 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
169                         }
170                         using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort))
171                         {
172                                 s = client.Client;
173                                 Assert.IsNotNull (s, "#B:Client");
174                                 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
175                                 Assert.IsFalse (s.Connected, "#B:Client:Connected");
176         #if NET_2_0
177                                 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
178         #endif
179                                 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
180                                 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
181                                 Assert.IsFalse (client.Active, "#B:Active");
182         #if NET_2_0
183                                 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
184                                 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
185                                 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
186                                 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
187                                 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
188         #endif
189                                 localEP = s.LocalEndPoint as IPEndPoint;
190                                 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
191                                 Assert.AreEqual (IPAddress.Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
192                                 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
193                                 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
194                         }
195                 }
196
197                 [Test] // .ctor (Int32)
198                 public void Constructor3_Port_OutOfRange ()
199                 {
200                         try {
201                                 new UdpClient (IPEndPoint.MaxPort + 1);
202                                 Assert.Fail ("#A1");
203                         } catch (ArgumentOutOfRangeException ex) {
204                                 // Specified argument was out of the range of valid values
205                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
206                                 Assert.IsNull (ex.InnerException, "#A3");
207                                 Assert.IsNotNull (ex.Message, "#A4");
208                                 Assert.AreEqual ("port", ex.ParamName, "#A5");
209                         }
210
211                         try {
212                                 new UdpClient (IPEndPoint.MinPort - 1);
213                                 Assert.Fail ("#A1");
214                         } catch (ArgumentOutOfRangeException ex) {
215                                 // Specified argument was out of the range of valid values
216                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
217                                 Assert.IsNull (ex.InnerException, "#A3");
218                                 Assert.IsNotNull (ex.Message, "#A4");
219                                 Assert.AreEqual ("port", ex.ParamName, "#A5");
220                         }
221                 }
222
223                 [Test] // .ctor (IPEndPoint)
224                 public void Constructor4 ()
225                 {
226                         Socket s;
227                         IPEndPoint localEP;
228                         IPEndPoint clientEP;
229
230                         clientEP = new IPEndPoint (IPAddress.Loopback, 8001);
231                         using (MyUdpClient client = new MyUdpClient (clientEP))
232                         {
233                                 s = client.Client;
234                                 Assert.IsNotNull (s, "#A:Client");
235                                 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
236                                 Assert.IsFalse (s.Connected, "#A:Client:Connected");
237 #if NET_2_0
238                                 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
239 #endif
240                                 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
241                                 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
242                                 Assert.IsFalse (client.Active, "#A:Active");
243 #if NET_2_0
244                                 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
245                                 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
246                                 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
247                                 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
248                                 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
249 #endif
250                                 localEP = s.LocalEndPoint as IPEndPoint;
251                                 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
252                                 Assert.IsFalse (object.ReferenceEquals (clientEP, localEP), "#A:Client:LocalEndPoint/ReferenceEquality");
253                                 Assert.AreEqual (clientEP.Address, localEP.Address, "#A:Client:LocalEndPoint/Address");
254                                 Assert.AreEqual (clientEP.AddressFamily, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
255                                 Assert.AreEqual (clientEP.Port, localEP.Port, "#A:Client:LocalEndPoint/Port");
256                         }
257                 }
258
259                 [Test] // .ctor (IPEndPoint)
260                 public void Constructor4_LocalEP_Null ()
261                 {
262                         try {
263                                 new UdpClient ((IPEndPoint) null);
264                                 Assert.Fail ("#1");
265                         } catch (ArgumentNullException ex) {
266                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
267                                 Assert.IsNull (ex.InnerException, "#3");
268                                 Assert.IsNotNull (ex.Message, "#4");
269                                 Assert.AreEqual ("localEP", ex.ParamName, "#5");
270                         }
271                 }
272
273                 [Test] // .ctor (Int32, AddressFamily)
274                 public void Constructor5 ()
275                 {
276                         Socket s;
277                         IPEndPoint localEP;
278
279                         using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort, AddressFamily.InterNetwork))
280                         {
281                                 s = client.Client;
282                                 Assert.IsNotNull (s, "#A:Client");
283                                 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
284                                 Assert.IsFalse (s.Connected, "#A:Client:Connected");
285 #if NET_2_0
286                                 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
287 #endif
288                                 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
289                                 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
290                                 Assert.IsFalse (client.Active, "#A:Active");
291 #if NET_2_0
292                                 //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
293                                 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
294                                 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
295                                 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
296                                 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
297 #endif
298                                 localEP = s.LocalEndPoint as IPEndPoint;
299                                 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
300                                 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
301                                 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
302                         }
303
304 #if NET_2_0
305                         if (!Socket.OSSupportsIPv6)
306 #else
307                         if (!Socket.SupportsIPv6)
308 #endif
309                                 Assert.Ignore ("IPv6 not enabled.");
310
311                         using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort, AddressFamily.InterNetworkV6))
312                         {
313                                 s = client.Client;
314                                 Assert.IsNotNull (s, "#B:Client");
315                                 Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
316                                 Assert.IsFalse (s.Connected, "#B:Client:Connected");
317 #if NET_2_0
318                                 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
319 #endif
320                                 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
321                                 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
322                                 Assert.IsFalse (client.Active, "#B:Active");
323 #if NET_2_0
324                                 //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
325                                 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
326                                 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
327                                 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
328                                 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
329 #endif
330                                 localEP = s.LocalEndPoint as IPEndPoint;
331                                 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
332                                 Assert.AreEqual (IPAddress.IPv6Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
333                                 Assert.AreEqual (AddressFamily.InterNetworkV6, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
334                                 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
335                         }
336                 }
337
338                 [Test] // .ctor (Int32, AddressFamily)
339                 public void Constructor5_Family_Invalid ()
340                 {
341                         try {
342                                 new UdpClient (80, AddressFamily.NetBios);
343                                 Assert.Fail ("#A1");
344                         } catch (ArgumentException ex) {
345                                 // family
346                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
347                                 Assert.IsNull (ex.InnerException, "#A3");
348 #if NET_2_0
349                                 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
350                                 // addresses
351                                 Assert.IsNotNull (ex.Message, "#A4");
352                                 Assert.AreEqual ("family", ex.ParamName, "#A5");
353 #else
354                                 Assert.AreEqual ("family", ex.Message, "#A4");
355                                 Assert.IsNull (ex.ParamName, "#A5");
356 #endif
357                         }
358
359                         try {
360                                 new UdpClient (80, (AddressFamily) 666);
361                                 Assert.Fail ("#B1");
362                         } catch (ArgumentException ex) {
363                                 // family
364                                 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
365                                 Assert.IsNull (ex.InnerException, "#B3");
366 #if NET_2_0
367                                 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
368                                 // addresses
369                                 Assert.IsNotNull (ex.Message, "#B4");
370                                 Assert.AreEqual ("family", ex.ParamName, "#B5");
371 #else
372                                 Assert.AreEqual ("family", ex.Message, "#B4");
373                                 Assert.IsNull (ex.ParamName, "#B5");
374 #endif
375                         }
376                 }
377
378                 [Test] // .ctor (Int32, AddressFamily)
379                 public void Constructor5_Port_OutOfRange ()
380                 {
381                         try {
382                                 new UdpClient (IPEndPoint.MaxPort + 1, AddressFamily.InterNetwork);
383                                 Assert.Fail ("#A1");
384                         } catch (ArgumentOutOfRangeException ex) {
385                                 // Specified argument was out of the range of valid values
386                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
387                                 Assert.IsNull (ex.InnerException, "#A3");
388                                 Assert.IsNotNull (ex.Message, "#A4");
389                                 Assert.AreEqual ("port", ex.ParamName, "#A5");
390                         }
391
392                         try {
393                                 new UdpClient (IPEndPoint.MinPort - 1, AddressFamily.InterNetwork);
394                                 Assert.Fail ("#A1");
395                         } catch (ArgumentOutOfRangeException ex) {
396                                 // Specified argument was out of the range of valid values
397                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
398                                 Assert.IsNull (ex.InnerException, "#A3");
399                                 Assert.IsNotNull (ex.Message, "#A4");
400                                 Assert.AreEqual ("port", ex.ParamName, "#A5");
401                         }
402                 }
403
404                 [Test] // .ctor (String, Int32)
405                 public void Constructor6 ()
406                 {
407                         Socket s;
408                         IPEndPoint localEP;
409
410                         // Bug #5503
411                         // UDP port 0 doesn't seem to be valid.
412                         using (MyUdpClient client = new MyUdpClient ("127.0.0.1", 53))
413                         {
414                                 s = client.Client;
415                                 Assert.IsNotNull (s, "#A:Client");
416                                 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
417                                 Assert.IsTrue (s.Connected, "#A:Client:Connected");
418 #if NET_2_0
419                                 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
420 #endif
421                                 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
422                                 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
423                                 Assert.IsTrue (client.Active, "#A:Active");
424 #if NET_2_0
425                                 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
426                                 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
427                                 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
428                                 //Assert.IsFalse (client.MulticastLoopback, "#A:MulticastLoopback");
429                                 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
430 #endif
431                                 localEP = s.LocalEndPoint as IPEndPoint;
432                                 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
433                                 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#A:Client:LocalEndPoint/Address");
434                                 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
435                         }
436                         using (MyUdpClient client = new MyUdpClient ("127.0.0.1", IPEndPoint.MaxPort))
437                         {
438                                 s = client.Client;
439                                 Assert.IsNotNull (s, "#B:Client");
440                                 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
441                                 Assert.IsTrue (s.Connected, "#B:Client:Connected");
442 #if NET_2_0
443                                 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
444 #endif
445                                 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
446                                 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
447                                 Assert.IsTrue (client.Active, "#B:Active");
448 #if NET_2_0
449                                 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
450                                 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
451                                 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
452                                 //Assert.IsFalse (client.MulticastLoopback, "#B:MulticastLoopback");
453                                 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
454 #endif
455                                 localEP = s.LocalEndPoint as IPEndPoint;
456                                 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
457                                 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#B:Client:LocalEndPoint/Address");
458                                 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
459                         }
460                 }
461
462                 [Test] // .ctor (String, Int32)
463                 public void Constructor6_HostName_Null ()
464                 {
465                         try {
466                                 new UdpClient ((string) null, int.MaxValue);
467                                 Assert.Fail ("#1");
468                         } catch (ArgumentNullException ex) {
469                                 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
470                                 Assert.IsNull (ex.InnerException, "#3");
471                                 Assert.IsNotNull (ex.Message, "#4");
472                                 Assert.AreEqual ("hostname", ex.ParamName, "#5");
473                         }
474                 }
475
476                 [Test] // .ctor (String, Int32)
477                 public void Constructor6_Port_OutOfRange ()
478                 {
479                         try {
480                                 new UdpClient ("local", IPEndPoint.MaxPort + 1);
481                                 Assert.Fail ("#A1");
482                         } catch (ArgumentOutOfRangeException ex) {
483                                 // Specified argument was out of the range of valid values
484                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
485                                 Assert.IsNull (ex.InnerException, "#A3");
486                                 Assert.IsNotNull (ex.Message, "#A4");
487                                 Assert.AreEqual ("port", ex.ParamName, "#A5");
488                         }
489
490                         try {
491                                 new UdpClient ("local", IPEndPoint.MinPort - 1);
492                                 Assert.Fail ("#A1");
493                         } catch (ArgumentOutOfRangeException ex) {
494                                 // Specified argument was out of the range of valid values
495                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
496                                 Assert.IsNull (ex.InnerException, "#A3");
497                                 Assert.IsNotNull (ex.Message, "#A4");
498                                 Assert.AreEqual ("port", ex.ParamName, "#A5");
499                         }
500                 }
501
502                 [Test]
503                 public void UdpClientBroadcastTest () 
504                 {
505                         UdpClient client = new UdpClient ();
506                         byte[] bytes = new byte[] {10, 11, 12, 13};
507
508                         try {
509                                 client.Send (bytes, bytes.Length, new IPEndPoint (IPAddress.Broadcast, 1235));
510                         } finally {
511                                 client.Close ();
512                         }
513                 }
514
515                 [Test] // JoinMulticastGroup (IPAddress)
516                 public void JoinMulticastGroup1_IPv4 ()
517                 {
518                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
519
520                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
521                                 client.JoinMulticastGroup (mcast_addr);
522                         }
523                 }
524
525                 [Test] // JoinMulticastGroup (IPAddress)
526                 public void JoinMulticastGroup1_IPv6 ()
527                 {
528 #if NET_2_0
529                         if (!Socket.OSSupportsIPv6)
530 #else
531                         if (!Socket.SupportsIPv6)
532 #endif
533                                 Assert.Ignore ("IPv6 not enabled.");
534
535                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
536
537                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
538                                 client.JoinMulticastGroup (mcast_addr);
539                         }
540                 }
541
542                 [Test] // JoinMulticastGroup (IPAddress)
543                 public void JoinMulticastGroup1_MulticastAddr_Null ()
544                 {
545                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
546                                 try {
547                                         client.JoinMulticastGroup ((IPAddress) null);
548                                         Assert.Fail ("#1");
549 #if NET_2_0
550                                 } catch (ArgumentNullException ex) {
551                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
552                                         Assert.IsNull (ex.InnerException, "#3");
553                                         Assert.IsNotNull (ex.Message, "#4");
554                                         Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
555                                 }
556 #else
557                                 } catch (NullReferenceException ex) {
558                                         Assert.AreEqual (typeof (NullReferenceException), ex.GetType (), "#2");
559                                         Assert.IsNull (ex.InnerException, "#3");
560                                         Assert.IsNotNull (ex.Message, "#4");
561                                 }
562 #endif
563                         }
564                 }
565
566                 [Test] // JoinMulticastGroup (IPAddress)
567                 public void JoinMulticastGroup1_Socket_Closed ()
568                 {
569                         IPAddress mcast_addr = null;
570
571                         UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
572                         client.Close ();
573                         try {
574                                 client.JoinMulticastGroup (mcast_addr);
575                                 Assert.Fail ("#1");
576                         } catch (ObjectDisposedException ex) {
577                                 // Cannot access a disposed object
578                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
579                                 Assert.IsNull (ex.InnerException, "#3");
580                                 Assert.IsNotNull (ex.Message, "#4");
581                                 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
582                         }
583                 }
584
585                 [Test] // JoinMulticastGroup (IPAddress)
586                 [Category ("NotWorking")]
587                 public void JoinMulticastGroup1_Socket_NotBound ()
588                 {
589                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
590
591                         UdpClient client = new UdpClient (AddressFamily.InterNetwork);
592                         try {
593                                 client.JoinMulticastGroup (mcast_addr);
594                                 Assert.Fail ("#1");
595                         } catch (SocketException ex) {
596                                 // An invalid argument was supplied
597                                 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
598                                 Assert.AreEqual (10022, ex.ErrorCode, "#3");
599                                 Assert.IsNull (ex.InnerException, "#4");
600                                 Assert.IsNotNull (ex.Message, "#5");
601                                 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
602 #if NET_2_0
603                                 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
604 #endif
605                         } finally {
606                                 client.Close ();
607                         }
608                 }
609
610                 [Test] // JoinMulticastGroup (In32, IPAddress)
611                 public void JoinMulticastGroup2_IPv4 ()
612                 {
613                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
614
615                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
616                                 try {
617                                         client.JoinMulticastGroup (0, mcast_addr);
618                                         Assert.Fail ("#1");
619                                 } catch (SocketException ex) {
620                                         // The attempted operation is not supported for the type of
621                                         // object referenced
622                                         Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
623                                         Assert.AreEqual (10045, ex.ErrorCode, "#3");
624                                         Assert.IsNull (ex.InnerException, "#4");
625                                         Assert.IsNotNull (ex.Message, "#5");
626                                         Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
627 #if NET_2_0
628                                         Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
629 #endif
630                                 }
631                         }
632                 }
633
634                 [Test] // JoinMulticastGroup (In32, IPAddress)
635                 public void JoinMulticastGroup2_IPv6 ()
636                 {
637 #if NET_2_0
638                         if (!Socket.OSSupportsIPv6)
639 #else
640                         if (!Socket.SupportsIPv6)
641 #endif
642                                 Assert.Ignore ("IPv6 not enabled.");
643
644                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
645
646                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
647                                 client.JoinMulticastGroup (0, mcast_addr);
648                         }
649                 }
650
651                 [Test] // JoinMulticastGroup (Int32, IPAddress)
652                 public void JoinMulticastGroup2_MulticastAddr_Null ()
653                 {
654                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
655                                 try {
656                                         client.JoinMulticastGroup (0, (IPAddress) null);
657                                         Assert.Fail ("#1");
658                                 } catch (ArgumentNullException ex) {
659                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
660                                         Assert.IsNull (ex.InnerException, "#3");
661                                         Assert.IsNotNull (ex.Message, "#4");
662                                         Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
663                                 }
664                         }
665                 }
666
667                 [Test] // JoinMulticastGroup (Int32, IPAddress)
668                 public void JoinMulticastGroup2_Socket_Closed ()
669                 {
670 #if NET_2_0
671                         if (!Socket.OSSupportsIPv6)
672 #else
673                         if (!Socket.SupportsIPv6)
674 #endif
675                                 Assert.Ignore ("IPv6 not enabled.");
676
677                         IPAddress mcast_addr = null;
678
679                         UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234));
680                         client.Close ();
681                         try {
682                                 client.JoinMulticastGroup (0, mcast_addr);
683                                 Assert.Fail ("#1");
684                         } catch (ObjectDisposedException ex) {
685                                 // Cannot access a disposed object
686                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
687                                 Assert.IsNull (ex.InnerException, "#3");
688                                 Assert.IsNotNull (ex.Message, "#4");
689                                 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
690                         }
691                 }
692
693                 [Test] // JoinMulticastGroup (Int32, IPAddress)
694                 [Category ("NotWorking")]
695                 public void JoinMulticastGroup2_Socket_NotBound ()
696                 {
697                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
698
699                         UdpClient client = new UdpClient (AddressFamily.InterNetworkV6);
700                         try {
701                                 client.JoinMulticastGroup (0, mcast_addr);
702                                 Assert.Fail ("#1");
703                         } catch (SocketException ex) {
704                                 // An invalid argument was supplied
705                                 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
706                                 Assert.AreEqual (10022, ex.ErrorCode, "#3");
707                                 Assert.IsNull (ex.InnerException, "#4");
708                                 Assert.IsNotNull (ex.Message, "#5");
709                                 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
710 #if NET_2_0
711                                 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
712 #endif
713                         } finally {
714                                 client.Close ();
715                         }
716                 }
717
718                 [Test] // JoinMulticastGroup (IPAddress, Int32)
719                 public void JoinMulticastGroup3_IPv4 ()
720                 {
721                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
722
723                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
724                                 client.JoinMulticastGroup (mcast_addr, 0);
725                         }
726
727                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
728                                 client.JoinMulticastGroup (mcast_addr, 255);
729                         }
730                 }
731
732                 [Test] // JoinMulticastGroup (IPAddress, Int32)
733                 public void JoinMulticastGroup3_IPv6 ()
734                 {
735 #if NET_2_0
736                         if (!Socket.OSSupportsIPv6)
737 #else
738                         if (!Socket.SupportsIPv6)
739 #endif
740                                 Assert.Ignore ("IPv6 not enabled.");
741
742                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
743
744                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
745                                 client.JoinMulticastGroup (mcast_addr, 0);
746                         }
747
748                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
749                                 client.JoinMulticastGroup (mcast_addr, 255);
750                         }
751                 }
752
753                 [Test] // JoinMulticastGroup (IPAddress, Int32)
754                 public void JoinMulticastGroup3_MulticastAddr_Null ()
755                 {
756                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
757                                 try {
758                                         client.JoinMulticastGroup ((IPAddress) null, int.MaxValue);
759                                         Assert.Fail ("#1");
760                                 } catch (ArgumentNullException ex) {
761                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
762                                         Assert.IsNull (ex.InnerException, "#3");
763                                         Assert.IsNotNull (ex.Message, "#4");
764                                         Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
765                                 }
766                         }
767                 }
768
769                 [Test] // JoinMulticastGroup (IPAddress, Int32)
770                 public void JoinMulticastGroup3_Socket_Closed ()
771                 {
772                         IPAddress mcast_addr = null;
773
774                         UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
775                         client.Close ();
776                         try {
777                                 client.JoinMulticastGroup (mcast_addr, 0);
778                                 Assert.Fail ("#1");
779                         } catch (ObjectDisposedException ex) {
780                                 // Cannot access a disposed object
781                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
782                                 Assert.IsNull (ex.InnerException, "#3");
783                                 Assert.IsNotNull (ex.Message, "#4");
784                                 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
785                         }
786                 }
787
788                 [Test] // JoinMulticastGroup (IPAddress, Int32)
789                 [Category ("NotWorking")]
790                 public void JoinMulticastGroup3_Socket_NotBound ()
791                 {
792                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
793
794                         UdpClient client = new UdpClient (AddressFamily.InterNetwork);
795                         try {
796                                 client.JoinMulticastGroup (mcast_addr, 5);
797                                 Assert.Fail ("#1");
798                         } catch (SocketException ex) {
799                                 // An invalid argument was supplied
800                                 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
801                                 Assert.AreEqual (10022, ex.ErrorCode, "#3");
802                                 Assert.IsNull (ex.InnerException, "#4");
803                                 Assert.IsNotNull (ex.Message, "#5");
804                                 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
805 #if NET_2_0
806                                 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
807 #endif
808                         } finally {
809                                 client.Close ();
810                         }
811                 }
812
813 #if NET_2_0
814                 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
815                 public void JoinMulticastGroup4_IPv4 ()
816                 {
817                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
818                         IPAddress local_addr = IPAddress.Any;
819
820                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
821                                 client.JoinMulticastGroup (mcast_addr, local_addr);
822                         }
823                 }
824
825                 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
826                 public void JoinMulticastGroup4_IPv6 ()
827                 {
828                         if (!Socket.OSSupportsIPv6)
829                                 Assert.Ignore ("IPv6 not enabled.");
830
831                         IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
832                         IPAddress local_addr = IPAddress.IPv6Any;
833
834                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
835                                 try {
836                                         client.JoinMulticastGroup (mcast_addr, local_addr);
837                                         Assert.Fail ("#1");
838                                 } catch (SocketException ex) {
839                                         // The attempted operation is not supported for the type of
840                                         // object referenced
841                                         Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
842                                         Assert.AreEqual (10045, ex.ErrorCode, "#3");
843                                         Assert.IsNull (ex.InnerException, "#4");
844                                         Assert.IsNotNull (ex.Message, "#5");
845                                         Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
846                                         Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
847                                 }
848                         }
849                 }
850
851                 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
852                 public void JoinMulticastGroup4_LocalAddress_Null ()
853                 {
854                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
855
856                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
857                                 try {
858                                         client.JoinMulticastGroup (mcast_addr, (IPAddress) null);
859                                         Assert.Fail ("#1");
860                                 } catch (ArgumentNullException ex) {
861                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
862                                         Assert.IsNull (ex.InnerException, "#3");
863                                         Assert.IsNotNull (ex.Message, "#4");
864                                         Assert.AreEqual ("mcint", ex.ParamName, "#5");
865                                 }
866                         }
867                 }
868
869                 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
870                 public void JoinMulticastGroup4_MulticastAddr_Null ()
871                 {
872                         using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
873                                 try {
874                                         client.JoinMulticastGroup ((IPAddress) null, IPAddress.Loopback);
875                                         Assert.Fail ("#1");
876                                 } catch (ArgumentNullException ex) {
877                                         Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
878                                         Assert.IsNull (ex.InnerException, "#3");
879                                         Assert.IsNotNull (ex.Message, "#4");
880                                         Assert.AreEqual ("group", ex.ParamName, "#5");
881                                 }
882                         }
883                 }
884
885                 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
886                 public void JoinMulticastGroup4_Socket_Closed ()
887                 {
888                         IPAddress mcast_addr = null;
889                         IPAddress local_addr = null;
890
891                         UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
892                         client.Close ();
893                         try {
894                                 client.JoinMulticastGroup (mcast_addr, local_addr);
895                                 Assert.Fail ("#1");
896                         } catch (ObjectDisposedException ex) {
897                                 // Cannot access a disposed object
898                                 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
899                                 Assert.IsNull (ex.InnerException, "#3");
900                                 Assert.IsNotNull (ex.Message, "#4");
901                                 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
902                         }
903                 }
904
905                 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
906                 [Category ("NotWorking")]
907                 public void JoinMulticastGroup4_Socket_NotBound ()
908                 {
909                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
910                         IPAddress local_addr = Dns.GetHostEntry (string.Empty).AddressList [0];
911
912                         UdpClient client = new UdpClient (AddressFamily.InterNetwork);
913                         try {
914                                 client.JoinMulticastGroup (mcast_addr, local_addr);
915                                 Assert.Fail ("#1");
916                         } catch (SocketException ex) {
917                                 // An invalid argument was supplied
918                                 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
919                                 Assert.AreEqual (10022, ex.ErrorCode, "#3");
920                                 Assert.IsNull (ex.InnerException, "#4");
921                                 Assert.IsNotNull (ex.Message, "#5");
922                                 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
923                                 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
924                         } finally {
925                                 client.Close ();
926                         }
927                 }
928
929                 [Test]
930                 public void CloseInReceive ()
931                 {
932                         UdpClient client = null;
933                         var rnd = new Random ();
934                         for (int i = 0; i < 5; i++) {
935                                 int port = rnd.Next (1025, 65534);
936                                 try {
937                                         client = new UdpClient (port);
938                                         break;
939                                 } catch (Exception ex) {
940                                         if (i == 5)
941                                                 throw;
942                                 }
943                         }
944
945                         new Thread(delegate() {
946                                 Thread.Sleep(2000);
947                                 client.Close();
948                                 }).Start();
949
950                         bool got_exc = false;
951                         IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
952                         try {
953                                 client.Receive(ref ep);
954                         } catch (SocketException) {
955                                 got_exc = true;
956                         } finally {
957                                 client.Close ();
958                         }
959                         Assert.IsTrue (got_exc);
960                 }
961
962                 // Test for bug 324033
963                 [Test]
964                 public void JoinMulticastGroupWithLocal ()
965                 {
966                         UdpClient client = new UdpClient (9001);
967                         IPAddress mcast_addr = IPAddress.Parse ("224.0.0.24");
968                         IPAddress local_addr = IPAddress.Any;
969
970                         try {
971                                 client.JoinMulticastGroup (mcast_addr, local_addr);
972                         } finally {
973                                 client.Close ();
974                         }
975                 }
976                 
977                 [Test]
978                 [ExpectedException (typeof(ArgumentNullException))]
979                 public void BeginSendNull ()
980                 {
981                         UdpClient client = new UdpClient ();
982                         client.BeginSend (null, 0, null, null);
983                         client.Close ();
984                 }
985                 
986                 static bool BSSent = false;
987                 static int BSBytes;
988                 static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
989                 
990                 private static void BSCallback (IAsyncResult asyncResult)
991                 {
992                         UdpClient client = (UdpClient)asyncResult.AsyncState;
993                         
994                         BSBytes = client.EndSend (asyncResult);
995                         
996                         BSSent = true;
997                         BSCalledBack.Set ();
998                 }
999                 
1000                 [Test]
1001                 public void BeginSend ()
1002                 {
1003                         UdpClient client = new UdpClient ();
1004                         byte[] bytes = new byte[] {10, 11, 12, 13};
1005
1006                         try {
1007                                 client.BeginSend (bytes, bytes.Length, new AsyncCallback (BSCallback), client);
1008                                 Assert.Fail ("BeginSend #1");
1009                         } catch (SocketException ex) {
1010                                 Assert.AreEqual (10057, ex.ErrorCode,
1011                                                  "BeginSend #2");
1012                         }
1013                         
1014                         try {
1015                                 client.BeginSend (bytes, bytes.Length, null, new AsyncCallback (BSCallback), client);
1016                                 Assert.Fail ("BeginSend #3");
1017                         } catch (SocketException ex) {
1018                                 Assert.AreEqual (10057, ex.ErrorCode,
1019                                                  "BeginSend #4");
1020                         }
1021
1022                         IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1236);
1023                         
1024                         BSCalledBack.Reset ();
1025                         
1026                         client.BeginSend (bytes, bytes.Length, ep,
1027                                           new AsyncCallback (BSCallback),
1028                                           client);
1029                         if (BSCalledBack.WaitOne (2000, false) == false) {
1030                                 Assert.Fail ("BeginSend wait timed out");
1031                         }
1032                         
1033                         Assert.AreEqual (true, BSSent, "BeginSend #5");
1034                         Assert.AreEqual (4, BSBytes, "BeginSend #6");
1035
1036                         client.Close ();
1037                 }
1038                 
1039                 static bool BRReceived = false;
1040                 static byte[] BRBytes;
1041                 static IPEndPoint BRFrom;
1042                 static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
1043                 
1044                 private static void BRCallback (IAsyncResult asyncResult)
1045                 {
1046                         UdpClient client = (UdpClient)asyncResult.AsyncState;
1047                         
1048                         BRBytes = client.EndReceive (asyncResult, ref BRFrom);
1049                         
1050                         BRReceived = true;
1051                         BRCalledBack.Set ();
1052                 }
1053                 
1054                 [Test]
1055                 public void BeginReceive ()
1056                 {
1057                         UdpClient client = new UdpClient (1237);
1058                         
1059                         BRCalledBack.Reset ();
1060                         
1061                         client.BeginReceive (BRCallback, client);
1062
1063                         IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1237);
1064                         byte[] send_bytes = new byte[] {10, 11, 12, 13};
1065                         client.Send (send_bytes, send_bytes.Length, ep);
1066
1067                         if (BRCalledBack.WaitOne (2000, false) == false) {
1068                                 Assert.Fail ("BeginReceive wait timed out");
1069                         }
1070                         
1071                         Assert.AreEqual (true, BRReceived, "BeginReceive #1");
1072                         Assert.AreEqual (4, BRBytes.Length, "BeginReceive #2");
1073                         Assert.AreEqual (ep. Port, BRFrom.Port, "BeginReceive #3");
1074                         Assert.AreEqual (ep.Address, BRFrom.Address, "BeginReceive #4");
1075
1076                         client.Close ();
1077                 }
1078                 
1079                 [Test]
1080                 public void Available ()
1081                 {
1082                         using (UdpClient client = new UdpClient (1238)) {
1083                                 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1238);
1084                                 byte[] bytes = new byte[] {10, 11, 12, 13};
1085                                 
1086                                 int res = client.Send (bytes, bytes.Length, ep);
1087                                 Assert.AreEqual (bytes.Length, res, "Send");
1088
1089                                 // that might happen too quickly, data sent and not yet received/available
1090                                 Thread.Sleep (100);
1091                                 int avail = client.Available;
1092                                 
1093                                 Assert.AreEqual (bytes.Length, avail, "Available #1");
1094
1095                                 client.Close ();
1096                         }
1097                 }
1098                 
1099                 [Test]
1100                 [Category ("NotWorking")]  // Using PMTU settings workaround on Linux, default true
1101                 public void DontFragmentDefault ()
1102                 {
1103                         UdpClient client = new UdpClient ();
1104                         
1105                         /* Ignore the docs, testing shows the default
1106                          * here is in fact false
1107                          */
1108                         Assert.AreEqual (false, client.DontFragment, "DontFragmentDefault");
1109
1110                         client.Close ();
1111                 }
1112                 
1113                 [Test]
1114                 public void EnableBroadcastDefault ()
1115                 {
1116                         UdpClient client = new UdpClient ();
1117                         
1118                         Assert.AreEqual (false, client.EnableBroadcast, "EnableBroadcastDefault");
1119
1120                         client.Close ();
1121                 }
1122                 
1123                 /* Can't test the default for ExclusiveAddressUse as
1124                  * it's different on different versions and service
1125                  * packs of windows
1126                  */
1127                 [Test]
1128                 [Category ("NotWorking")] // Not supported on Linux
1129                 public void ExclusiveAddressUseUnbound ()
1130                 {
1131                         UdpClient client = new UdpClient ();
1132
1133                         client.ExclusiveAddressUse = true;
1134
1135                         Assert.AreEqual (true, client.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
1136
1137                         client.Close ();
1138                 }
1139                 
1140                 [Test]
1141                 [ExpectedException (typeof(InvalidOperationException))]
1142                 [Category ("NotWorking")] // Not supported on Linux
1143                 public void ExclusiveAddressUseBound ()
1144                 {
1145                         UdpClient client = new UdpClient (1239);
1146
1147                         client.ExclusiveAddressUse = true;
1148
1149                         client.Close ();
1150                 }
1151                 
1152                 [Test]
1153                 public void MulticastLoopbackDefault ()
1154                 {
1155                         UdpClient client = new UdpClient ();
1156                         
1157                         Assert.AreEqual (true, client.MulticastLoopback, "MulticastLoopbackDefault");
1158
1159                         client.Close ();
1160                 }
1161                 
1162                 /* No test for Ttl default as it is platform dependent */
1163 #endif
1164
1165                 class MyUdpClient : UdpClient
1166                 {
1167                         public MyUdpClient ()
1168                         {
1169                         }
1170
1171                         public MyUdpClient (AddressFamily family)
1172                                 : base (family)
1173                         {
1174                         }
1175
1176                         public MyUdpClient (Int32 port)
1177                                 : base (port)
1178                         {
1179                         }
1180
1181
1182                         public MyUdpClient (IPEndPoint localEP)
1183                                 : base (localEP)
1184                         {
1185                         }
1186
1187                         public MyUdpClient (int port, AddressFamily family)
1188                                 : base (port, family)
1189                         {
1190                         }
1191
1192                         public MyUdpClient (string hostname, int port)
1193                                 : base (hostname, port)
1194                         {
1195                         }
1196
1197                         public new bool Active {
1198                                 get { return base.Active; }
1199                                 set { base.Active = value; }
1200                         }
1201
1202 #if ONLY_1_1
1203                         public new Socket Client {
1204                                 get { return base.Client; }
1205                                 set { base.Client = value; }
1206                         }
1207 #endif
1208                 }
1209         }
1210 }