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");
49 [Test] // .ctor (AddressFamily)
50 public void Constructor2 ()
55 client = new MyUdpClient (AddressFamily.InterNetwork);
57 Assert.IsNotNull (s, "#A:Client");
58 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
59 Assert.IsFalse (s.Connected, "#A:Client:Connected");
61 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
63 Assert.IsNull (s.LocalEndPoint, "#A:Client:LocalEndPoint");
64 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
65 Assert.IsNull (s.RemoteEndPoint, "#A:Client:RemoteEndPoint");
66 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
67 Assert.IsFalse (client.Active, "#A:Active");
69 //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
70 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
71 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
72 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
73 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
76 client = new MyUdpClient (AddressFamily.InterNetworkV6);
78 Assert.IsNotNull (s, "#B:Client");
79 Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
80 Assert.IsFalse (s.Connected, "#B:Client:Connected");
82 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
84 Assert.IsNull (s.LocalEndPoint, "#B:Client:LocalEndPoint");
85 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
86 Assert.IsNull (s.RemoteEndPoint, "#B:Client:RemoteEndPoint");
87 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
88 Assert.IsFalse (client.Active, "#B:Active");
90 //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
91 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
92 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
93 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
94 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
98 [Test] // .ctor (AddressFamily)
99 public void Constructor2_Family_Invalid ()
102 new UdpClient (AddressFamily.NetBios);
104 } catch (ArgumentException ex) {
105 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
106 Assert.IsNull (ex.InnerException, "#A3");
108 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
110 Assert.IsNotNull (ex.Message, "#A4");
111 Assert.AreEqual ("family", ex.ParamName, "#A5");
113 Assert.AreEqual ("family", ex.Message, "#A4");
114 Assert.IsNull (ex.ParamName, "#A5");
119 new UdpClient ((AddressFamily) 666);
121 } catch (ArgumentException ex) {
122 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
123 Assert.IsNull (ex.InnerException, "#B3");
125 Assert.IsNotNull (ex.Message, "#B4");
126 Assert.AreEqual ("family", ex.ParamName, "#B5");
128 Assert.AreEqual ("family", ex.Message, "#B4");
129 Assert.IsNull (ex.ParamName, "#B5");
134 [Test] // .ctor (Int32)
135 public void Constructor3 ()
141 client = new MyUdpClient (IPEndPoint.MinPort);
143 Assert.IsNotNull (s, "#A:Client");
144 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
145 Assert.IsFalse (s.Connected, "#A:Client:Connected");
147 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
149 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
150 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
151 Assert.IsFalse (client.Active, "#A:Active");
153 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
154 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
155 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
156 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
157 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
159 localEP = s.LocalEndPoint as IPEndPoint;
160 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
161 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
162 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
164 client = new MyUdpClient (IPEndPoint.MaxPort);
166 Assert.IsNotNull (s, "#B:Client");
167 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
168 Assert.IsFalse (s.Connected, "#B:Client:Connected");
170 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
172 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
173 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
174 Assert.IsFalse (client.Active, "#B:Active");
176 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
177 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
178 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
179 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
180 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
182 localEP = s.LocalEndPoint as IPEndPoint;
183 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
184 Assert.AreEqual (IPAddress.Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
185 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
186 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
189 [Test] // .ctor (Int32)
190 public void Constructor3_Port_OutOfRange ()
193 new UdpClient (IPEndPoint.MaxPort + 1);
195 } catch (ArgumentOutOfRangeException ex) {
196 // Specified argument was out of the range of valid values
197 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
198 Assert.IsNull (ex.InnerException, "#A3");
199 Assert.IsNotNull (ex.Message, "#A4");
200 Assert.AreEqual ("port", ex.ParamName, "#A5");
204 new UdpClient (IPEndPoint.MinPort - 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 [Test] // .ctor (IPEndPoint)
216 public void Constructor4 ()
223 clientEP = new IPEndPoint (IPAddress.Loopback, 8001);
224 client = new MyUdpClient (clientEP);
226 Assert.IsNotNull (s, "#A:Client");
227 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
228 Assert.IsFalse (s.Connected, "#A:Client:Connected");
230 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
232 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
233 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
234 Assert.IsFalse (client.Active, "#A:Active");
236 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
237 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
238 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
239 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
240 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
242 localEP = s.LocalEndPoint as IPEndPoint;
243 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
244 Assert.IsFalse (object.ReferenceEquals (clientEP, localEP), "#A:Client:LocalEndPoint/ReferenceEquality");
245 Assert.AreEqual (clientEP.Address, localEP.Address, "#A:Client:LocalEndPoint/Address");
246 Assert.AreEqual (clientEP.AddressFamily, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
247 Assert.AreEqual (clientEP.Port, localEP.Port, "#A:Client:LocalEndPoint/Port");
250 [Test] // .ctor (IPEndPoint)
251 public void Constructor4_LocalEP_Null ()
254 new UdpClient ((IPEndPoint) null);
256 } catch (ArgumentNullException ex) {
257 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
258 Assert.IsNull (ex.InnerException, "#3");
259 Assert.IsNotNull (ex.Message, "#4");
260 Assert.AreEqual ("localEP", ex.ParamName, "#5");
264 [Test] // .ctor (Int32, AddressFamily)
265 public void Constructor5 ()
271 client = new MyUdpClient (IPEndPoint.MinPort, AddressFamily.InterNetwork);
273 Assert.IsNotNull (s, "#A:Client");
274 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
275 Assert.IsFalse (s.Connected, "#A:Client:Connected");
277 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
279 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
280 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
281 Assert.IsFalse (client.Active, "#A:Active");
283 //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
284 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
285 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
286 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
287 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
289 localEP = s.LocalEndPoint as IPEndPoint;
290 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
291 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
292 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
294 client = new MyUdpClient (IPEndPoint.MaxPort, AddressFamily.InterNetworkV6);
296 Assert.IsNotNull (s, "#B:Client");
297 Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
298 Assert.IsFalse (s.Connected, "#B:Client:Connected");
300 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
302 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
303 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
304 Assert.IsFalse (client.Active, "#B:Active");
306 //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
307 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
308 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
309 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
310 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
312 localEP = s.LocalEndPoint as IPEndPoint;
313 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
314 Assert.AreEqual (IPAddress.IPv6Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
315 Assert.AreEqual (AddressFamily.InterNetworkV6, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
316 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
319 [Test] // .ctor (Int32, AddressFamily)
320 public void Constructor5_Family_Invalid ()
323 new UdpClient (80, AddressFamily.NetBios);
325 } catch (ArgumentException ex) {
327 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
328 Assert.IsNull (ex.InnerException, "#A3");
330 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
332 Assert.IsNotNull (ex.Message, "#A4");
333 Assert.AreEqual ("family", ex.ParamName, "#A5");
335 Assert.AreEqual ("family", ex.Message, "#A4");
336 Assert.IsNull (ex.ParamName, "#A5");
341 new UdpClient (80, (AddressFamily) 666);
343 } catch (ArgumentException ex) {
345 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
346 Assert.IsNull (ex.InnerException, "#B3");
348 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
350 Assert.IsNotNull (ex.Message, "#B4");
351 Assert.AreEqual ("family", ex.ParamName, "#B5");
353 Assert.AreEqual ("family", ex.Message, "#B4");
354 Assert.IsNull (ex.ParamName, "#B5");
359 [Test] // .ctor (Int32, AddressFamily)
360 public void Constructor5_Port_OutOfRange ()
363 new UdpClient (IPEndPoint.MaxPort + 1, AddressFamily.InterNetwork);
365 } catch (ArgumentOutOfRangeException ex) {
366 // Specified argument was out of the range of valid values
367 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
368 Assert.IsNull (ex.InnerException, "#A3");
369 Assert.IsNotNull (ex.Message, "#A4");
370 Assert.AreEqual ("port", ex.ParamName, "#A5");
374 new UdpClient (IPEndPoint.MinPort - 1, AddressFamily.InterNetwork);
376 } catch (ArgumentOutOfRangeException ex) {
377 // Specified argument was out of the range of valid values
378 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
379 Assert.IsNull (ex.InnerException, "#A3");
380 Assert.IsNotNull (ex.Message, "#A4");
381 Assert.AreEqual ("port", ex.ParamName, "#A5");
385 [Test] // .ctor (String, Int32)
386 public void Constructor6 ()
393 // UDP port 0 doesn't seem to be valid.
394 client = new MyUdpClient ("127.0.0.1", 53);
396 Assert.IsNotNull (s, "#A:Client");
397 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
398 Assert.IsTrue (s.Connected, "#A:Client:Connected");
400 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
402 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
403 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
404 Assert.IsTrue (client.Active, "#A:Active");
406 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
407 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
408 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
409 //Assert.IsFalse (client.MulticastLoopback, "#A:MulticastLoopback");
410 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
412 localEP = s.LocalEndPoint as IPEndPoint;
413 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
414 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#A:Client:LocalEndPoint/Address");
415 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
417 client = new MyUdpClient ("127.0.0.1", IPEndPoint.MaxPort);
419 Assert.IsNotNull (s, "#B:Client");
420 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
421 Assert.IsTrue (s.Connected, "#B:Client:Connected");
423 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
425 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
426 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
427 Assert.IsTrue (client.Active, "#B:Active");
429 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
430 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
431 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
432 //Assert.IsFalse (client.MulticastLoopback, "#B:MulticastLoopback");
433 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
435 localEP = s.LocalEndPoint as IPEndPoint;
436 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
437 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#B:Client:LocalEndPoint/Address");
438 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
441 [Test] // .ctor (String, Int32)
442 public void Constructor6_HostName_Null ()
445 new UdpClient ((string) null, int.MaxValue);
447 } catch (ArgumentNullException ex) {
448 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
449 Assert.IsNull (ex.InnerException, "#3");
450 Assert.IsNotNull (ex.Message, "#4");
451 Assert.AreEqual ("hostname", ex.ParamName, "#5");
455 [Test] // .ctor (String, Int32)
456 public void Constructor6_Port_OutOfRange ()
459 new UdpClient ("local", IPEndPoint.MaxPort + 1);
461 } catch (ArgumentOutOfRangeException ex) {
462 // Specified argument was out of the range of valid values
463 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
464 Assert.IsNull (ex.InnerException, "#A3");
465 Assert.IsNotNull (ex.Message, "#A4");
466 Assert.AreEqual ("port", ex.ParamName, "#A5");
470 new UdpClient ("local", IPEndPoint.MinPort - 1);
472 } catch (ArgumentOutOfRangeException ex) {
473 // Specified argument was out of the range of valid values
474 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
475 Assert.IsNull (ex.InnerException, "#A3");
476 Assert.IsNotNull (ex.Message, "#A4");
477 Assert.AreEqual ("port", ex.ParamName, "#A5");
482 public void UdpClientBroadcastTest ()
484 UdpClient client = new UdpClient ();
485 byte[] bytes = new byte[] {10, 11, 12, 13};
488 client.Send (bytes, bytes.Length, new IPEndPoint (IPAddress.Broadcast, 1235));
494 [Test] // JoinMulticastGroup (IPAddress)
495 public void JoinMulticastGroup1_IPv4 ()
497 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
499 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
500 client.JoinMulticastGroup (mcast_addr);
504 [Test] // JoinMulticastGroup (IPAddress)
505 public void JoinMulticastGroup1_IPv6 ()
508 if (!Socket.OSSupportsIPv6)
510 if (!Socket.SupportsIPv6)
512 Assert.Ignore ("IPv6 not enabled.");
514 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
516 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
517 client.JoinMulticastGroup (mcast_addr);
521 [Test] // JoinMulticastGroup (IPAddress)
522 public void JoinMulticastGroup1_MulticastAddr_Null ()
524 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
526 client.JoinMulticastGroup ((IPAddress) null);
529 } catch (ArgumentNullException ex) {
530 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
531 Assert.IsNull (ex.InnerException, "#3");
532 Assert.IsNotNull (ex.Message, "#4");
533 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
536 } catch (NullReferenceException ex) {
537 Assert.AreEqual (typeof (NullReferenceException), ex.GetType (), "#2");
538 Assert.IsNull (ex.InnerException, "#3");
539 Assert.IsNotNull (ex.Message, "#4");
545 [Test] // JoinMulticastGroup (IPAddress)
546 public void JoinMulticastGroup1_Socket_Closed ()
548 IPAddress mcast_addr = null;
550 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
553 client.JoinMulticastGroup (mcast_addr);
555 } catch (ObjectDisposedException ex) {
556 // Cannot access a disposed object
557 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
558 Assert.IsNull (ex.InnerException, "#3");
559 Assert.IsNotNull (ex.Message, "#4");
560 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
564 [Test] // JoinMulticastGroup (IPAddress)
565 [Category ("NotWorking")]
566 public void JoinMulticastGroup1_Socket_NotBound ()
568 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
570 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
572 client.JoinMulticastGroup (mcast_addr);
574 } catch (SocketException ex) {
575 // An invalid argument was supplied
576 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
577 Assert.AreEqual (10022, ex.ErrorCode, "#3");
578 Assert.IsNull (ex.InnerException, "#4");
579 Assert.IsNotNull (ex.Message, "#5");
580 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
582 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
589 [Test] // JoinMulticastGroup (In32, IPAddress)
590 public void JoinMulticastGroup2_IPv4 ()
592 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
594 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
596 client.JoinMulticastGroup (0, mcast_addr);
598 } catch (SocketException ex) {
599 // The attempted operation is not supported for the type of
601 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
602 Assert.AreEqual (10045, ex.ErrorCode, "#3");
603 Assert.IsNull (ex.InnerException, "#4");
604 Assert.IsNotNull (ex.Message, "#5");
605 Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
607 Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
613 [Test] // JoinMulticastGroup (In32, IPAddress)
614 public void JoinMulticastGroup2_IPv6 ()
617 if (!Socket.OSSupportsIPv6)
619 if (!Socket.SupportsIPv6)
621 Assert.Ignore ("IPv6 not enabled.");
623 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
625 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
626 client.JoinMulticastGroup (0, mcast_addr);
630 [Test] // JoinMulticastGroup (Int32, IPAddress)
631 public void JoinMulticastGroup2_MulticastAddr_Null ()
633 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
635 client.JoinMulticastGroup (0, (IPAddress) null);
637 } catch (ArgumentNullException ex) {
638 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
639 Assert.IsNull (ex.InnerException, "#3");
640 Assert.IsNotNull (ex.Message, "#4");
641 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
646 [Test] // JoinMulticastGroup (Int32, IPAddress)
647 public void JoinMulticastGroup2_Socket_Closed ()
649 IPAddress mcast_addr = null;
651 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234));
654 client.JoinMulticastGroup (0, mcast_addr);
656 } catch (ObjectDisposedException ex) {
657 // Cannot access a disposed object
658 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
659 Assert.IsNull (ex.InnerException, "#3");
660 Assert.IsNotNull (ex.Message, "#4");
661 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
665 [Test] // JoinMulticastGroup (Int32, IPAddress)
666 [Category ("NotWorking")]
667 public void JoinMulticastGroup2_Socket_NotBound ()
669 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
671 UdpClient client = new UdpClient (AddressFamily.InterNetworkV6);
673 client.JoinMulticastGroup (0, mcast_addr);
675 } catch (SocketException ex) {
676 // An invalid argument was supplied
677 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
678 Assert.AreEqual (10022, ex.ErrorCode, "#3");
679 Assert.IsNull (ex.InnerException, "#4");
680 Assert.IsNotNull (ex.Message, "#5");
681 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
683 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
690 [Test] // JoinMulticastGroup (IPAddress, Int32)
691 public void JoinMulticastGroup3_IPv4 ()
693 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
695 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
696 client.JoinMulticastGroup (mcast_addr, 0);
699 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
700 client.JoinMulticastGroup (mcast_addr, 255);
704 [Test] // JoinMulticastGroup (IPAddress, Int32)
705 public void JoinMulticastGroup3_IPv6 ()
708 if (!Socket.OSSupportsIPv6)
710 if (!Socket.SupportsIPv6)
712 Assert.Ignore ("IPv6 not enabled.");
714 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
716 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
717 client.JoinMulticastGroup (mcast_addr, 0);
720 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
721 client.JoinMulticastGroup (mcast_addr, 255);
725 [Test] // JoinMulticastGroup (IPAddress, Int32)
726 public void JoinMulticastGroup3_MulticastAddr_Null ()
728 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
730 client.JoinMulticastGroup ((IPAddress) null, int.MaxValue);
732 } catch (ArgumentNullException ex) {
733 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
734 Assert.IsNull (ex.InnerException, "#3");
735 Assert.IsNotNull (ex.Message, "#4");
736 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
741 [Test] // JoinMulticastGroup (IPAddress, Int32)
742 public void JoinMulticastGroup3_Socket_Closed ()
744 IPAddress mcast_addr = null;
746 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
749 client.JoinMulticastGroup (mcast_addr, 0);
751 } catch (ObjectDisposedException ex) {
752 // Cannot access a disposed object
753 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
754 Assert.IsNull (ex.InnerException, "#3");
755 Assert.IsNotNull (ex.Message, "#4");
756 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
760 [Test] // JoinMulticastGroup (IPAddress, Int32)
761 [Category ("NotWorking")]
762 public void JoinMulticastGroup3_Socket_NotBound ()
764 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
766 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
768 client.JoinMulticastGroup (mcast_addr, 5);
770 } catch (SocketException ex) {
771 // An invalid argument was supplied
772 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
773 Assert.AreEqual (10022, ex.ErrorCode, "#3");
774 Assert.IsNull (ex.InnerException, "#4");
775 Assert.IsNotNull (ex.Message, "#5");
776 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
778 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
786 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
787 public void JoinMulticastGroup4_IPv4 ()
789 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
790 IPAddress local_addr = IPAddress.Any;
792 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
793 client.JoinMulticastGroup (mcast_addr, local_addr);
797 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
798 public void JoinMulticastGroup4_IPv6 ()
800 if (!Socket.OSSupportsIPv6)
801 Assert.Ignore ("IPv6 not enabled.");
803 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
804 IPAddress local_addr = IPAddress.IPv6Any;
806 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
808 client.JoinMulticastGroup (mcast_addr, local_addr);
810 } catch (SocketException ex) {
811 // The attempted operation is not supported for the type of
813 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
814 Assert.AreEqual (10045, ex.ErrorCode, "#3");
815 Assert.IsNull (ex.InnerException, "#4");
816 Assert.IsNotNull (ex.Message, "#5");
817 Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
818 Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
823 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
824 public void JoinMulticastGroup4_LocalAddress_Null ()
826 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
828 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
830 client.JoinMulticastGroup (mcast_addr, (IPAddress) null);
832 } catch (ArgumentNullException ex) {
833 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
834 Assert.IsNull (ex.InnerException, "#3");
835 Assert.IsNotNull (ex.Message, "#4");
836 Assert.AreEqual ("mcint", ex.ParamName, "#5");
841 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
842 public void JoinMulticastGroup4_MulticastAddr_Null ()
844 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
846 client.JoinMulticastGroup ((IPAddress) null, IPAddress.Loopback);
848 } catch (ArgumentNullException ex) {
849 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
850 Assert.IsNull (ex.InnerException, "#3");
851 Assert.IsNotNull (ex.Message, "#4");
852 Assert.AreEqual ("group", ex.ParamName, "#5");
857 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
858 public void JoinMulticastGroup4_Socket_Closed ()
860 IPAddress mcast_addr = null;
861 IPAddress local_addr = null;
863 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
866 client.JoinMulticastGroup (mcast_addr, local_addr);
868 } catch (ObjectDisposedException ex) {
869 // Cannot access a disposed object
870 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
871 Assert.IsNull (ex.InnerException, "#3");
872 Assert.IsNotNull (ex.Message, "#4");
873 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
877 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
878 [Category ("NotWorking")]
879 public void JoinMulticastGroup4_Socket_NotBound ()
881 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
882 IPAddress local_addr = Dns.GetHostEntry (string.Empty).AddressList [0];
884 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
886 client.JoinMulticastGroup (mcast_addr, local_addr);
888 } catch (SocketException ex) {
889 // An invalid argument was supplied
890 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
891 Assert.AreEqual (10022, ex.ErrorCode, "#3");
892 Assert.IsNull (ex.InnerException, "#4");
893 Assert.IsNotNull (ex.Message, "#5");
894 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
895 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
902 public void CloseInReceive ()
904 UdpClient client = null;
905 var rnd = new Random ();
906 for (int i = 0; i < 5; i++) {
907 int port = rnd.Next (1025, 65534);
909 client = new UdpClient (port);
911 } catch (Exception ex) {
917 new Thread(delegate() {
922 bool got_exc = false;
923 IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
925 client.Receive(ref ep);
926 } catch (SocketException) {
931 Assert.IsTrue (got_exc);
934 // Test for bug 324033
936 public void JoinMulticastGroupWithLocal ()
938 UdpClient client = new UdpClient (9001);
939 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.24");
940 IPAddress local_addr = IPAddress.Any;
943 client.JoinMulticastGroup (mcast_addr, local_addr);
950 [ExpectedException (typeof(ArgumentNullException))]
951 public void BeginSendNull ()
953 UdpClient client = new UdpClient ();
954 client.BeginSend (null, 0, null, null);
958 static bool BSSent = false;
960 static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
962 private static void BSCallback (IAsyncResult asyncResult)
964 UdpClient client = (UdpClient)asyncResult.AsyncState;
966 BSBytes = client.EndSend (asyncResult);
973 public void BeginSend ()
975 UdpClient client = new UdpClient ();
976 byte[] bytes = new byte[] {10, 11, 12, 13};
979 client.BeginSend (bytes, bytes.Length, new AsyncCallback (BSCallback), client);
980 Assert.Fail ("BeginSend #1");
981 } catch (SocketException ex) {
982 Assert.AreEqual (10057, ex.ErrorCode,
987 client.BeginSend (bytes, bytes.Length, null, new AsyncCallback (BSCallback), client);
988 Assert.Fail ("BeginSend #3");
989 } catch (SocketException ex) {
990 Assert.AreEqual (10057, ex.ErrorCode,
994 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1236);
996 BSCalledBack.Reset ();
998 client.BeginSend (bytes, bytes.Length, ep,
999 new AsyncCallback (BSCallback),
1001 if (BSCalledBack.WaitOne (2000, false) == false) {
1002 Assert.Fail ("BeginSend wait timed out");
1005 Assert.AreEqual (true, BSSent, "BeginSend #5");
1006 Assert.AreEqual (4, BSBytes, "BeginSend #6");
1011 static bool BRReceived = false;
1012 static byte[] BRBytes;
1013 static IPEndPoint BRFrom;
1014 static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
1016 private static void BRCallback (IAsyncResult asyncResult)
1018 UdpClient client = (UdpClient)asyncResult.AsyncState;
1020 BRBytes = client.EndReceive (asyncResult, ref BRFrom);
1023 BRCalledBack.Set ();
1027 public void BeginReceive ()
1029 UdpClient client = new UdpClient (1237);
1031 BRCalledBack.Reset ();
1033 client.BeginReceive (BRCallback, client);
1035 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1237);
1036 byte[] send_bytes = new byte[] {10, 11, 12, 13};
1037 client.Send (send_bytes, send_bytes.Length, ep);
1039 if (BRCalledBack.WaitOne (2000, false) == false) {
1040 Assert.Fail ("BeginReceive wait timed out");
1043 Assert.AreEqual (true, BRReceived, "BeginReceive #1");
1044 Assert.AreEqual (4, BRBytes.Length, "BeginReceive #2");
1045 Assert.AreEqual (ep. Port, BRFrom.Port, "BeginReceive #3");
1046 Assert.AreEqual (ep.Address, BRFrom.Address, "BeginReceive #4");
1052 public void Available ()
1054 UdpClient client = new UdpClient (1238);
1055 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1238);
1056 byte[] bytes = new byte[] {10, 11, 12, 13};
1058 client.Send (bytes, bytes.Length, ep);
1059 int avail = client.Available;
1061 Assert.AreEqual (bytes.Length, avail, "Available #1");
1067 [Category ("NotWorking")] // Using PMTU settings workaround on Linux, default true
1068 public void DontFragmentDefault ()
1070 UdpClient client = new UdpClient ();
1072 /* Ignore the docs, testing shows the default
1073 * here is in fact false
1075 Assert.AreEqual (false, client.DontFragment, "DontFragmentDefault");
1081 public void EnableBroadcastDefault ()
1083 UdpClient client = new UdpClient ();
1085 Assert.AreEqual (false, client.EnableBroadcast, "EnableBroadcastDefault");
1090 /* Can't test the default for ExclusiveAddressUse as
1091 * it's different on different versions and service
1095 [Category ("NotWorking")] // Not supported on Linux
1096 public void ExclusiveAddressUseUnbound ()
1098 UdpClient client = new UdpClient ();
1100 client.ExclusiveAddressUse = true;
1102 Assert.AreEqual (true, client.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
1108 [ExpectedException (typeof(InvalidOperationException))]
1109 [Category ("NotWorking")] // Not supported on Linux
1110 public void ExclusiveAddressUseBound ()
1112 UdpClient client = new UdpClient (1239);
1114 client.ExclusiveAddressUse = true;
1120 public void MulticastLoopbackDefault ()
1122 UdpClient client = new UdpClient ();
1124 Assert.AreEqual (true, client.MulticastLoopback, "MulticastLoopbackDefault");
1129 /* No test for Ttl default as it is platform dependent */
1132 class MyUdpClient : UdpClient
1134 public MyUdpClient ()
1138 public MyUdpClient (AddressFamily family)
1143 public MyUdpClient (Int32 port)
1149 public MyUdpClient (IPEndPoint localEP)
1154 public MyUdpClient (int port, AddressFamily family)
1155 : base (port, family)
1159 public MyUdpClient (string hostname, int port)
1160 : base (hostname, port)
1164 public new bool Active {
1165 get { return base.Active; }
1166 set { base.Active = value; }
1170 public new Socket Client {
1171 get { return base.Client; }
1172 set { base.Client = value; }