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");
77 client = new MyUdpClient (AddressFamily.InterNetworkV6);
79 Assert.IsNotNull (s, "#B:Client");
80 Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
81 Assert.IsFalse (s.Connected, "#B:Client:Connected");
83 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
85 Assert.IsNull (s.LocalEndPoint, "#B:Client:LocalEndPoint");
86 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
87 Assert.IsNull (s.RemoteEndPoint, "#B:Client:RemoteEndPoint");
88 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
89 Assert.IsFalse (client.Active, "#B:Active");
91 //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
92 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
93 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
94 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
95 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
100 [Test] // .ctor (AddressFamily)
101 public void Constructor2_Family_Invalid ()
104 new UdpClient (AddressFamily.NetBios);
106 } catch (ArgumentException ex) {
107 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
108 Assert.IsNull (ex.InnerException, "#A3");
110 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
112 Assert.IsNotNull (ex.Message, "#A4");
113 Assert.AreEqual ("family", ex.ParamName, "#A5");
115 Assert.AreEqual ("family", ex.Message, "#A4");
116 Assert.IsNull (ex.ParamName, "#A5");
121 new UdpClient ((AddressFamily) 666);
123 } catch (ArgumentException ex) {
124 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
125 Assert.IsNull (ex.InnerException, "#B3");
127 Assert.IsNotNull (ex.Message, "#B4");
128 Assert.AreEqual ("family", ex.ParamName, "#B5");
130 Assert.AreEqual ("family", ex.Message, "#B4");
131 Assert.IsNull (ex.ParamName, "#B5");
136 [Test] // .ctor (Int32)
137 public void Constructor3 ()
142 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort))
145 Assert.IsNotNull (s, "#A:Client");
146 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
147 Assert.IsFalse (s.Connected, "#A:Client:Connected");
149 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
151 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
152 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
153 Assert.IsFalse (client.Active, "#A:Active");
155 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
156 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
157 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
158 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
159 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
161 localEP = s.LocalEndPoint as IPEndPoint;
162 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
163 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
164 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
166 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort))
169 Assert.IsNotNull (s, "#B:Client");
170 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
171 Assert.IsFalse (s.Connected, "#B:Client:Connected");
173 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
175 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
176 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
177 Assert.IsFalse (client.Active, "#B:Active");
179 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
180 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
181 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
182 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
183 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
185 localEP = s.LocalEndPoint as IPEndPoint;
186 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
187 Assert.AreEqual (IPAddress.Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
188 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
189 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
193 [Test] // .ctor (Int32)
194 public void Constructor3_Port_OutOfRange ()
197 new UdpClient (IPEndPoint.MaxPort + 1);
199 } catch (ArgumentOutOfRangeException ex) {
200 // Specified argument was out of the range of valid values
201 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
202 Assert.IsNull (ex.InnerException, "#A3");
203 Assert.IsNotNull (ex.Message, "#A4");
204 Assert.AreEqual ("port", ex.ParamName, "#A5");
208 new UdpClient (IPEndPoint.MinPort - 1);
210 } catch (ArgumentOutOfRangeException ex) {
211 // Specified argument was out of the range of valid values
212 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
213 Assert.IsNull (ex.InnerException, "#A3");
214 Assert.IsNotNull (ex.Message, "#A4");
215 Assert.AreEqual ("port", ex.ParamName, "#A5");
219 [Test] // .ctor (IPEndPoint)
220 public void Constructor4 ()
226 clientEP = new IPEndPoint (IPAddress.Loopback, 8001);
227 using (MyUdpClient client = new MyUdpClient (clientEP))
230 Assert.IsNotNull (s, "#A:Client");
231 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
232 Assert.IsFalse (s.Connected, "#A:Client:Connected");
234 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
236 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
237 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
238 Assert.IsFalse (client.Active, "#A:Active");
240 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
241 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
242 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
243 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
244 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
246 localEP = s.LocalEndPoint as IPEndPoint;
247 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
248 Assert.IsFalse (object.ReferenceEquals (clientEP, localEP), "#A:Client:LocalEndPoint/ReferenceEquality");
249 Assert.AreEqual (clientEP.Address, localEP.Address, "#A:Client:LocalEndPoint/Address");
250 Assert.AreEqual (clientEP.AddressFamily, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
251 Assert.AreEqual (clientEP.Port, localEP.Port, "#A:Client:LocalEndPoint/Port");
255 [Test] // .ctor (IPEndPoint)
256 public void Constructor4_LocalEP_Null ()
259 new UdpClient ((IPEndPoint) null);
261 } catch (ArgumentNullException ex) {
262 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
263 Assert.IsNull (ex.InnerException, "#3");
264 Assert.IsNotNull (ex.Message, "#4");
265 Assert.AreEqual ("localEP", ex.ParamName, "#5");
269 [Test] // .ctor (Int32, AddressFamily)
270 public void Constructor5 ()
275 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort, AddressFamily.InterNetwork))
278 Assert.IsNotNull (s, "#A:Client");
279 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
280 Assert.IsFalse (s.Connected, "#A:Client:Connected");
282 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
284 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
285 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
286 Assert.IsFalse (client.Active, "#A:Active");
288 //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
289 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
290 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
291 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
292 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
294 localEP = s.LocalEndPoint as IPEndPoint;
295 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
296 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
297 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
300 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort, AddressFamily.InterNetworkV6))
303 Assert.IsNotNull (s, "#B:Client");
304 Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
305 Assert.IsFalse (s.Connected, "#B:Client:Connected");
307 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
309 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
310 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
311 Assert.IsFalse (client.Active, "#B:Active");
313 //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
314 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
315 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
316 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
317 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
319 localEP = s.LocalEndPoint as IPEndPoint;
320 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
321 Assert.AreEqual (IPAddress.IPv6Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
322 Assert.AreEqual (AddressFamily.InterNetworkV6, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
323 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
327 [Test] // .ctor (Int32, AddressFamily)
328 public void Constructor5_Family_Invalid ()
331 new UdpClient (80, AddressFamily.NetBios);
333 } catch (ArgumentException ex) {
335 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
336 Assert.IsNull (ex.InnerException, "#A3");
338 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
340 Assert.IsNotNull (ex.Message, "#A4");
341 Assert.AreEqual ("family", ex.ParamName, "#A5");
343 Assert.AreEqual ("family", ex.Message, "#A4");
344 Assert.IsNull (ex.ParamName, "#A5");
349 new UdpClient (80, (AddressFamily) 666);
351 } catch (ArgumentException ex) {
353 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
354 Assert.IsNull (ex.InnerException, "#B3");
356 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
358 Assert.IsNotNull (ex.Message, "#B4");
359 Assert.AreEqual ("family", ex.ParamName, "#B5");
361 Assert.AreEqual ("family", ex.Message, "#B4");
362 Assert.IsNull (ex.ParamName, "#B5");
367 [Test] // .ctor (Int32, AddressFamily)
368 public void Constructor5_Port_OutOfRange ()
371 new UdpClient (IPEndPoint.MaxPort + 1, AddressFamily.InterNetwork);
373 } catch (ArgumentOutOfRangeException ex) {
374 // Specified argument was out of the range of valid values
375 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
376 Assert.IsNull (ex.InnerException, "#A3");
377 Assert.IsNotNull (ex.Message, "#A4");
378 Assert.AreEqual ("port", ex.ParamName, "#A5");
382 new UdpClient (IPEndPoint.MinPort - 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 [Test] // .ctor (String, Int32)
394 public void Constructor6 ()
400 // UDP port 0 doesn't seem to be valid.
401 using (MyUdpClient client = new MyUdpClient ("127.0.0.1", 53))
404 Assert.IsNotNull (s, "#A:Client");
405 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
406 Assert.IsTrue (s.Connected, "#A:Client:Connected");
408 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
410 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
411 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
412 Assert.IsTrue (client.Active, "#A:Active");
414 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
415 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
416 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
417 //Assert.IsFalse (client.MulticastLoopback, "#A:MulticastLoopback");
418 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
420 localEP = s.LocalEndPoint as IPEndPoint;
421 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
422 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#A:Client:LocalEndPoint/Address");
423 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
425 using (MyUdpClient client = new MyUdpClient ("127.0.0.1", IPEndPoint.MaxPort))
428 Assert.IsNotNull (s, "#B:Client");
429 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
430 Assert.IsTrue (s.Connected, "#B:Client:Connected");
432 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
434 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
435 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
436 Assert.IsTrue (client.Active, "#B:Active");
438 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
439 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
440 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
441 //Assert.IsFalse (client.MulticastLoopback, "#B:MulticastLoopback");
442 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
444 localEP = s.LocalEndPoint as IPEndPoint;
445 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
446 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#B:Client:LocalEndPoint/Address");
447 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
451 [Test] // .ctor (String, Int32)
452 public void Constructor6_HostName_Null ()
455 new UdpClient ((string) null, int.MaxValue);
457 } catch (ArgumentNullException ex) {
458 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
459 Assert.IsNull (ex.InnerException, "#3");
460 Assert.IsNotNull (ex.Message, "#4");
461 Assert.AreEqual ("hostname", ex.ParamName, "#5");
465 [Test] // .ctor (String, Int32)
466 public void Constructor6_Port_OutOfRange ()
469 new UdpClient ("local", IPEndPoint.MaxPort + 1);
471 } catch (ArgumentOutOfRangeException ex) {
472 // Specified argument was out of the range of valid values
473 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
474 Assert.IsNull (ex.InnerException, "#A3");
475 Assert.IsNotNull (ex.Message, "#A4");
476 Assert.AreEqual ("port", ex.ParamName, "#A5");
480 new UdpClient ("local", IPEndPoint.MinPort - 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");
492 public void UdpClientBroadcastTest ()
494 UdpClient client = new UdpClient ();
495 byte[] bytes = new byte[] {10, 11, 12, 13};
498 client.Send (bytes, bytes.Length, new IPEndPoint (IPAddress.Broadcast, 1235));
504 [Test] // JoinMulticastGroup (IPAddress)
505 public void JoinMulticastGroup1_IPv4 ()
507 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
509 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
510 client.JoinMulticastGroup (mcast_addr);
514 [Test] // JoinMulticastGroup (IPAddress)
515 public void JoinMulticastGroup1_IPv6 ()
518 if (!Socket.OSSupportsIPv6)
520 if (!Socket.SupportsIPv6)
522 Assert.Ignore ("IPv6 not enabled.");
524 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
526 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
527 client.JoinMulticastGroup (mcast_addr);
531 [Test] // JoinMulticastGroup (IPAddress)
532 public void JoinMulticastGroup1_MulticastAddr_Null ()
534 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
536 client.JoinMulticastGroup ((IPAddress) null);
539 } catch (ArgumentNullException ex) {
540 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
541 Assert.IsNull (ex.InnerException, "#3");
542 Assert.IsNotNull (ex.Message, "#4");
543 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
546 } catch (NullReferenceException ex) {
547 Assert.AreEqual (typeof (NullReferenceException), ex.GetType (), "#2");
548 Assert.IsNull (ex.InnerException, "#3");
549 Assert.IsNotNull (ex.Message, "#4");
555 [Test] // JoinMulticastGroup (IPAddress)
556 public void JoinMulticastGroup1_Socket_Closed ()
558 IPAddress mcast_addr = null;
560 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
563 client.JoinMulticastGroup (mcast_addr);
565 } catch (ObjectDisposedException ex) {
566 // Cannot access a disposed object
567 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
568 Assert.IsNull (ex.InnerException, "#3");
569 Assert.IsNotNull (ex.Message, "#4");
570 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
574 [Test] // JoinMulticastGroup (IPAddress)
575 [Category ("NotWorking")]
576 public void JoinMulticastGroup1_Socket_NotBound ()
578 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
580 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
582 client.JoinMulticastGroup (mcast_addr);
584 } catch (SocketException ex) {
585 // An invalid argument was supplied
586 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
587 Assert.AreEqual (10022, ex.ErrorCode, "#3");
588 Assert.IsNull (ex.InnerException, "#4");
589 Assert.IsNotNull (ex.Message, "#5");
590 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
592 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
599 [Test] // JoinMulticastGroup (In32, IPAddress)
600 public void JoinMulticastGroup2_IPv4 ()
602 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
604 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
606 client.JoinMulticastGroup (0, mcast_addr);
608 } catch (SocketException ex) {
609 // The attempted operation is not supported for the type of
611 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
612 Assert.AreEqual (10045, ex.ErrorCode, "#3");
613 Assert.IsNull (ex.InnerException, "#4");
614 Assert.IsNotNull (ex.Message, "#5");
615 Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
617 Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
623 [Test] // JoinMulticastGroup (In32, IPAddress)
624 public void JoinMulticastGroup2_IPv6 ()
627 if (!Socket.OSSupportsIPv6)
629 if (!Socket.SupportsIPv6)
631 Assert.Ignore ("IPv6 not enabled.");
633 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
635 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
636 client.JoinMulticastGroup (0, mcast_addr);
640 [Test] // JoinMulticastGroup (Int32, IPAddress)
641 public void JoinMulticastGroup2_MulticastAddr_Null ()
643 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
645 client.JoinMulticastGroup (0, (IPAddress) null);
647 } catch (ArgumentNullException ex) {
648 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
649 Assert.IsNull (ex.InnerException, "#3");
650 Assert.IsNotNull (ex.Message, "#4");
651 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
656 [Test] // JoinMulticastGroup (Int32, IPAddress)
657 public void JoinMulticastGroup2_Socket_Closed ()
659 IPAddress mcast_addr = null;
661 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234));
664 client.JoinMulticastGroup (0, mcast_addr);
666 } catch (ObjectDisposedException ex) {
667 // Cannot access a disposed object
668 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
669 Assert.IsNull (ex.InnerException, "#3");
670 Assert.IsNotNull (ex.Message, "#4");
671 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
675 [Test] // JoinMulticastGroup (Int32, IPAddress)
676 [Category ("NotWorking")]
677 public void JoinMulticastGroup2_Socket_NotBound ()
679 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
681 UdpClient client = new UdpClient (AddressFamily.InterNetworkV6);
683 client.JoinMulticastGroup (0, mcast_addr);
685 } catch (SocketException ex) {
686 // An invalid argument was supplied
687 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
688 Assert.AreEqual (10022, ex.ErrorCode, "#3");
689 Assert.IsNull (ex.InnerException, "#4");
690 Assert.IsNotNull (ex.Message, "#5");
691 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
693 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
700 [Test] // JoinMulticastGroup (IPAddress, Int32)
701 public void JoinMulticastGroup3_IPv4 ()
703 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
705 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
706 client.JoinMulticastGroup (mcast_addr, 0);
709 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
710 client.JoinMulticastGroup (mcast_addr, 255);
714 [Test] // JoinMulticastGroup (IPAddress, Int32)
715 public void JoinMulticastGroup3_IPv6 ()
718 if (!Socket.OSSupportsIPv6)
720 if (!Socket.SupportsIPv6)
722 Assert.Ignore ("IPv6 not enabled.");
724 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
726 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
727 client.JoinMulticastGroup (mcast_addr, 0);
730 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
731 client.JoinMulticastGroup (mcast_addr, 255);
735 [Test] // JoinMulticastGroup (IPAddress, Int32)
736 public void JoinMulticastGroup3_MulticastAddr_Null ()
738 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
740 client.JoinMulticastGroup ((IPAddress) null, int.MaxValue);
742 } catch (ArgumentNullException ex) {
743 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
744 Assert.IsNull (ex.InnerException, "#3");
745 Assert.IsNotNull (ex.Message, "#4");
746 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
751 [Test] // JoinMulticastGroup (IPAddress, Int32)
752 public void JoinMulticastGroup3_Socket_Closed ()
754 IPAddress mcast_addr = null;
756 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
759 client.JoinMulticastGroup (mcast_addr, 0);
761 } catch (ObjectDisposedException ex) {
762 // Cannot access a disposed object
763 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
764 Assert.IsNull (ex.InnerException, "#3");
765 Assert.IsNotNull (ex.Message, "#4");
766 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
770 [Test] // JoinMulticastGroup (IPAddress, Int32)
771 [Category ("NotWorking")]
772 public void JoinMulticastGroup3_Socket_NotBound ()
774 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
776 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
778 client.JoinMulticastGroup (mcast_addr, 5);
780 } catch (SocketException ex) {
781 // An invalid argument was supplied
782 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
783 Assert.AreEqual (10022, ex.ErrorCode, "#3");
784 Assert.IsNull (ex.InnerException, "#4");
785 Assert.IsNotNull (ex.Message, "#5");
786 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
788 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
796 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
797 public void JoinMulticastGroup4_IPv4 ()
799 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
800 IPAddress local_addr = IPAddress.Any;
802 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
803 client.JoinMulticastGroup (mcast_addr, local_addr);
807 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
808 public void JoinMulticastGroup4_IPv6 ()
810 if (!Socket.OSSupportsIPv6)
811 Assert.Ignore ("IPv6 not enabled.");
813 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
814 IPAddress local_addr = IPAddress.IPv6Any;
816 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
818 client.JoinMulticastGroup (mcast_addr, local_addr);
820 } catch (SocketException ex) {
821 // The attempted operation is not supported for the type of
823 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
824 Assert.AreEqual (10045, ex.ErrorCode, "#3");
825 Assert.IsNull (ex.InnerException, "#4");
826 Assert.IsNotNull (ex.Message, "#5");
827 Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
828 Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
833 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
834 public void JoinMulticastGroup4_LocalAddress_Null ()
836 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
838 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
840 client.JoinMulticastGroup (mcast_addr, (IPAddress) null);
842 } catch (ArgumentNullException ex) {
843 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
844 Assert.IsNull (ex.InnerException, "#3");
845 Assert.IsNotNull (ex.Message, "#4");
846 Assert.AreEqual ("mcint", ex.ParamName, "#5");
851 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
852 public void JoinMulticastGroup4_MulticastAddr_Null ()
854 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
856 client.JoinMulticastGroup ((IPAddress) null, IPAddress.Loopback);
858 } catch (ArgumentNullException ex) {
859 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
860 Assert.IsNull (ex.InnerException, "#3");
861 Assert.IsNotNull (ex.Message, "#4");
862 Assert.AreEqual ("group", ex.ParamName, "#5");
867 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
868 public void JoinMulticastGroup4_Socket_Closed ()
870 IPAddress mcast_addr = null;
871 IPAddress local_addr = null;
873 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
876 client.JoinMulticastGroup (mcast_addr, local_addr);
878 } catch (ObjectDisposedException ex) {
879 // Cannot access a disposed object
880 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
881 Assert.IsNull (ex.InnerException, "#3");
882 Assert.IsNotNull (ex.Message, "#4");
883 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
887 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
888 [Category ("NotWorking")]
889 public void JoinMulticastGroup4_Socket_NotBound ()
891 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
892 IPAddress local_addr = Dns.GetHostEntry (string.Empty).AddressList [0];
894 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
896 client.JoinMulticastGroup (mcast_addr, local_addr);
898 } catch (SocketException ex) {
899 // An invalid argument was supplied
900 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
901 Assert.AreEqual (10022, ex.ErrorCode, "#3");
902 Assert.IsNull (ex.InnerException, "#4");
903 Assert.IsNotNull (ex.Message, "#5");
904 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
905 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
912 public void CloseInReceive ()
914 UdpClient client = null;
915 var rnd = new Random ();
916 for (int i = 0; i < 5; i++) {
917 int port = rnd.Next (1025, 65534);
919 client = new UdpClient (port);
921 } catch (Exception ex) {
927 new Thread(delegate() {
932 bool got_exc = false;
933 IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
935 client.Receive(ref ep);
936 } catch (SocketException) {
941 Assert.IsTrue (got_exc);
944 // Test for bug 324033
946 public void JoinMulticastGroupWithLocal ()
948 UdpClient client = new UdpClient (9001);
949 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.24");
950 IPAddress local_addr = IPAddress.Any;
953 client.JoinMulticastGroup (mcast_addr, local_addr);
960 [ExpectedException (typeof(ArgumentNullException))]
961 public void BeginSendNull ()
963 UdpClient client = new UdpClient ();
964 client.BeginSend (null, 0, null, null);
968 static bool BSSent = false;
970 static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
972 private static void BSCallback (IAsyncResult asyncResult)
974 UdpClient client = (UdpClient)asyncResult.AsyncState;
976 BSBytes = client.EndSend (asyncResult);
983 public void BeginSend ()
985 UdpClient client = new UdpClient ();
986 byte[] bytes = new byte[] {10, 11, 12, 13};
989 client.BeginSend (bytes, bytes.Length, new AsyncCallback (BSCallback), client);
990 Assert.Fail ("BeginSend #1");
991 } catch (SocketException ex) {
992 Assert.AreEqual (10057, ex.ErrorCode,
997 client.BeginSend (bytes, bytes.Length, null, new AsyncCallback (BSCallback), client);
998 Assert.Fail ("BeginSend #3");
999 } catch (SocketException ex) {
1000 Assert.AreEqual (10057, ex.ErrorCode,
1004 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1236);
1006 BSCalledBack.Reset ();
1008 client.BeginSend (bytes, bytes.Length, ep,
1009 new AsyncCallback (BSCallback),
1011 if (BSCalledBack.WaitOne (2000, false) == false) {
1012 Assert.Fail ("BeginSend wait timed out");
1015 Assert.AreEqual (true, BSSent, "BeginSend #5");
1016 Assert.AreEqual (4, BSBytes, "BeginSend #6");
1021 static bool BRReceived = false;
1022 static byte[] BRBytes;
1023 static IPEndPoint BRFrom;
1024 static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
1026 private static void BRCallback (IAsyncResult asyncResult)
1028 UdpClient client = (UdpClient)asyncResult.AsyncState;
1030 BRBytes = client.EndReceive (asyncResult, ref BRFrom);
1033 BRCalledBack.Set ();
1037 public void BeginReceive ()
1039 UdpClient client = new UdpClient (1237);
1041 BRCalledBack.Reset ();
1043 client.BeginReceive (BRCallback, client);
1045 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1237);
1046 byte[] send_bytes = new byte[] {10, 11, 12, 13};
1047 client.Send (send_bytes, send_bytes.Length, ep);
1049 if (BRCalledBack.WaitOne (2000, false) == false) {
1050 Assert.Fail ("BeginReceive wait timed out");
1053 Assert.AreEqual (true, BRReceived, "BeginReceive #1");
1054 Assert.AreEqual (4, BRBytes.Length, "BeginReceive #2");
1055 Assert.AreEqual (ep. Port, BRFrom.Port, "BeginReceive #3");
1056 Assert.AreEqual (ep.Address, BRFrom.Address, "BeginReceive #4");
1062 public void Available ()
1064 using (UdpClient client = new UdpClient (1238)) {
1065 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1238);
1066 byte[] bytes = new byte[] {10, 11, 12, 13};
1068 int res = client.Send (bytes, bytes.Length, ep);
1069 Assert.AreEqual (bytes.Length, res, "Send");
1071 // that might happen too quickly, data sent and not yet received/available
1073 int avail = client.Available;
1075 Assert.AreEqual (bytes.Length, avail, "Available #1");
1082 [Category ("NotWorking")] // Using PMTU settings workaround on Linux, default true
1083 public void DontFragmentDefault ()
1085 UdpClient client = new UdpClient ();
1087 /* Ignore the docs, testing shows the default
1088 * here is in fact false
1090 Assert.AreEqual (false, client.DontFragment, "DontFragmentDefault");
1096 public void EnableBroadcastDefault ()
1098 UdpClient client = new UdpClient ();
1100 Assert.AreEqual (false, client.EnableBroadcast, "EnableBroadcastDefault");
1105 /* Can't test the default for ExclusiveAddressUse as
1106 * it's different on different versions and service
1110 [Category ("NotWorking")] // Not supported on Linux
1111 public void ExclusiveAddressUseUnbound ()
1113 UdpClient client = new UdpClient ();
1115 client.ExclusiveAddressUse = true;
1117 Assert.AreEqual (true, client.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
1123 [ExpectedException (typeof(InvalidOperationException))]
1124 [Category ("NotWorking")] // Not supported on Linux
1125 public void ExclusiveAddressUseBound ()
1127 UdpClient client = new UdpClient (1239);
1129 client.ExclusiveAddressUse = true;
1135 public void MulticastLoopbackDefault ()
1137 UdpClient client = new UdpClient ();
1139 Assert.AreEqual (true, client.MulticastLoopback, "MulticastLoopbackDefault");
1144 /* No test for Ttl default as it is platform dependent */
1147 class MyUdpClient : UdpClient
1149 public MyUdpClient ()
1153 public MyUdpClient (AddressFamily family)
1158 public MyUdpClient (Int32 port)
1164 public MyUdpClient (IPEndPoint localEP)
1169 public MyUdpClient (int port, AddressFamily family)
1170 : base (port, family)
1174 public MyUdpClient (string hostname, int port)
1175 : base (hostname, port)
1179 public new bool Active {
1180 get { return base.Active; }
1181 set { base.Active = value; }
1185 public new Socket Client {
1186 get { return base.Client; }
1187 set { base.Client = value; }