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