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");
30 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
32 Assert.IsNull (s.LocalEndPoint, "Client:LocalEndPoint");
33 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "Client:ProtocolType");
34 Assert.IsNull (s.RemoteEndPoint, "Client:RemoteEndPoint");
35 Assert.AreEqual (SocketType.Dgram, s.SocketType, "Client:SocketType");
36 Assert.IsFalse (client.Active, "Active");
38 Assert.IsFalse (client.DontFragment, "DontFragment");
39 Assert.IsFalse (client.EnableBroadcast, "EnableBroadcast");
40 //Assert.IsFalse (client.ExclusiveAddressUse, "ExclusiveAddressUse");
41 Assert.IsTrue (client.MulticastLoopback, "MulticastLoopback");
42 //Assert.AreEqual (32, client.Ttl, "Ttl");
47 [Test] // .ctor (AddressFamily)
48 public void Constructor2 ()
53 client = new MyUdpClient (AddressFamily.InterNetwork);
55 Assert.IsNotNull (s, "#A:Client");
56 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
57 Assert.IsFalse (s.Connected, "#A:Client:Connected");
59 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
61 Assert.IsNull (s.LocalEndPoint, "#A:Client:LocalEndPoint");
62 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
63 Assert.IsNull (s.RemoteEndPoint, "#A:Client:RemoteEndPoint");
64 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
65 Assert.IsFalse (client.Active, "#A:Active");
67 //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
68 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
69 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
70 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
71 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
75 if (!Socket.OSSupportsIPv6)
77 if (!Socket.SupportsIPv6)
79 Assert.Ignore ("IPv6 not enabled.");
81 client = new MyUdpClient (AddressFamily.InterNetworkV6);
83 Assert.IsNotNull (s, "#B:Client");
84 Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
85 Assert.IsFalse (s.Connected, "#B:Client:Connected");
87 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
89 Assert.IsNull (s.LocalEndPoint, "#B:Client:LocalEndPoint");
90 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
91 Assert.IsNull (s.RemoteEndPoint, "#B:Client:RemoteEndPoint");
92 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
93 Assert.IsFalse (client.Active, "#B:Active");
95 //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
96 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
97 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
98 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
99 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
104 [Test] // .ctor (AddressFamily)
105 public void Constructor2_Family_Invalid ()
108 new UdpClient (AddressFamily.NetBios);
110 } catch (ArgumentException ex) {
111 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
112 Assert.IsNull (ex.InnerException, "#A3");
114 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
116 Assert.IsNotNull (ex.Message, "#A4");
117 Assert.AreEqual ("family", ex.ParamName, "#A5");
119 Assert.AreEqual ("family", ex.Message, "#A4");
120 Assert.IsNull (ex.ParamName, "#A5");
125 new UdpClient ((AddressFamily) 666);
127 } catch (ArgumentException ex) {
128 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
129 Assert.IsNull (ex.InnerException, "#B3");
131 Assert.IsNotNull (ex.Message, "#B4");
132 Assert.AreEqual ("family", ex.ParamName, "#B5");
134 Assert.AreEqual ("family", ex.Message, "#B4");
135 Assert.IsNull (ex.ParamName, "#B5");
140 [Test] // .ctor (Int32)
141 public void Constructor3 ()
146 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort))
149 Assert.IsNotNull (s, "#A:Client");
150 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
151 Assert.IsFalse (s.Connected, "#A:Client:Connected");
153 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
155 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
156 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
157 Assert.IsFalse (client.Active, "#A:Active");
159 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
160 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
161 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
162 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
163 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
165 localEP = s.LocalEndPoint as IPEndPoint;
166 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
167 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
168 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
170 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort))
173 Assert.IsNotNull (s, "#B:Client");
174 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
175 Assert.IsFalse (s.Connected, "#B:Client:Connected");
177 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
179 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
180 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
181 Assert.IsFalse (client.Active, "#B:Active");
183 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
184 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
185 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
186 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
187 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
189 localEP = s.LocalEndPoint as IPEndPoint;
190 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
191 Assert.AreEqual (IPAddress.Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
192 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
193 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
197 [Test] // .ctor (Int32)
198 public void Constructor3_Port_OutOfRange ()
201 new UdpClient (IPEndPoint.MaxPort + 1);
203 } catch (ArgumentOutOfRangeException ex) {
204 // Specified argument was out of the range of valid values
205 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
206 Assert.IsNull (ex.InnerException, "#A3");
207 Assert.IsNotNull (ex.Message, "#A4");
208 Assert.AreEqual ("port", ex.ParamName, "#A5");
212 new UdpClient (IPEndPoint.MinPort - 1);
214 } catch (ArgumentOutOfRangeException ex) {
215 // Specified argument was out of the range of valid values
216 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
217 Assert.IsNull (ex.InnerException, "#A3");
218 Assert.IsNotNull (ex.Message, "#A4");
219 Assert.AreEqual ("port", ex.ParamName, "#A5");
223 [Test] // .ctor (IPEndPoint)
224 public void Constructor4 ()
230 clientEP = new IPEndPoint (IPAddress.Loopback, 8001);
231 using (MyUdpClient client = new MyUdpClient (clientEP))
234 Assert.IsNotNull (s, "#A:Client");
235 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
236 Assert.IsFalse (s.Connected, "#A:Client:Connected");
238 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
240 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
241 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
242 Assert.IsFalse (client.Active, "#A:Active");
244 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
245 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
246 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
247 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
248 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
250 localEP = s.LocalEndPoint as IPEndPoint;
251 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
252 Assert.IsFalse (object.ReferenceEquals (clientEP, localEP), "#A:Client:LocalEndPoint/ReferenceEquality");
253 Assert.AreEqual (clientEP.Address, localEP.Address, "#A:Client:LocalEndPoint/Address");
254 Assert.AreEqual (clientEP.AddressFamily, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
255 Assert.AreEqual (clientEP.Port, localEP.Port, "#A:Client:LocalEndPoint/Port");
259 [Test] // .ctor (IPEndPoint)
260 public void Constructor4_LocalEP_Null ()
263 new UdpClient ((IPEndPoint) null);
265 } catch (ArgumentNullException ex) {
266 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
267 Assert.IsNull (ex.InnerException, "#3");
268 Assert.IsNotNull (ex.Message, "#4");
269 Assert.AreEqual ("localEP", ex.ParamName, "#5");
273 [Test] // .ctor (Int32, AddressFamily)
274 public void Constructor5 ()
279 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort, AddressFamily.InterNetwork))
282 Assert.IsNotNull (s, "#A:Client");
283 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
284 Assert.IsFalse (s.Connected, "#A:Client:Connected");
286 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
288 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
289 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
290 Assert.IsFalse (client.Active, "#A:Active");
292 //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
293 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
294 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
295 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
296 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
298 localEP = s.LocalEndPoint as IPEndPoint;
299 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
300 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
301 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
305 if (!Socket.OSSupportsIPv6)
307 if (!Socket.SupportsIPv6)
309 Assert.Ignore ("IPv6 not enabled.");
311 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort, AddressFamily.InterNetworkV6))
314 Assert.IsNotNull (s, "#B:Client");
315 Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
316 Assert.IsFalse (s.Connected, "#B:Client:Connected");
318 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
320 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
321 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
322 Assert.IsFalse (client.Active, "#B:Active");
324 //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
325 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
326 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
327 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
328 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
330 localEP = s.LocalEndPoint as IPEndPoint;
331 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
332 Assert.AreEqual (IPAddress.IPv6Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
333 Assert.AreEqual (AddressFamily.InterNetworkV6, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
334 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
338 [Test] // .ctor (Int32, AddressFamily)
339 public void Constructor5_Family_Invalid ()
342 new UdpClient (80, AddressFamily.NetBios);
344 } catch (ArgumentException ex) {
346 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
347 Assert.IsNull (ex.InnerException, "#A3");
349 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
351 Assert.IsNotNull (ex.Message, "#A4");
352 Assert.AreEqual ("family", ex.ParamName, "#A5");
354 Assert.AreEqual ("family", ex.Message, "#A4");
355 Assert.IsNull (ex.ParamName, "#A5");
360 new UdpClient (80, (AddressFamily) 666);
362 } catch (ArgumentException ex) {
364 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
365 Assert.IsNull (ex.InnerException, "#B3");
367 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
369 Assert.IsNotNull (ex.Message, "#B4");
370 Assert.AreEqual ("family", ex.ParamName, "#B5");
372 Assert.AreEqual ("family", ex.Message, "#B4");
373 Assert.IsNull (ex.ParamName, "#B5");
378 [Test] // .ctor (Int32, AddressFamily)
379 public void Constructor5_Port_OutOfRange ()
382 new UdpClient (IPEndPoint.MaxPort + 1, AddressFamily.InterNetwork);
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");
393 new UdpClient (IPEndPoint.MinPort - 1, AddressFamily.InterNetwork);
395 } catch (ArgumentOutOfRangeException ex) {
396 // Specified argument was out of the range of valid values
397 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
398 Assert.IsNull (ex.InnerException, "#A3");
399 Assert.IsNotNull (ex.Message, "#A4");
400 Assert.AreEqual ("port", ex.ParamName, "#A5");
404 [Test] // .ctor (String, Int32)
405 public void Constructor6 ()
411 // UDP port 0 doesn't seem to be valid.
412 using (MyUdpClient client = new MyUdpClient ("127.0.0.1", 53))
415 Assert.IsNotNull (s, "#A:Client");
416 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
417 Assert.IsTrue (s.Connected, "#A:Client:Connected");
419 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
421 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
422 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
423 Assert.IsTrue (client.Active, "#A:Active");
425 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
426 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
427 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
428 //Assert.IsFalse (client.MulticastLoopback, "#A:MulticastLoopback");
429 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
431 localEP = s.LocalEndPoint as IPEndPoint;
432 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
433 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#A:Client:LocalEndPoint/Address");
434 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
436 using (MyUdpClient client = new MyUdpClient ("127.0.0.1", IPEndPoint.MaxPort))
439 Assert.IsNotNull (s, "#B:Client");
440 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
441 Assert.IsTrue (s.Connected, "#B:Client:Connected");
443 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
445 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
446 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
447 Assert.IsTrue (client.Active, "#B:Active");
449 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
450 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
451 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
452 //Assert.IsFalse (client.MulticastLoopback, "#B:MulticastLoopback");
453 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
455 localEP = s.LocalEndPoint as IPEndPoint;
456 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
457 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#B:Client:LocalEndPoint/Address");
458 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
462 [Test] // .ctor (String, Int32)
463 public void Constructor6_HostName_Null ()
466 new UdpClient ((string) null, int.MaxValue);
468 } catch (ArgumentNullException ex) {
469 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
470 Assert.IsNull (ex.InnerException, "#3");
471 Assert.IsNotNull (ex.Message, "#4");
472 Assert.AreEqual ("hostname", ex.ParamName, "#5");
476 [Test] // .ctor (String, Int32)
477 public void Constructor6_Port_OutOfRange ()
480 new UdpClient ("local", IPEndPoint.MaxPort + 1);
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");
491 new UdpClient ("local", IPEndPoint.MinPort - 1);
493 } catch (ArgumentOutOfRangeException ex) {
494 // Specified argument was out of the range of valid values
495 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
496 Assert.IsNull (ex.InnerException, "#A3");
497 Assert.IsNotNull (ex.Message, "#A4");
498 Assert.AreEqual ("port", ex.ParamName, "#A5");
503 public void UdpClientBroadcastTest ()
505 UdpClient client = new UdpClient ();
506 byte[] bytes = new byte[] {10, 11, 12, 13};
509 client.Send (bytes, bytes.Length, new IPEndPoint (IPAddress.Broadcast, 1235));
515 [Test] // JoinMulticastGroup (IPAddress)
516 public void JoinMulticastGroup1_IPv4 ()
518 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
520 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
521 client.JoinMulticastGroup (mcast_addr);
525 [Test] // JoinMulticastGroup (IPAddress)
526 public void JoinMulticastGroup1_IPv6 ()
529 if (!Socket.OSSupportsIPv6)
531 if (!Socket.SupportsIPv6)
533 Assert.Ignore ("IPv6 not enabled.");
535 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
537 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
538 client.JoinMulticastGroup (mcast_addr);
542 [Test] // JoinMulticastGroup (IPAddress)
543 public void JoinMulticastGroup1_MulticastAddr_Null ()
545 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
547 client.JoinMulticastGroup ((IPAddress) null);
550 } catch (ArgumentNullException ex) {
551 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
552 Assert.IsNull (ex.InnerException, "#3");
553 Assert.IsNotNull (ex.Message, "#4");
554 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
557 } catch (NullReferenceException ex) {
558 Assert.AreEqual (typeof (NullReferenceException), ex.GetType (), "#2");
559 Assert.IsNull (ex.InnerException, "#3");
560 Assert.IsNotNull (ex.Message, "#4");
566 [Test] // JoinMulticastGroup (IPAddress)
567 public void JoinMulticastGroup1_Socket_Closed ()
569 IPAddress mcast_addr = null;
571 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
574 client.JoinMulticastGroup (mcast_addr);
576 } catch (ObjectDisposedException ex) {
577 // Cannot access a disposed object
578 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
579 Assert.IsNull (ex.InnerException, "#3");
580 Assert.IsNotNull (ex.Message, "#4");
581 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
585 [Test] // JoinMulticastGroup (IPAddress)
586 [Category ("NotWorking")]
587 public void JoinMulticastGroup1_Socket_NotBound ()
589 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
591 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
593 client.JoinMulticastGroup (mcast_addr);
595 } catch (SocketException ex) {
596 // An invalid argument was supplied
597 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
598 Assert.AreEqual (10022, ex.ErrorCode, "#3");
599 Assert.IsNull (ex.InnerException, "#4");
600 Assert.IsNotNull (ex.Message, "#5");
601 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
603 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
610 [Test] // JoinMulticastGroup (In32, IPAddress)
611 public void JoinMulticastGroup2_IPv4 ()
613 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
615 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
617 client.JoinMulticastGroup (0, mcast_addr);
619 } catch (SocketException ex) {
620 // The attempted operation is not supported for the type of
622 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
623 Assert.AreEqual (10045, ex.ErrorCode, "#3");
624 Assert.IsNull (ex.InnerException, "#4");
625 Assert.IsNotNull (ex.Message, "#5");
626 Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
628 Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
634 [Test] // JoinMulticastGroup (In32, IPAddress)
635 public void JoinMulticastGroup2_IPv6 ()
638 if (!Socket.OSSupportsIPv6)
640 if (!Socket.SupportsIPv6)
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 (0, mcast_addr);
651 [Test] // JoinMulticastGroup (Int32, IPAddress)
652 public void JoinMulticastGroup2_MulticastAddr_Null ()
654 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
656 client.JoinMulticastGroup (0, (IPAddress) null);
658 } catch (ArgumentNullException ex) {
659 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
660 Assert.IsNull (ex.InnerException, "#3");
661 Assert.IsNotNull (ex.Message, "#4");
662 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
667 [Test] // JoinMulticastGroup (Int32, IPAddress)
668 public void JoinMulticastGroup2_Socket_Closed ()
671 if (!Socket.OSSupportsIPv6)
673 if (!Socket.SupportsIPv6)
675 Assert.Ignore ("IPv6 not enabled.");
677 IPAddress mcast_addr = null;
679 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234));
682 client.JoinMulticastGroup (0, mcast_addr);
684 } catch (ObjectDisposedException ex) {
685 // Cannot access a disposed object
686 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
687 Assert.IsNull (ex.InnerException, "#3");
688 Assert.IsNotNull (ex.Message, "#4");
689 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
693 [Test] // JoinMulticastGroup (Int32, IPAddress)
694 [Category ("NotWorking")]
695 public void JoinMulticastGroup2_Socket_NotBound ()
697 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
699 UdpClient client = new UdpClient (AddressFamily.InterNetworkV6);
701 client.JoinMulticastGroup (0, mcast_addr);
703 } catch (SocketException ex) {
704 // An invalid argument was supplied
705 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
706 Assert.AreEqual (10022, ex.ErrorCode, "#3");
707 Assert.IsNull (ex.InnerException, "#4");
708 Assert.IsNotNull (ex.Message, "#5");
709 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
711 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
718 [Test] // JoinMulticastGroup (IPAddress, Int32)
719 public void JoinMulticastGroup3_IPv4 ()
721 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
723 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
724 client.JoinMulticastGroup (mcast_addr, 0);
727 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
728 client.JoinMulticastGroup (mcast_addr, 255);
732 [Test] // JoinMulticastGroup (IPAddress, Int32)
733 public void JoinMulticastGroup3_IPv6 ()
736 if (!Socket.OSSupportsIPv6)
738 if (!Socket.SupportsIPv6)
740 Assert.Ignore ("IPv6 not enabled.");
742 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
744 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
745 client.JoinMulticastGroup (mcast_addr, 0);
748 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
749 client.JoinMulticastGroup (mcast_addr, 255);
753 [Test] // JoinMulticastGroup (IPAddress, Int32)
754 public void JoinMulticastGroup3_MulticastAddr_Null ()
756 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
758 client.JoinMulticastGroup ((IPAddress) null, int.MaxValue);
760 } catch (ArgumentNullException ex) {
761 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
762 Assert.IsNull (ex.InnerException, "#3");
763 Assert.IsNotNull (ex.Message, "#4");
764 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
769 [Test] // JoinMulticastGroup (IPAddress, Int32)
770 public void JoinMulticastGroup3_Socket_Closed ()
772 IPAddress mcast_addr = null;
774 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
777 client.JoinMulticastGroup (mcast_addr, 0);
779 } catch (ObjectDisposedException ex) {
780 // Cannot access a disposed object
781 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
782 Assert.IsNull (ex.InnerException, "#3");
783 Assert.IsNotNull (ex.Message, "#4");
784 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
788 [Test] // JoinMulticastGroup (IPAddress, Int32)
789 [Category ("NotWorking")]
790 public void JoinMulticastGroup3_Socket_NotBound ()
792 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
794 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
796 client.JoinMulticastGroup (mcast_addr, 5);
798 } catch (SocketException ex) {
799 // An invalid argument was supplied
800 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
801 Assert.AreEqual (10022, ex.ErrorCode, "#3");
802 Assert.IsNull (ex.InnerException, "#4");
803 Assert.IsNotNull (ex.Message, "#5");
804 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
806 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
814 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
815 public void JoinMulticastGroup4_IPv4 ()
817 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
818 IPAddress local_addr = IPAddress.Any;
820 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
821 client.JoinMulticastGroup (mcast_addr, local_addr);
825 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
826 public void JoinMulticastGroup4_IPv6 ()
828 if (!Socket.OSSupportsIPv6)
829 Assert.Ignore ("IPv6 not enabled.");
831 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
832 IPAddress local_addr = IPAddress.IPv6Any;
834 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
836 client.JoinMulticastGroup (mcast_addr, local_addr);
838 } catch (SocketException ex) {
839 // The attempted operation is not supported for the type of
841 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
842 Assert.AreEqual (10045, ex.ErrorCode, "#3");
843 Assert.IsNull (ex.InnerException, "#4");
844 Assert.IsNotNull (ex.Message, "#5");
845 Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
846 Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
851 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
852 public void JoinMulticastGroup4_LocalAddress_Null ()
854 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
856 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
858 client.JoinMulticastGroup (mcast_addr, (IPAddress) null);
860 } catch (ArgumentNullException ex) {
861 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
862 Assert.IsNull (ex.InnerException, "#3");
863 Assert.IsNotNull (ex.Message, "#4");
864 Assert.AreEqual ("mcint", ex.ParamName, "#5");
869 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
870 public void JoinMulticastGroup4_MulticastAddr_Null ()
872 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
874 client.JoinMulticastGroup ((IPAddress) null, IPAddress.Loopback);
876 } catch (ArgumentNullException ex) {
877 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
878 Assert.IsNull (ex.InnerException, "#3");
879 Assert.IsNotNull (ex.Message, "#4");
880 Assert.AreEqual ("group", ex.ParamName, "#5");
885 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
886 public void JoinMulticastGroup4_Socket_Closed ()
888 IPAddress mcast_addr = null;
889 IPAddress local_addr = null;
891 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
894 client.JoinMulticastGroup (mcast_addr, local_addr);
896 } catch (ObjectDisposedException ex) {
897 // Cannot access a disposed object
898 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
899 Assert.IsNull (ex.InnerException, "#3");
900 Assert.IsNotNull (ex.Message, "#4");
901 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
905 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
906 [Category ("NotWorking")]
907 public void JoinMulticastGroup4_Socket_NotBound ()
909 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
910 IPAddress local_addr = Dns.GetHostEntry (string.Empty).AddressList [0];
912 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
914 client.JoinMulticastGroup (mcast_addr, local_addr);
916 } catch (SocketException ex) {
917 // An invalid argument was supplied
918 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
919 Assert.AreEqual (10022, ex.ErrorCode, "#3");
920 Assert.IsNull (ex.InnerException, "#4");
921 Assert.IsNotNull (ex.Message, "#5");
922 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
923 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
930 public void CloseInReceive ()
932 UdpClient client = null;
933 var rnd = new Random ();
934 for (int i = 0; i < 5; i++) {
935 int port = rnd.Next (1025, 65534);
937 client = new UdpClient (port);
939 } catch (Exception ex) {
945 new Thread(delegate() {
950 bool got_exc = false;
951 IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
953 client.Receive(ref ep);
954 } catch (SocketException) {
959 Assert.IsTrue (got_exc);
962 // Test for bug 324033
964 public void JoinMulticastGroupWithLocal ()
966 UdpClient client = new UdpClient (9001);
967 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.24");
968 IPAddress local_addr = IPAddress.Any;
971 client.JoinMulticastGroup (mcast_addr, local_addr);
978 [ExpectedException (typeof(ArgumentNullException))]
979 public void BeginSendNull ()
981 UdpClient client = new UdpClient ();
982 client.BeginSend (null, 0, null, null);
986 static bool BSSent = false;
988 static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
990 private static void BSCallback (IAsyncResult asyncResult)
992 UdpClient client = (UdpClient)asyncResult.AsyncState;
994 BSBytes = client.EndSend (asyncResult);
1001 public void BeginSend ()
1003 UdpClient client = new UdpClient ();
1004 byte[] bytes = new byte[] {10, 11, 12, 13};
1007 client.BeginSend (bytes, bytes.Length, new AsyncCallback (BSCallback), client);
1008 Assert.Fail ("BeginSend #1");
1009 } catch (SocketException ex) {
1010 Assert.AreEqual (10057, ex.ErrorCode,
1015 client.BeginSend (bytes, bytes.Length, null, new AsyncCallback (BSCallback), client);
1016 Assert.Fail ("BeginSend #3");
1017 } catch (SocketException ex) {
1018 Assert.AreEqual (10057, ex.ErrorCode,
1022 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1236);
1024 BSCalledBack.Reset ();
1026 client.BeginSend (bytes, bytes.Length, ep,
1027 new AsyncCallback (BSCallback),
1029 if (BSCalledBack.WaitOne (2000, false) == false) {
1030 Assert.Fail ("BeginSend wait timed out");
1033 Assert.AreEqual (true, BSSent, "BeginSend #5");
1034 Assert.AreEqual (4, BSBytes, "BeginSend #6");
1039 static bool BRReceived = false;
1040 static byte[] BRBytes;
1041 static IPEndPoint BRFrom;
1042 static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
1044 private static void BRCallback (IAsyncResult asyncResult)
1046 UdpClient client = (UdpClient)asyncResult.AsyncState;
1048 BRBytes = client.EndReceive (asyncResult, ref BRFrom);
1051 BRCalledBack.Set ();
1055 public void BeginReceive ()
1057 UdpClient client = new UdpClient (1237);
1059 BRCalledBack.Reset ();
1061 client.BeginReceive (BRCallback, client);
1063 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1237);
1064 byte[] send_bytes = new byte[] {10, 11, 12, 13};
1065 client.Send (send_bytes, send_bytes.Length, ep);
1067 if (BRCalledBack.WaitOne (2000, false) == false) {
1068 Assert.Fail ("BeginReceive wait timed out");
1071 Assert.AreEqual (true, BRReceived, "BeginReceive #1");
1072 Assert.AreEqual (4, BRBytes.Length, "BeginReceive #2");
1073 Assert.AreEqual (ep. Port, BRFrom.Port, "BeginReceive #3");
1074 Assert.AreEqual (ep.Address, BRFrom.Address, "BeginReceive #4");
1080 public void Available ()
1082 using (UdpClient client = new UdpClient (1238)) {
1083 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1238);
1084 byte[] bytes = new byte[] {10, 11, 12, 13};
1086 int res = client.Send (bytes, bytes.Length, ep);
1087 Assert.AreEqual (bytes.Length, res, "Send");
1089 // that might happen too quickly, data sent and not yet received/available
1091 int avail = client.Available;
1093 Assert.AreEqual (bytes.Length, avail, "Available #1");
1100 [Category ("NotWorking")] // Using PMTU settings workaround on Linux, default true
1101 public void DontFragmentDefault ()
1103 UdpClient client = new UdpClient ();
1105 /* Ignore the docs, testing shows the default
1106 * here is in fact false
1108 Assert.AreEqual (false, client.DontFragment, "DontFragmentDefault");
1114 public void EnableBroadcastDefault ()
1116 UdpClient client = new UdpClient ();
1118 Assert.AreEqual (false, client.EnableBroadcast, "EnableBroadcastDefault");
1123 /* Can't test the default for ExclusiveAddressUse as
1124 * it's different on different versions and service
1128 [Category ("NotWorking")] // Not supported on Linux
1129 public void ExclusiveAddressUseUnbound ()
1131 UdpClient client = new UdpClient ();
1133 client.ExclusiveAddressUse = true;
1135 Assert.AreEqual (true, client.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
1141 [ExpectedException (typeof(InvalidOperationException))]
1142 [Category ("NotWorking")] // Not supported on Linux
1143 public void ExclusiveAddressUseBound ()
1145 UdpClient client = new UdpClient (1239);
1147 client.ExclusiveAddressUse = true;
1153 public void MulticastLoopbackDefault ()
1155 UdpClient client = new UdpClient ();
1157 Assert.AreEqual (true, client.MulticastLoopback, "MulticastLoopbackDefault");
1162 /* No test for Ttl default as it is platform dependent */
1165 class MyUdpClient : UdpClient
1167 public MyUdpClient ()
1171 public MyUdpClient (AddressFamily family)
1176 public MyUdpClient (Int32 port)
1182 public MyUdpClient (IPEndPoint localEP)
1187 public MyUdpClient (int port, AddressFamily family)
1188 : base (port, family)
1192 public MyUdpClient (string hostname, int port)
1193 : base (hostname, port)
1197 public new bool Active {
1198 get { return base.Active; }
1199 set { base.Active = value; }
1203 public new Socket Client {
1204 get { return base.Client; }
1205 set { base.Client = value; }