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 [Ignore("UdpClient is not supported - since UDP sockets are not supported")]
20 public class UdpClientTest {
22 public void Constructor1 ()
27 client = new MyUdpClient ();
29 Assert.IsNotNull (s, "Client");
30 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "Client:AddressFamily");
31 Assert.IsFalse (s.Connected, "Client:Connected");
33 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
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");
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");
50 [Test] // .ctor (AddressFamily)
51 public void Constructor2 ()
56 client = new MyUdpClient (AddressFamily.InterNetwork);
58 Assert.IsNotNull (s, "#A:Client");
59 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
60 Assert.IsFalse (s.Connected, "#A:Client:Connected");
62 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
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");
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");
78 if (!Socket.OSSupportsIPv6)
80 if (!Socket.SupportsIPv6)
82 Assert.Ignore ("IPv6 not enabled.");
84 client = new MyUdpClient (AddressFamily.InterNetworkV6);
86 Assert.IsNotNull (s, "#B:Client");
87 Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
88 Assert.IsFalse (s.Connected, "#B:Client:Connected");
90 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
92 Assert.IsNull (s.LocalEndPoint, "#B:Client:LocalEndPoint");
93 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
94 Assert.IsNull (s.RemoteEndPoint, "#B:Client:RemoteEndPoint");
95 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
96 Assert.IsFalse (client.Active, "#B:Active");
98 //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
99 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
100 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
101 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
102 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
107 [Test] // .ctor (AddressFamily)
108 public void Constructor2_Family_Invalid ()
111 new UdpClient (AddressFamily.NetBios);
113 } catch (ArgumentException ex) {
114 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
115 Assert.IsNull (ex.InnerException, "#A3");
117 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
119 Assert.IsNotNull (ex.Message, "#A4");
120 Assert.AreEqual ("family", ex.ParamName, "#A5");
122 Assert.AreEqual ("family", ex.Message, "#A4");
123 Assert.IsNull (ex.ParamName, "#A5");
128 new UdpClient ((AddressFamily) 666);
130 } catch (ArgumentException ex) {
131 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
132 Assert.IsNull (ex.InnerException, "#B3");
134 Assert.IsNotNull (ex.Message, "#B4");
135 Assert.AreEqual ("family", ex.ParamName, "#B5");
137 Assert.AreEqual ("family", ex.Message, "#B4");
138 Assert.IsNull (ex.ParamName, "#B5");
143 [Test] // .ctor (Int32)
144 public void Constructor3 ()
149 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort))
152 Assert.IsNotNull (s, "#A:Client");
153 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
154 Assert.IsFalse (s.Connected, "#A:Client:Connected");
156 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
158 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
159 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
160 Assert.IsFalse (client.Active, "#A:Active");
162 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
163 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
164 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
165 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
166 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
168 localEP = s.LocalEndPoint as IPEndPoint;
169 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
170 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
171 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
173 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort))
176 Assert.IsNotNull (s, "#B:Client");
177 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
178 Assert.IsFalse (s.Connected, "#B:Client:Connected");
180 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
182 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
183 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
184 Assert.IsFalse (client.Active, "#B:Active");
186 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
187 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
188 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
189 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
190 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
192 localEP = s.LocalEndPoint as IPEndPoint;
193 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
194 Assert.AreEqual (IPAddress.Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
195 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
196 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
200 [Test] // .ctor (Int32)
201 public void Constructor3_Port_OutOfRange ()
204 new UdpClient (IPEndPoint.MaxPort + 1);
206 } catch (ArgumentOutOfRangeException ex) {
207 // Specified argument was out of the range of valid values
208 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
209 Assert.IsNull (ex.InnerException, "#A3");
210 Assert.IsNotNull (ex.Message, "#A4");
211 Assert.AreEqual ("port", ex.ParamName, "#A5");
215 new UdpClient (IPEndPoint.MinPort - 1);
217 } catch (ArgumentOutOfRangeException ex) {
218 // Specified argument was out of the range of valid values
219 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
220 Assert.IsNull (ex.InnerException, "#A3");
221 Assert.IsNotNull (ex.Message, "#A4");
222 Assert.AreEqual ("port", ex.ParamName, "#A5");
226 [Test] // .ctor (IPEndPoint)
227 public void Constructor4 ()
233 clientEP = new IPEndPoint (IPAddress.Loopback, 8001);
234 using (MyUdpClient client = new MyUdpClient (clientEP))
237 Assert.IsNotNull (s, "#A:Client");
238 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
239 Assert.IsFalse (s.Connected, "#A:Client:Connected");
241 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
243 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
244 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
245 Assert.IsFalse (client.Active, "#A:Active");
247 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
248 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
249 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
250 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
251 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
253 localEP = s.LocalEndPoint as IPEndPoint;
254 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
255 Assert.IsFalse (object.ReferenceEquals (clientEP, localEP), "#A:Client:LocalEndPoint/ReferenceEquality");
256 Assert.AreEqual (clientEP.Address, localEP.Address, "#A:Client:LocalEndPoint/Address");
257 Assert.AreEqual (clientEP.AddressFamily, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
258 Assert.AreEqual (clientEP.Port, localEP.Port, "#A:Client:LocalEndPoint/Port");
262 [Test] // .ctor (IPEndPoint)
263 public void Constructor4_LocalEP_Null ()
266 new UdpClient ((IPEndPoint) null);
268 } catch (ArgumentNullException ex) {
269 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
270 Assert.IsNull (ex.InnerException, "#3");
271 Assert.IsNotNull (ex.Message, "#4");
272 Assert.AreEqual ("localEP", ex.ParamName, "#5");
276 [Test] // .ctor (Int32, AddressFamily)
277 public void Constructor5 ()
282 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort, AddressFamily.InterNetwork))
285 Assert.IsNotNull (s, "#A:Client");
286 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
287 Assert.IsFalse (s.Connected, "#A:Client:Connected");
289 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
291 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
292 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
293 Assert.IsFalse (client.Active, "#A:Active");
295 //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
296 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
297 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
298 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
299 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
301 localEP = s.LocalEndPoint as IPEndPoint;
302 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
303 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
304 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
308 if (!Socket.OSSupportsIPv6)
310 if (!Socket.SupportsIPv6)
312 Assert.Ignore ("IPv6 not enabled.");
314 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort, AddressFamily.InterNetworkV6))
317 Assert.IsNotNull (s, "#B:Client");
318 Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
319 Assert.IsFalse (s.Connected, "#B:Client:Connected");
321 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
323 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
324 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
325 Assert.IsFalse (client.Active, "#B:Active");
327 //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
328 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
329 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
330 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
331 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
333 localEP = s.LocalEndPoint as IPEndPoint;
334 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
335 Assert.AreEqual (IPAddress.IPv6Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
336 Assert.AreEqual (AddressFamily.InterNetworkV6, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
337 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
341 [Test] // .ctor (Int32, AddressFamily)
342 public void Constructor5_Family_Invalid ()
345 new UdpClient (80, AddressFamily.NetBios);
347 } catch (ArgumentException ex) {
349 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
350 Assert.IsNull (ex.InnerException, "#A3");
352 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
354 Assert.IsNotNull (ex.Message, "#A4");
355 Assert.AreEqual ("family", ex.ParamName, "#A5");
357 Assert.AreEqual ("family", ex.Message, "#A4");
358 Assert.IsNull (ex.ParamName, "#A5");
363 new UdpClient (80, (AddressFamily) 666);
365 } catch (ArgumentException ex) {
367 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
368 Assert.IsNull (ex.InnerException, "#B3");
370 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
372 Assert.IsNotNull (ex.Message, "#B4");
373 Assert.AreEqual ("family", ex.ParamName, "#B5");
375 Assert.AreEqual ("family", ex.Message, "#B4");
376 Assert.IsNull (ex.ParamName, "#B5");
381 [Test] // .ctor (Int32, AddressFamily)
382 public void Constructor5_Port_OutOfRange ()
385 new UdpClient (IPEndPoint.MaxPort + 1, AddressFamily.InterNetwork);
387 } catch (ArgumentOutOfRangeException ex) {
388 // Specified argument was out of the range of valid values
389 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
390 Assert.IsNull (ex.InnerException, "#A3");
391 Assert.IsNotNull (ex.Message, "#A4");
392 Assert.AreEqual ("port", ex.ParamName, "#A5");
396 new UdpClient (IPEndPoint.MinPort - 1, AddressFamily.InterNetwork);
398 } catch (ArgumentOutOfRangeException ex) {
399 // Specified argument was out of the range of valid values
400 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
401 Assert.IsNull (ex.InnerException, "#A3");
402 Assert.IsNotNull (ex.Message, "#A4");
403 Assert.AreEqual ("port", ex.ParamName, "#A5");
407 [Test] // .ctor (String, Int32)
408 public void Constructor6 ()
414 // UDP port 0 doesn't seem to be valid.
415 using (MyUdpClient client = new MyUdpClient ("127.0.0.1", 53))
418 Assert.IsNotNull (s, "#A:Client");
419 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
420 Assert.IsTrue (s.Connected, "#A:Client:Connected");
422 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
424 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
425 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
426 Assert.IsTrue (client.Active, "#A:Active");
428 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
429 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
430 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
431 //Assert.IsFalse (client.MulticastLoopback, "#A:MulticastLoopback");
432 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
434 localEP = s.LocalEndPoint as IPEndPoint;
435 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
436 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#A:Client:LocalEndPoint/Address");
437 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
439 using (MyUdpClient client = new MyUdpClient ("127.0.0.1", IPEndPoint.MaxPort))
442 Assert.IsNotNull (s, "#B:Client");
443 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
444 Assert.IsTrue (s.Connected, "#B:Client:Connected");
446 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
448 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
449 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
450 Assert.IsTrue (client.Active, "#B:Active");
452 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
453 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
454 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
455 //Assert.IsFalse (client.MulticastLoopback, "#B:MulticastLoopback");
456 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
458 localEP = s.LocalEndPoint as IPEndPoint;
459 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
460 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#B:Client:LocalEndPoint/Address");
461 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
465 [Test] // .ctor (String, Int32)
466 public void Constructor6_HostName_Null ()
469 new UdpClient ((string) null, int.MaxValue);
471 } catch (ArgumentNullException ex) {
472 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
473 Assert.IsNull (ex.InnerException, "#3");
474 Assert.IsNotNull (ex.Message, "#4");
475 Assert.AreEqual ("hostname", ex.ParamName, "#5");
479 [Test] // .ctor (String, Int32)
480 public void Constructor6_Port_OutOfRange ()
483 new UdpClient ("local", IPEndPoint.MaxPort + 1);
485 } catch (ArgumentOutOfRangeException ex) {
486 // Specified argument was out of the range of valid values
487 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
488 Assert.IsNull (ex.InnerException, "#A3");
489 Assert.IsNotNull (ex.Message, "#A4");
490 Assert.AreEqual ("port", ex.ParamName, "#A5");
494 new UdpClient ("local", IPEndPoint.MinPort - 1);
496 } catch (ArgumentOutOfRangeException ex) {
497 // Specified argument was out of the range of valid values
498 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
499 Assert.IsNull (ex.InnerException, "#A3");
500 Assert.IsNotNull (ex.Message, "#A4");
501 Assert.AreEqual ("port", ex.ParamName, "#A5");
506 public void UdpClientBroadcastTest ()
508 UdpClient client = new UdpClient ();
509 byte[] bytes = new byte[] {10, 11, 12, 13};
512 client.Send (bytes, bytes.Length, new IPEndPoint (IPAddress.Broadcast, 1235));
518 [Test] // JoinMulticastGroup (IPAddress)
519 public void JoinMulticastGroup1_IPv4 ()
521 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
523 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
524 client.JoinMulticastGroup (mcast_addr);
528 [Test] // JoinMulticastGroup (IPAddress)
529 public void JoinMulticastGroup1_IPv6 ()
532 if (!Socket.OSSupportsIPv6)
534 if (!Socket.SupportsIPv6)
536 Assert.Ignore ("IPv6 not enabled.");
538 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
540 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
541 client.JoinMulticastGroup (mcast_addr);
545 [Test] // JoinMulticastGroup (IPAddress)
546 public void JoinMulticastGroup1_MulticastAddr_Null ()
548 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
550 client.JoinMulticastGroup ((IPAddress) null);
553 } catch (ArgumentNullException ex) {
554 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
555 Assert.IsNull (ex.InnerException, "#3");
556 Assert.IsNotNull (ex.Message, "#4");
557 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
560 } catch (NullReferenceException ex) {
561 Assert.AreEqual (typeof (NullReferenceException), ex.GetType (), "#2");
562 Assert.IsNull (ex.InnerException, "#3");
563 Assert.IsNotNull (ex.Message, "#4");
569 [Test] // JoinMulticastGroup (IPAddress)
570 public void JoinMulticastGroup1_Socket_Closed ()
572 IPAddress mcast_addr = null;
574 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
577 client.JoinMulticastGroup (mcast_addr);
579 } catch (ObjectDisposedException ex) {
580 // Cannot access a disposed object
581 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
582 Assert.IsNull (ex.InnerException, "#3");
583 Assert.IsNotNull (ex.Message, "#4");
584 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
588 [Test] // JoinMulticastGroup (IPAddress)
589 [Category ("NotWorking")]
590 public void JoinMulticastGroup1_Socket_NotBound ()
592 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
594 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
596 client.JoinMulticastGroup (mcast_addr);
598 } catch (SocketException ex) {
599 // An invalid argument was supplied
600 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
601 Assert.AreEqual (10022, ex.ErrorCode, "#3");
602 Assert.IsNull (ex.InnerException, "#4");
603 Assert.IsNotNull (ex.Message, "#5");
604 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
606 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
613 [Test] // JoinMulticastGroup (In32, IPAddress)
614 public void JoinMulticastGroup2_IPv4 ()
616 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
618 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
620 client.JoinMulticastGroup (0, mcast_addr);
622 } catch (SocketException ex) {
623 // The attempted operation is not supported for the type of
625 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
626 Assert.AreEqual (10045, ex.ErrorCode, "#3");
627 Assert.IsNull (ex.InnerException, "#4");
628 Assert.IsNotNull (ex.Message, "#5");
629 Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
631 Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
637 [Test] // JoinMulticastGroup (In32, IPAddress)
638 public void JoinMulticastGroup2_IPv6 ()
641 if (!Socket.OSSupportsIPv6)
643 if (!Socket.SupportsIPv6)
645 Assert.Ignore ("IPv6 not enabled.");
647 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
649 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
650 client.JoinMulticastGroup (0, mcast_addr);
654 [Test] // JoinMulticastGroup (Int32, IPAddress)
655 public void JoinMulticastGroup2_MulticastAddr_Null ()
657 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
659 client.JoinMulticastGroup (0, (IPAddress) null);
661 } catch (ArgumentNullException ex) {
662 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
663 Assert.IsNull (ex.InnerException, "#3");
664 Assert.IsNotNull (ex.Message, "#4");
665 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
670 [Test] // JoinMulticastGroup (Int32, IPAddress)
671 public void JoinMulticastGroup2_Socket_Closed ()
674 if (!Socket.OSSupportsIPv6)
676 if (!Socket.SupportsIPv6)
678 Assert.Ignore ("IPv6 not enabled.");
680 IPAddress mcast_addr = null;
682 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234));
685 client.JoinMulticastGroup (0, mcast_addr);
687 } catch (ObjectDisposedException ex) {
688 // Cannot access a disposed object
689 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
690 Assert.IsNull (ex.InnerException, "#3");
691 Assert.IsNotNull (ex.Message, "#4");
692 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
696 [Test] // JoinMulticastGroup (Int32, IPAddress)
697 [Category ("NotWorking")]
698 public void JoinMulticastGroup2_Socket_NotBound ()
700 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
702 UdpClient client = new UdpClient (AddressFamily.InterNetworkV6);
704 client.JoinMulticastGroup (0, mcast_addr);
706 } catch (SocketException ex) {
707 // An invalid argument was supplied
708 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
709 Assert.AreEqual (10022, ex.ErrorCode, "#3");
710 Assert.IsNull (ex.InnerException, "#4");
711 Assert.IsNotNull (ex.Message, "#5");
712 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
714 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
721 [Test] // JoinMulticastGroup (IPAddress, Int32)
722 public void JoinMulticastGroup3_IPv4 ()
724 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
726 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
727 client.JoinMulticastGroup (mcast_addr, 0);
730 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
731 client.JoinMulticastGroup (mcast_addr, 255);
735 [Test] // JoinMulticastGroup (IPAddress, Int32)
736 public void JoinMulticastGroup3_IPv6 ()
739 if (!Socket.OSSupportsIPv6)
741 if (!Socket.SupportsIPv6)
743 Assert.Ignore ("IPv6 not enabled.");
745 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
747 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
748 client.JoinMulticastGroup (mcast_addr, 0);
751 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
752 client.JoinMulticastGroup (mcast_addr, 255);
756 [Test] // JoinMulticastGroup (IPAddress, Int32)
757 public void JoinMulticastGroup3_MulticastAddr_Null ()
759 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
761 client.JoinMulticastGroup ((IPAddress) null, int.MaxValue);
763 } catch (ArgumentNullException ex) {
764 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
765 Assert.IsNull (ex.InnerException, "#3");
766 Assert.IsNotNull (ex.Message, "#4");
767 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
772 [Test] // JoinMulticastGroup (IPAddress, Int32)
773 public void JoinMulticastGroup3_Socket_Closed ()
775 IPAddress mcast_addr = null;
777 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
780 client.JoinMulticastGroup (mcast_addr, 0);
782 } catch (ObjectDisposedException ex) {
783 // Cannot access a disposed object
784 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
785 Assert.IsNull (ex.InnerException, "#3");
786 Assert.IsNotNull (ex.Message, "#4");
787 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
791 [Test] // JoinMulticastGroup (IPAddress, Int32)
792 [Category ("NotWorking")]
793 public void JoinMulticastGroup3_Socket_NotBound ()
795 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
797 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
799 client.JoinMulticastGroup (mcast_addr, 5);
801 } catch (SocketException ex) {
802 // An invalid argument was supplied
803 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
804 Assert.AreEqual (10022, ex.ErrorCode, "#3");
805 Assert.IsNull (ex.InnerException, "#4");
806 Assert.IsNotNull (ex.Message, "#5");
807 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
809 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
817 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
818 public void JoinMulticastGroup4_IPv4 ()
820 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
821 IPAddress local_addr = IPAddress.Any;
823 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
824 client.JoinMulticastGroup (mcast_addr, local_addr);
828 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
829 public void JoinMulticastGroup4_IPv6 ()
831 if (!Socket.OSSupportsIPv6)
832 Assert.Ignore ("IPv6 not enabled.");
834 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
835 IPAddress local_addr = IPAddress.IPv6Any;
837 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
839 client.JoinMulticastGroup (mcast_addr, local_addr);
841 } catch (SocketException ex) {
842 // The attempted operation is not supported for the type of
844 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
845 Assert.AreEqual (10045, ex.ErrorCode, "#3");
846 Assert.IsNull (ex.InnerException, "#4");
847 Assert.IsNotNull (ex.Message, "#5");
848 Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
849 Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
854 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
855 public void JoinMulticastGroup4_LocalAddress_Null ()
857 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
859 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
861 client.JoinMulticastGroup (mcast_addr, (IPAddress) null);
863 } catch (ArgumentNullException ex) {
864 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
865 Assert.IsNull (ex.InnerException, "#3");
866 Assert.IsNotNull (ex.Message, "#4");
867 Assert.AreEqual ("mcint", ex.ParamName, "#5");
872 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
873 public void JoinMulticastGroup4_MulticastAddr_Null ()
875 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
877 client.JoinMulticastGroup ((IPAddress) null, IPAddress.Loopback);
879 } catch (ArgumentNullException ex) {
880 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
881 Assert.IsNull (ex.InnerException, "#3");
882 Assert.IsNotNull (ex.Message, "#4");
883 Assert.AreEqual ("group", ex.ParamName, "#5");
888 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
889 public void JoinMulticastGroup4_Socket_Closed ()
891 IPAddress mcast_addr = null;
892 IPAddress local_addr = null;
894 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
897 client.JoinMulticastGroup (mcast_addr, local_addr);
899 } catch (ObjectDisposedException ex) {
900 // Cannot access a disposed object
901 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
902 Assert.IsNull (ex.InnerException, "#3");
903 Assert.IsNotNull (ex.Message, "#4");
904 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
908 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
909 [Category ("NotWorking")]
910 public void JoinMulticastGroup4_Socket_NotBound ()
912 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
913 IPAddress local_addr = Dns.GetHostEntry (string.Empty).AddressList [0];
915 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
917 client.JoinMulticastGroup (mcast_addr, local_addr);
919 } catch (SocketException ex) {
920 // An invalid argument was supplied
921 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
922 Assert.AreEqual (10022, ex.ErrorCode, "#3");
923 Assert.IsNull (ex.InnerException, "#4");
924 Assert.IsNotNull (ex.Message, "#5");
925 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
926 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
933 public void CloseInReceive ()
935 UdpClient client = null;
936 var rnd = new Random ();
937 for (int i = 0; i < 5; i++) {
938 int port = rnd.Next (1025, 65534);
940 client = new UdpClient (port);
942 } catch (Exception ex) {
948 new Thread(delegate() {
953 bool got_exc = false;
954 IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
956 client.Receive(ref ep);
957 } catch (SocketException) {
962 Assert.IsTrue (got_exc);
965 // Test for bug 324033
967 public void JoinMulticastGroupWithLocal ()
969 UdpClient client = new UdpClient (9001);
970 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.24");
971 IPAddress local_addr = IPAddress.Any;
974 client.JoinMulticastGroup (mcast_addr, local_addr);
981 [ExpectedException (typeof(ArgumentNullException))]
982 public void BeginSendNull ()
984 UdpClient client = new UdpClient ();
985 client.BeginSend (null, 0, null, null);
989 static bool BSSent = false;
991 static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
993 private static void BSCallback (IAsyncResult asyncResult)
995 UdpClient client = (UdpClient)asyncResult.AsyncState;
997 BSBytes = client.EndSend (asyncResult);
1000 BSCalledBack.Set ();
1004 public void BeginSend ()
1006 UdpClient client = new UdpClient ();
1007 byte[] bytes = new byte[] {10, 11, 12, 13};
1010 client.BeginSend (bytes, bytes.Length, new AsyncCallback (BSCallback), client);
1011 Assert.Fail ("BeginSend #1");
1012 } catch (SocketException ex) {
1013 Assert.AreEqual (10057, ex.ErrorCode,
1018 client.BeginSend (bytes, bytes.Length, null, new AsyncCallback (BSCallback), client);
1019 Assert.Fail ("BeginSend #3");
1020 } catch (SocketException ex) {
1021 Assert.AreEqual (10057, ex.ErrorCode,
1025 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1236);
1027 BSCalledBack.Reset ();
1029 client.BeginSend (bytes, bytes.Length, ep,
1030 new AsyncCallback (BSCallback),
1032 if (BSCalledBack.WaitOne (2000, false) == false) {
1033 Assert.Fail ("BeginSend wait timed out");
1036 Assert.AreEqual (true, BSSent, "BeginSend #5");
1037 Assert.AreEqual (4, BSBytes, "BeginSend #6");
1042 static bool BRReceived = false;
1043 static byte[] BRBytes;
1044 static IPEndPoint BRFrom;
1045 static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
1047 private static void BRCallback (IAsyncResult asyncResult)
1049 UdpClient client = (UdpClient)asyncResult.AsyncState;
1051 BRBytes = client.EndReceive (asyncResult, ref BRFrom);
1054 BRCalledBack.Set ();
1058 public void BeginReceive ()
1060 UdpClient client = new UdpClient (1237);
1062 BRCalledBack.Reset ();
1064 client.BeginReceive (BRCallback, client);
1066 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1237);
1067 byte[] send_bytes = new byte[] {10, 11, 12, 13};
1068 client.Send (send_bytes, send_bytes.Length, ep);
1070 if (BRCalledBack.WaitOne (2000, false) == false) {
1071 Assert.Fail ("BeginReceive wait timed out");
1074 Assert.AreEqual (true, BRReceived, "BeginReceive #1");
1075 Assert.AreEqual (4, BRBytes.Length, "BeginReceive #2");
1076 Assert.AreEqual (ep. Port, BRFrom.Port, "BeginReceive #3");
1077 Assert.AreEqual (ep.Address, BRFrom.Address, "BeginReceive #4");
1083 public void Available ()
1085 using (UdpClient client = new UdpClient (1238)) {
1086 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1238);
1087 byte[] bytes = new byte[] {10, 11, 12, 13};
1089 int res = client.Send (bytes, bytes.Length, ep);
1090 Assert.AreEqual (bytes.Length, res, "Send");
1092 // that might happen too quickly, data sent and not yet received/available
1094 int avail = client.Available;
1096 Assert.AreEqual (bytes.Length, avail, "Available #1");
1103 [Category ("NotWorking")] // Using PMTU settings workaround on Linux, default true
1104 public void DontFragmentDefault ()
1106 UdpClient client = new UdpClient ();
1108 /* Ignore the docs, testing shows the default
1109 * here is in fact false
1111 Assert.AreEqual (false, client.DontFragment, "DontFragmentDefault");
1117 public void EnableBroadcastDefault ()
1119 UdpClient client = new UdpClient ();
1121 Assert.AreEqual (false, client.EnableBroadcast, "EnableBroadcastDefault");
1126 /* Can't test the default for ExclusiveAddressUse as
1127 * it's different on different versions and service
1131 [Category ("NotWorking")] // Not supported on Linux
1132 public void ExclusiveAddressUseUnbound ()
1134 UdpClient client = new UdpClient ();
1136 client.ExclusiveAddressUse = true;
1138 Assert.AreEqual (true, client.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
1144 [ExpectedException (typeof(InvalidOperationException))]
1145 [Category ("NotWorking")] // Not supported on Linux
1146 public void ExclusiveAddressUseBound ()
1148 UdpClient client = new UdpClient (1239);
1150 client.ExclusiveAddressUse = true;
1156 public void MulticastLoopbackDefault ()
1158 UdpClient client = new UdpClient ();
1160 Assert.AreEqual (true, client.MulticastLoopback, "MulticastLoopbackDefault");
1165 /* No test for Ttl default as it is platform dependent */
1168 class MyUdpClient : UdpClient
1170 public MyUdpClient ()
1174 public MyUdpClient (AddressFamily family)
1179 public MyUdpClient (Int32 port)
1185 public MyUdpClient (IPEndPoint localEP)
1190 public MyUdpClient (int port, AddressFamily family)
1191 : base (port, family)
1195 public MyUdpClient (string hostname, int port)
1196 : base (hostname, port)
1200 public new bool Active {
1201 get { return base.Active; }
1202 set { base.Active = value; }
1206 public new Socket Client {
1207 get { return base.Client; }
1208 set { base.Client = value; }