1 // System.Net.Sockets.UdpClientTest.cs
4 // Chris Bacon <chris.bacon@docobo.co.uk>
5 // Gert Driesen <drieseng@users.sourceforge.net>
10 using System.Net.Sockets;
11 using System.Threading;
13 using NUnit.Framework;
15 namespace MonoTests.System.Net.Sockets {
17 public class UdpClientTest {
19 public void Constructor1 ()
24 client = new MyUdpClient ();
26 Assert.IsNotNull (s, "Client");
27 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "Client:AddressFamily");
28 Assert.IsFalse (s.Connected, "Client:Connected");
29 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
30 Assert.IsNull (s.LocalEndPoint, "Client:LocalEndPoint");
31 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "Client:ProtocolType");
32 Assert.IsNull (s.RemoteEndPoint, "Client:RemoteEndPoint");
33 Assert.AreEqual (SocketType.Dgram, s.SocketType, "Client:SocketType");
34 Assert.IsFalse (client.Active, "Active");
35 Assert.IsFalse (client.DontFragment, "DontFragment");
36 Assert.IsFalse (client.EnableBroadcast, "EnableBroadcast");
37 //Assert.IsFalse (client.ExclusiveAddressUse, "ExclusiveAddressUse");
38 Assert.IsTrue (client.MulticastLoopback, "MulticastLoopback");
39 //Assert.AreEqual (32, client.Ttl, "Ttl");
43 [Test] // .ctor (AddressFamily)
44 public void Constructor2 ()
49 client = new MyUdpClient (AddressFamily.InterNetwork);
51 Assert.IsNotNull (s, "#A:Client");
52 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
53 Assert.IsFalse (s.Connected, "#A:Client:Connected");
54 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
55 Assert.IsNull (s.LocalEndPoint, "#A:Client:LocalEndPoint");
56 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
57 Assert.IsNull (s.RemoteEndPoint, "#A:Client:RemoteEndPoint");
58 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
59 Assert.IsFalse (client.Active, "#A:Active");
60 //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
61 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
62 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
63 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
64 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
66 if (!Socket.OSSupportsIPv6)
67 Assert.Ignore ("IPv6 not enabled.");
69 client = new MyUdpClient (AddressFamily.InterNetworkV6);
71 Assert.IsNotNull (s, "#B:Client");
72 Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
73 Assert.IsFalse (s.Connected, "#B:Client:Connected");
74 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
75 Assert.IsNull (s.LocalEndPoint, "#B:Client:LocalEndPoint");
76 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
77 Assert.IsNull (s.RemoteEndPoint, "#B:Client:RemoteEndPoint");
78 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
79 Assert.IsFalse (client.Active, "#B:Active");
80 //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
81 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
82 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
83 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
84 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
88 [Test] // .ctor (AddressFamily)
89 public void Constructor2_Family_Invalid ()
92 new UdpClient (AddressFamily.NetBios);
94 } catch (ArgumentException ex) {
95 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
96 Assert.IsNull (ex.InnerException, "#A3");
97 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
99 Assert.IsNotNull (ex.Message, "#A4");
100 Assert.AreEqual ("family", ex.ParamName, "#A5");
104 new UdpClient ((AddressFamily) 666);
106 } catch (ArgumentException ex) {
107 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
108 Assert.IsNull (ex.InnerException, "#B3");
109 Assert.IsNotNull (ex.Message, "#B4");
110 Assert.AreEqual ("family", ex.ParamName, "#B5");
114 [Test] // .ctor (Int32)
115 public void Constructor3 ()
120 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort))
123 Assert.IsNotNull (s, "#A:Client");
124 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
125 Assert.IsFalse (s.Connected, "#A:Client:Connected");
126 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
127 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
128 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
129 Assert.IsFalse (client.Active, "#A:Active");
130 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
131 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
132 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
133 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
134 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
135 localEP = s.LocalEndPoint as IPEndPoint;
136 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
137 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
138 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
140 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort))
143 Assert.IsNotNull (s, "#B:Client");
144 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
145 Assert.IsFalse (s.Connected, "#B:Client:Connected");
146 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
147 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
148 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
149 Assert.IsFalse (client.Active, "#B:Active");
150 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
151 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
152 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
153 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
154 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
155 localEP = s.LocalEndPoint as IPEndPoint;
156 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
157 Assert.AreEqual (IPAddress.Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
158 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
159 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
163 [Test] // .ctor (Int32)
164 public void Constructor3_Port_OutOfRange ()
167 new UdpClient (IPEndPoint.MaxPort + 1);
169 } catch (ArgumentOutOfRangeException ex) {
170 // Specified argument was out of the range of valid values
171 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
172 Assert.IsNull (ex.InnerException, "#A3");
173 Assert.IsNotNull (ex.Message, "#A4");
174 Assert.AreEqual ("port", ex.ParamName, "#A5");
178 new UdpClient (IPEndPoint.MinPort - 1);
180 } catch (ArgumentOutOfRangeException ex) {
181 // Specified argument was out of the range of valid values
182 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
183 Assert.IsNull (ex.InnerException, "#A3");
184 Assert.IsNotNull (ex.Message, "#A4");
185 Assert.AreEqual ("port", ex.ParamName, "#A5");
189 [Test] // .ctor (IPEndPoint)
190 public void Constructor4 ()
196 clientEP = new IPEndPoint (IPAddress.Loopback, 8001);
197 using (MyUdpClient client = new MyUdpClient (clientEP))
200 Assert.IsNotNull (s, "#A:Client");
201 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
202 Assert.IsFalse (s.Connected, "#A:Client:Connected");
203 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
204 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
205 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
206 Assert.IsFalse (client.Active, "#A:Active");
207 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
208 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
209 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
210 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
211 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
212 localEP = s.LocalEndPoint as IPEndPoint;
213 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
214 Assert.IsFalse (object.ReferenceEquals (clientEP, localEP), "#A:Client:LocalEndPoint/ReferenceEquality");
215 Assert.AreEqual (clientEP.Address, localEP.Address, "#A:Client:LocalEndPoint/Address");
216 Assert.AreEqual (clientEP.AddressFamily, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
217 Assert.AreEqual (clientEP.Port, localEP.Port, "#A:Client:LocalEndPoint/Port");
221 [Test] // .ctor (IPEndPoint)
222 public void Constructor4_LocalEP_Null ()
225 new UdpClient ((IPEndPoint) null);
227 } catch (ArgumentNullException ex) {
228 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
229 Assert.IsNull (ex.InnerException, "#3");
230 Assert.IsNotNull (ex.Message, "#4");
231 Assert.AreEqual ("localEP", ex.ParamName, "#5");
235 [Test] // .ctor (Int32, AddressFamily)
236 public void Constructor5 ()
241 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort, AddressFamily.InterNetwork))
244 Assert.IsNotNull (s, "#A:Client");
245 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
246 Assert.IsFalse (s.Connected, "#A:Client:Connected");
247 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
248 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
249 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
250 Assert.IsFalse (client.Active, "#A:Active");
251 //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
252 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
253 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
254 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
255 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
256 localEP = s.LocalEndPoint as IPEndPoint;
257 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
258 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
259 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
262 if (!Socket.OSSupportsIPv6)
263 Assert.Ignore ("IPv6 not enabled.");
265 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort, AddressFamily.InterNetworkV6))
268 Assert.IsNotNull (s, "#B:Client");
269 Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
270 Assert.IsFalse (s.Connected, "#B:Client:Connected");
271 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
272 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
273 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
274 Assert.IsFalse (client.Active, "#B:Active");
275 //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
276 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
277 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
278 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
279 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
280 localEP = s.LocalEndPoint as IPEndPoint;
281 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
282 Assert.AreEqual (IPAddress.IPv6Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
283 Assert.AreEqual (AddressFamily.InterNetworkV6, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
284 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
288 [Test] // .ctor (Int32, AddressFamily)
289 public void Constructor5_Family_Invalid ()
292 new UdpClient (80, AddressFamily.NetBios);
294 } catch (ArgumentException ex) {
296 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
297 Assert.IsNull (ex.InnerException, "#A3");
298 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
300 Assert.IsNotNull (ex.Message, "#A4");
301 Assert.AreEqual ("family", ex.ParamName, "#A5");
305 new UdpClient (80, (AddressFamily) 666);
307 } catch (ArgumentException ex) {
309 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
310 Assert.IsNull (ex.InnerException, "#B3");
311 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
313 Assert.IsNotNull (ex.Message, "#B4");
314 Assert.AreEqual ("family", ex.ParamName, "#B5");
318 [Test] // .ctor (Int32, AddressFamily)
319 public void Constructor5_Port_OutOfRange ()
322 new UdpClient (IPEndPoint.MaxPort + 1, AddressFamily.InterNetwork);
324 } catch (ArgumentOutOfRangeException ex) {
325 // Specified argument was out of the range of valid values
326 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
327 Assert.IsNull (ex.InnerException, "#A3");
328 Assert.IsNotNull (ex.Message, "#A4");
329 Assert.AreEqual ("port", ex.ParamName, "#A5");
333 new UdpClient (IPEndPoint.MinPort - 1, AddressFamily.InterNetwork);
335 } catch (ArgumentOutOfRangeException ex) {
336 // Specified argument was out of the range of valid values
337 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
338 Assert.IsNull (ex.InnerException, "#A3");
339 Assert.IsNotNull (ex.Message, "#A4");
340 Assert.AreEqual ("port", ex.ParamName, "#A5");
344 [Test] // .ctor (String, Int32)
345 public void Constructor6 ()
351 // UDP port 0 doesn't seem to be valid.
352 using (MyUdpClient client = new MyUdpClient ("127.0.0.1", 53))
355 Assert.IsNotNull (s, "#A:Client");
356 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
357 Assert.IsTrue (s.Connected, "#A:Client:Connected");
358 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
359 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
360 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
361 Assert.IsTrue (client.Active, "#A:Active");
362 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
363 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
364 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
365 //Assert.IsFalse (client.MulticastLoopback, "#A:MulticastLoopback");
366 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
367 localEP = s.LocalEndPoint as IPEndPoint;
368 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
369 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#A:Client:LocalEndPoint/Address");
370 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
372 using (MyUdpClient client = new MyUdpClient ("127.0.0.1", IPEndPoint.MaxPort))
375 Assert.IsNotNull (s, "#B:Client");
376 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
377 Assert.IsTrue (s.Connected, "#B:Client:Connected");
378 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
379 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
380 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
381 Assert.IsTrue (client.Active, "#B:Active");
382 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
383 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
384 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
385 //Assert.IsFalse (client.MulticastLoopback, "#B:MulticastLoopback");
386 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
387 localEP = s.LocalEndPoint as IPEndPoint;
388 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
389 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#B:Client:LocalEndPoint/Address");
390 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
394 [Test] // .ctor (String, Int32)
395 public void Constructor6_HostName_Null ()
398 new UdpClient ((string) null, int.MaxValue);
400 } catch (ArgumentNullException ex) {
401 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
402 Assert.IsNull (ex.InnerException, "#3");
403 Assert.IsNotNull (ex.Message, "#4");
404 Assert.AreEqual ("hostname", ex.ParamName, "#5");
408 [Test] // .ctor (String, Int32)
409 public void Constructor6_Port_OutOfRange ()
412 new UdpClient ("local", IPEndPoint.MaxPort + 1);
414 } catch (ArgumentOutOfRangeException ex) {
415 // Specified argument was out of the range of valid values
416 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
417 Assert.IsNull (ex.InnerException, "#A3");
418 Assert.IsNotNull (ex.Message, "#A4");
419 Assert.AreEqual ("port", ex.ParamName, "#A5");
423 new UdpClient ("local", IPEndPoint.MinPort - 1);
425 } catch (ArgumentOutOfRangeException ex) {
426 // Specified argument was out of the range of valid values
427 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
428 Assert.IsNull (ex.InnerException, "#A3");
429 Assert.IsNotNull (ex.Message, "#A4");
430 Assert.AreEqual ("port", ex.ParamName, "#A5");
435 public void UdpClientBroadcastTest ()
437 UdpClient client = new UdpClient ();
438 byte[] bytes = new byte[] {10, 11, 12, 13};
441 client.Send (bytes, bytes.Length, new IPEndPoint (IPAddress.Broadcast, 1235));
447 [Test] // JoinMulticastGroup (IPAddress)
448 public void JoinMulticastGroup1_IPv4 ()
450 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
452 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
453 client.JoinMulticastGroup (mcast_addr);
457 [Test] // JoinMulticastGroup (IPAddress)
458 public void JoinMulticastGroup1_IPv6 ()
460 if (!Socket.OSSupportsIPv6)
461 Assert.Ignore ("IPv6 not enabled.");
463 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
465 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
466 client.JoinMulticastGroup (mcast_addr);
470 [Test] // JoinMulticastGroup (IPAddress)
471 public void JoinMulticastGroup1_MulticastAddr_Null ()
473 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
475 client.JoinMulticastGroup ((IPAddress) null);
477 } catch (ArgumentNullException ex) {
478 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
479 Assert.IsNull (ex.InnerException, "#3");
480 Assert.IsNotNull (ex.Message, "#4");
481 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
486 [Test] // JoinMulticastGroup (IPAddress)
487 public void JoinMulticastGroup1_Socket_Closed ()
489 IPAddress mcast_addr = null;
491 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
494 client.JoinMulticastGroup (mcast_addr);
496 } catch (ObjectDisposedException ex) {
497 // Cannot access a disposed object
498 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
499 Assert.IsNull (ex.InnerException, "#3");
500 Assert.IsNotNull (ex.Message, "#4");
501 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
505 [Test] // JoinMulticastGroup (IPAddress)
506 [Category ("NotWorking")]
507 public void JoinMulticastGroup1_Socket_NotBound ()
509 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
511 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
513 client.JoinMulticastGroup (mcast_addr);
515 } catch (SocketException ex) {
516 // An invalid argument was supplied
517 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
518 Assert.AreEqual (10022, ex.ErrorCode, "#3");
519 Assert.IsNull (ex.InnerException, "#4");
520 Assert.IsNotNull (ex.Message, "#5");
521 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
522 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
528 [Test] // JoinMulticastGroup (In32, IPAddress)
529 public void JoinMulticastGroup2_IPv4 ()
531 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
533 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
535 client.JoinMulticastGroup (0, mcast_addr);
537 } catch (SocketException ex) {
538 // The attempted operation is not supported for the type of
540 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
541 Assert.AreEqual (10045, ex.ErrorCode, "#3");
542 Assert.IsNull (ex.InnerException, "#4");
543 Assert.IsNotNull (ex.Message, "#5");
544 Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
545 Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
550 [Test] // JoinMulticastGroup (In32, IPAddress)
551 public void JoinMulticastGroup2_IPv6 ()
553 if (!Socket.OSSupportsIPv6)
554 Assert.Ignore ("IPv6 not enabled.");
556 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
558 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
559 client.JoinMulticastGroup (0, mcast_addr);
563 [Test] // JoinMulticastGroup (Int32, IPAddress)
564 public void JoinMulticastGroup2_MulticastAddr_Null ()
566 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
568 client.JoinMulticastGroup (0, (IPAddress) null);
570 } catch (ArgumentNullException ex) {
571 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
572 Assert.IsNull (ex.InnerException, "#3");
573 Assert.IsNotNull (ex.Message, "#4");
574 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
579 [Test] // JoinMulticastGroup (Int32, IPAddress)
580 public void JoinMulticastGroup2_Socket_Closed ()
582 if (!Socket.OSSupportsIPv6)
583 Assert.Ignore ("IPv6 not enabled.");
585 IPAddress mcast_addr = null;
587 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234));
590 client.JoinMulticastGroup (0, mcast_addr);
592 } catch (ObjectDisposedException ex) {
593 // Cannot access a disposed object
594 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
595 Assert.IsNull (ex.InnerException, "#3");
596 Assert.IsNotNull (ex.Message, "#4");
597 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
601 [Test] // JoinMulticastGroup (Int32, IPAddress)
602 [Category ("NotWorking")]
603 public void JoinMulticastGroup2_Socket_NotBound ()
605 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
607 UdpClient client = new UdpClient (AddressFamily.InterNetworkV6);
609 client.JoinMulticastGroup (0, mcast_addr);
611 } catch (SocketException ex) {
612 // An invalid argument was supplied
613 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
614 Assert.AreEqual (10022, ex.ErrorCode, "#3");
615 Assert.IsNull (ex.InnerException, "#4");
616 Assert.IsNotNull (ex.Message, "#5");
617 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
618 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
624 [Test] // JoinMulticastGroup (IPAddress, Int32)
625 public void JoinMulticastGroup3_IPv4 ()
627 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
629 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
630 client.JoinMulticastGroup (mcast_addr, 0);
633 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
634 client.JoinMulticastGroup (mcast_addr, 255);
638 [Test] // JoinMulticastGroup (IPAddress, Int32)
639 public void JoinMulticastGroup3_IPv6 ()
641 if (!Socket.OSSupportsIPv6)
642 Assert.Ignore ("IPv6 not enabled.");
644 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
646 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
647 client.JoinMulticastGroup (mcast_addr, 0);
650 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
651 client.JoinMulticastGroup (mcast_addr, 255);
655 [Test] // JoinMulticastGroup (IPAddress, Int32)
656 public void JoinMulticastGroup3_MulticastAddr_Null ()
658 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
660 client.JoinMulticastGroup ((IPAddress) null, int.MaxValue);
662 } catch (ArgumentNullException ex) {
663 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
664 Assert.IsNull (ex.InnerException, "#3");
665 Assert.IsNotNull (ex.Message, "#4");
666 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
671 [Test] // JoinMulticastGroup (IPAddress, Int32)
672 public void JoinMulticastGroup3_Socket_Closed ()
674 IPAddress mcast_addr = null;
676 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
679 client.JoinMulticastGroup (mcast_addr, 0);
681 } catch (ObjectDisposedException ex) {
682 // Cannot access a disposed object
683 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
684 Assert.IsNull (ex.InnerException, "#3");
685 Assert.IsNotNull (ex.Message, "#4");
686 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
690 [Test] // JoinMulticastGroup (IPAddress, Int32)
691 [Category ("NotWorking")]
692 public void JoinMulticastGroup3_Socket_NotBound ()
694 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
696 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
698 client.JoinMulticastGroup (mcast_addr, 5);
700 } catch (SocketException ex) {
701 // An invalid argument was supplied
702 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
703 Assert.AreEqual (10022, ex.ErrorCode, "#3");
704 Assert.IsNull (ex.InnerException, "#4");
705 Assert.IsNotNull (ex.Message, "#5");
706 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
707 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
713 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
714 public void JoinMulticastGroup4_IPv4 ()
716 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
717 IPAddress local_addr = IPAddress.Any;
719 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
720 client.JoinMulticastGroup (mcast_addr, local_addr);
724 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
725 public void JoinMulticastGroup4_IPv6 ()
727 if (!Socket.OSSupportsIPv6)
728 Assert.Ignore ("IPv6 not enabled.");
730 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
731 IPAddress local_addr = IPAddress.IPv6Any;
733 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
735 client.JoinMulticastGroup (mcast_addr, local_addr);
737 } catch (SocketException ex) {
738 // The attempted operation is not supported for the type of
740 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
741 Assert.AreEqual (10045, ex.ErrorCode, "#3");
742 Assert.IsNull (ex.InnerException, "#4");
743 Assert.IsNotNull (ex.Message, "#5");
744 Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
745 Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
750 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
751 public void JoinMulticastGroup4_LocalAddress_Null ()
753 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
755 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
757 client.JoinMulticastGroup (mcast_addr, (IPAddress) null);
759 } catch (ArgumentNullException ex) {
760 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
761 Assert.IsNull (ex.InnerException, "#3");
762 Assert.IsNotNull (ex.Message, "#4");
763 Assert.AreEqual ("mcint", ex.ParamName, "#5");
768 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
769 public void JoinMulticastGroup4_MulticastAddr_Null ()
771 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
773 client.JoinMulticastGroup ((IPAddress) null, IPAddress.Loopback);
775 } catch (ArgumentNullException ex) {
776 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
777 Assert.IsNull (ex.InnerException, "#3");
778 Assert.IsNotNull (ex.Message, "#4");
779 Assert.AreEqual ("group", ex.ParamName, "#5");
784 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
785 public void JoinMulticastGroup4_Socket_Closed ()
787 IPAddress mcast_addr = null;
788 IPAddress local_addr = null;
790 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
793 client.JoinMulticastGroup (mcast_addr, local_addr);
795 } catch (ObjectDisposedException ex) {
796 // Cannot access a disposed object
797 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
798 Assert.IsNull (ex.InnerException, "#3");
799 Assert.IsNotNull (ex.Message, "#4");
800 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
804 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
805 [Category ("NotWorking")]
806 public void JoinMulticastGroup4_Socket_NotBound ()
808 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
809 IPAddress local_addr = Dns.GetHostEntry (string.Empty).AddressList [0];
811 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
813 client.JoinMulticastGroup (mcast_addr, local_addr);
815 } catch (SocketException ex) {
816 // An invalid argument was supplied
817 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
818 Assert.AreEqual (10022, ex.ErrorCode, "#3");
819 Assert.IsNull (ex.InnerException, "#4");
820 Assert.IsNotNull (ex.Message, "#5");
821 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
822 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
829 public void CloseInReceive ()
831 UdpClient client = null;
832 var rnd = new Random ();
833 for (int i = 0; i < 5; i++) {
834 int port = rnd.Next (1025, 65534);
836 client = new UdpClient (port);
838 } catch (Exception) {
844 new Thread(delegate() {
849 bool got_exc = false;
850 IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
852 client.Receive(ref ep);
853 } catch (SocketException) {
858 Assert.IsTrue (got_exc);
861 // Test for bug 324033
863 public void JoinMulticastGroupWithLocal ()
865 UdpClient client = new UdpClient (9001);
866 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.24");
867 IPAddress local_addr = IPAddress.Any;
870 client.JoinMulticastGroup (mcast_addr, local_addr);
877 [ExpectedException (typeof(ArgumentNullException))]
878 public void BeginSendNull ()
880 UdpClient client = new UdpClient ();
881 client.BeginSend (null, 0, null, null);
885 static bool BSSent = false;
887 static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
889 private static void BSCallback (IAsyncResult asyncResult)
891 UdpClient client = (UdpClient)asyncResult.AsyncState;
893 BSBytes = client.EndSend (asyncResult);
900 public void BeginSend ()
902 UdpClient client = new UdpClient ();
903 byte[] bytes = new byte[] {10, 11, 12, 13};
906 client.BeginSend (bytes, bytes.Length, new AsyncCallback (BSCallback), client);
907 Assert.Fail ("BeginSend #1");
908 } catch (SocketException ex) {
909 Assert.AreEqual (10057, ex.ErrorCode,
914 client.BeginSend (bytes, bytes.Length, null, new AsyncCallback (BSCallback), client);
915 Assert.Fail ("BeginSend #3");
916 } catch (SocketException ex) {
917 Assert.AreEqual (10057, ex.ErrorCode,
921 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1236);
923 BSCalledBack.Reset ();
925 client.BeginSend (bytes, bytes.Length, ep,
926 new AsyncCallback (BSCallback),
928 if (BSCalledBack.WaitOne (2000, false) == false) {
929 Assert.Fail ("BeginSend wait timed out");
932 Assert.AreEqual (true, BSSent, "BeginSend #5");
933 Assert.AreEqual (4, BSBytes, "BeginSend #6");
938 static bool BRReceived = false;
939 static byte[] BRBytes;
940 static IPEndPoint BRFrom;
941 static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
943 private static void BRCallback (IAsyncResult asyncResult)
945 UdpClient client = (UdpClient)asyncResult.AsyncState;
947 BRBytes = client.EndReceive (asyncResult, ref BRFrom);
954 public void BeginReceive ()
956 UdpClient client = new UdpClient (1237);
958 BRCalledBack.Reset ();
960 client.BeginReceive (BRCallback, client);
962 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1237);
963 byte[] send_bytes = new byte[] {10, 11, 12, 13};
964 client.Send (send_bytes, send_bytes.Length, ep);
966 if (BRCalledBack.WaitOne (2000, false) == false) {
967 Assert.Fail ("BeginReceive wait timed out");
970 Assert.AreEqual (true, BRReceived, "BeginReceive #1");
971 Assert.AreEqual (4, BRBytes.Length, "BeginReceive #2");
972 Assert.AreEqual (ep. Port, BRFrom.Port, "BeginReceive #3");
973 Assert.AreEqual (ep.Address, BRFrom.Address, "BeginReceive #4");
979 public void Available ()
981 using (UdpClient client = new UdpClient (1238)) {
982 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1238);
983 byte[] bytes = new byte[] {10, 11, 12, 13};
985 int res = client.Send (bytes, bytes.Length, ep);
986 Assert.AreEqual (bytes.Length, res, "Send");
988 // that might happen too quickly, data sent and not yet received/available
990 int avail = client.Available;
992 Assert.AreEqual (bytes.Length, avail, "Available #1");
999 [Category ("NotWorking")] // Using PMTU settings workaround on Linux, default true
1000 public void DontFragmentDefault ()
1002 UdpClient client = new UdpClient ();
1004 /* Ignore the docs, testing shows the default
1005 * here is in fact false
1007 Assert.AreEqual (false, client.DontFragment, "DontFragmentDefault");
1013 public void EnableBroadcastDefault ()
1015 UdpClient client = new UdpClient ();
1017 Assert.AreEqual (false, client.EnableBroadcast, "EnableBroadcastDefault");
1022 /* Can't test the default for ExclusiveAddressUse as
1023 * it's different on different versions and service
1027 [Category ("NotWorking")] // Not supported on Linux
1028 public void ExclusiveAddressUseUnbound ()
1030 UdpClient client = new UdpClient ();
1032 client.ExclusiveAddressUse = true;
1034 Assert.AreEqual (true, client.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
1040 [ExpectedException (typeof(InvalidOperationException))]
1041 [Category ("NotWorking")] // Not supported on Linux
1042 public void ExclusiveAddressUseBound ()
1044 UdpClient client = new UdpClient (1239);
1046 client.ExclusiveAddressUse = true;
1052 public void MulticastLoopbackDefault ()
1054 UdpClient client = new UdpClient ();
1056 Assert.AreEqual (true, client.MulticastLoopback, "MulticastLoopbackDefault");
1061 /* No test for Ttl default as it is platform dependent */
1063 class MyUdpClient : UdpClient
1065 public MyUdpClient ()
1069 public MyUdpClient (AddressFamily family)
1074 public MyUdpClient (Int32 port)
1080 public MyUdpClient (IPEndPoint localEP)
1085 public MyUdpClient (int port, AddressFamily family)
1086 : base (port, family)
1090 public MyUdpClient (string hostname, int port)
1091 : base (hostname, port)
1095 public new bool Active {
1096 get { return base.Active; }
1097 set { base.Active = value; }