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 [Category ("NotOnMac")]
506 public void JoinMulticastGroup1_IPv6 ()
509 if (!Socket.OSSupportsIPv6)
511 if (!Socket.SupportsIPv6)
513 Assert.Ignore ("IPv6 not enabled.");
515 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
517 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
518 client.JoinMulticastGroup (mcast_addr);
522 [Test] // JoinMulticastGroup (IPAddress)
523 public void JoinMulticastGroup1_MulticastAddr_Null ()
525 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
527 client.JoinMulticastGroup ((IPAddress) null);
530 } catch (ArgumentNullException ex) {
531 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
532 Assert.IsNull (ex.InnerException, "#3");
533 Assert.IsNotNull (ex.Message, "#4");
534 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
537 } catch (NullReferenceException ex) {
538 Assert.AreEqual (typeof (NullReferenceException), ex.GetType (), "#2");
539 Assert.IsNull (ex.InnerException, "#3");
540 Assert.IsNotNull (ex.Message, "#4");
546 [Test] // JoinMulticastGroup (IPAddress)
547 public void JoinMulticastGroup1_Socket_Closed ()
549 IPAddress mcast_addr = null;
551 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
554 client.JoinMulticastGroup (mcast_addr);
556 } catch (ObjectDisposedException ex) {
557 // Cannot access a disposed object
558 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
559 Assert.IsNull (ex.InnerException, "#3");
560 Assert.IsNotNull (ex.Message, "#4");
561 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
565 [Test] // JoinMulticastGroup (IPAddress)
566 [Category ("NotWorking")]
567 public void JoinMulticastGroup1_Socket_NotBound ()
569 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
571 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
573 client.JoinMulticastGroup (mcast_addr);
575 } catch (SocketException ex) {
576 // An invalid argument was supplied
577 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
578 Assert.AreEqual (10022, ex.ErrorCode, "#3");
579 Assert.IsNull (ex.InnerException, "#4");
580 Assert.IsNotNull (ex.Message, "#5");
581 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
583 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
590 [Test] // JoinMulticastGroup (In32, IPAddress)
591 public void JoinMulticastGroup2_IPv4 ()
593 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
595 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
597 client.JoinMulticastGroup (0, mcast_addr);
599 } catch (SocketException ex) {
600 // The attempted operation is not supported for the type of
602 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
603 Assert.AreEqual (10045, ex.ErrorCode, "#3");
604 Assert.IsNull (ex.InnerException, "#4");
605 Assert.IsNotNull (ex.Message, "#5");
606 Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
608 Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
614 [Test] // JoinMulticastGroup (In32, IPAddress)
615 [Category ("NotOnMac")]
616 public void JoinMulticastGroup2_IPv6 ()
619 if (!Socket.OSSupportsIPv6)
621 if (!Socket.SupportsIPv6)
623 Assert.Ignore ("IPv6 not enabled.");
625 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
627 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
628 client.JoinMulticastGroup (0, mcast_addr);
632 [Test] // JoinMulticastGroup (Int32, IPAddress)
633 public void JoinMulticastGroup2_MulticastAddr_Null ()
635 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
637 client.JoinMulticastGroup (0, (IPAddress) null);
639 } catch (ArgumentNullException ex) {
640 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
641 Assert.IsNull (ex.InnerException, "#3");
642 Assert.IsNotNull (ex.Message, "#4");
643 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
648 [Test] // JoinMulticastGroup (Int32, IPAddress)
649 public void JoinMulticastGroup2_Socket_Closed ()
651 IPAddress mcast_addr = null;
653 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234));
656 client.JoinMulticastGroup (0, mcast_addr);
658 } catch (ObjectDisposedException ex) {
659 // Cannot access a disposed object
660 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
661 Assert.IsNull (ex.InnerException, "#3");
662 Assert.IsNotNull (ex.Message, "#4");
663 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
667 [Test] // JoinMulticastGroup (Int32, IPAddress)
668 [Category ("NotWorking")]
669 public void JoinMulticastGroup2_Socket_NotBound ()
671 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
673 UdpClient client = new UdpClient (AddressFamily.InterNetworkV6);
675 client.JoinMulticastGroup (0, mcast_addr);
677 } catch (SocketException ex) {
678 // An invalid argument was supplied
679 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
680 Assert.AreEqual (10022, ex.ErrorCode, "#3");
681 Assert.IsNull (ex.InnerException, "#4");
682 Assert.IsNotNull (ex.Message, "#5");
683 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
685 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
692 [Test] // JoinMulticastGroup (IPAddress, Int32)
693 public void JoinMulticastGroup3_IPv4 ()
695 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
697 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
698 client.JoinMulticastGroup (mcast_addr, 0);
701 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
702 client.JoinMulticastGroup (mcast_addr, 255);
706 [Test] // JoinMulticastGroup (IPAddress, Int32)
707 [Category ("NotOnMac")]
708 public void JoinMulticastGroup3_IPv6 ()
711 if (!Socket.OSSupportsIPv6)
713 if (!Socket.SupportsIPv6)
715 Assert.Ignore ("IPv6 not enabled.");
717 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
719 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
720 client.JoinMulticastGroup (mcast_addr, 0);
723 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
724 client.JoinMulticastGroup (mcast_addr, 255);
728 [Test] // JoinMulticastGroup (IPAddress, Int32)
729 public void JoinMulticastGroup3_MulticastAddr_Null ()
731 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
733 client.JoinMulticastGroup ((IPAddress) null, int.MaxValue);
735 } catch (ArgumentNullException ex) {
736 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
737 Assert.IsNull (ex.InnerException, "#3");
738 Assert.IsNotNull (ex.Message, "#4");
739 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
744 [Test] // JoinMulticastGroup (IPAddress, Int32)
745 public void JoinMulticastGroup3_Socket_Closed ()
747 IPAddress mcast_addr = null;
749 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
752 client.JoinMulticastGroup (mcast_addr, 0);
754 } catch (ObjectDisposedException ex) {
755 // Cannot access a disposed object
756 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
757 Assert.IsNull (ex.InnerException, "#3");
758 Assert.IsNotNull (ex.Message, "#4");
759 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
763 [Test] // JoinMulticastGroup (IPAddress, Int32)
764 [Category ("NotWorking")]
765 public void JoinMulticastGroup3_Socket_NotBound ()
767 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
769 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
771 client.JoinMulticastGroup (mcast_addr, 5);
773 } catch (SocketException ex) {
774 // An invalid argument was supplied
775 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
776 Assert.AreEqual (10022, ex.ErrorCode, "#3");
777 Assert.IsNull (ex.InnerException, "#4");
778 Assert.IsNotNull (ex.Message, "#5");
779 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
781 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
789 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
790 public void JoinMulticastGroup4_IPv4 ()
792 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
793 IPAddress local_addr = IPAddress.Any;
795 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
796 client.JoinMulticastGroup (mcast_addr, local_addr);
800 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
801 public void JoinMulticastGroup4_IPv6 ()
803 if (!Socket.OSSupportsIPv6)
804 Assert.Ignore ("IPv6 not enabled.");
806 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
807 IPAddress local_addr = IPAddress.IPv6Any;
809 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
811 client.JoinMulticastGroup (mcast_addr, local_addr);
813 } catch (SocketException ex) {
814 // The attempted operation is not supported for the type of
816 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
817 Assert.AreEqual (10045, ex.ErrorCode, "#3");
818 Assert.IsNull (ex.InnerException, "#4");
819 Assert.IsNotNull (ex.Message, "#5");
820 Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
821 Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
826 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
827 public void JoinMulticastGroup4_LocalAddress_Null ()
829 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
831 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
833 client.JoinMulticastGroup (mcast_addr, (IPAddress) null);
835 } catch (ArgumentNullException ex) {
836 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
837 Assert.IsNull (ex.InnerException, "#3");
838 Assert.IsNotNull (ex.Message, "#4");
839 Assert.AreEqual ("mcint", ex.ParamName, "#5");
844 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
845 public void JoinMulticastGroup4_MulticastAddr_Null ()
847 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
849 client.JoinMulticastGroup ((IPAddress) null, IPAddress.Loopback);
851 } catch (ArgumentNullException ex) {
852 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
853 Assert.IsNull (ex.InnerException, "#3");
854 Assert.IsNotNull (ex.Message, "#4");
855 Assert.AreEqual ("group", ex.ParamName, "#5");
860 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
861 public void JoinMulticastGroup4_Socket_Closed ()
863 IPAddress mcast_addr = null;
864 IPAddress local_addr = null;
866 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
869 client.JoinMulticastGroup (mcast_addr, local_addr);
871 } catch (ObjectDisposedException ex) {
872 // Cannot access a disposed object
873 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
874 Assert.IsNull (ex.InnerException, "#3");
875 Assert.IsNotNull (ex.Message, "#4");
876 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
880 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
881 [Category ("NotWorking")]
882 public void JoinMulticastGroup4_Socket_NotBound ()
884 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
885 IPAddress local_addr = Dns.GetHostEntry (string.Empty).AddressList [0];
887 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
889 client.JoinMulticastGroup (mcast_addr, local_addr);
891 } catch (SocketException ex) {
892 // An invalid argument was supplied
893 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
894 Assert.AreEqual (10022, ex.ErrorCode, "#3");
895 Assert.IsNull (ex.InnerException, "#4");
896 Assert.IsNotNull (ex.Message, "#5");
897 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
898 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
905 public void CloseInReceive ()
907 UdpClient client = new UdpClient (50000);
908 new Thread(delegate() {
913 bool got_exc = false;
914 IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
916 client.Receive(ref ep);
917 } catch (SocketException) {
922 Assert.IsTrue (got_exc);
925 // Test for bug 324033
927 public void JoinMulticastGroupWithLocal ()
929 UdpClient client = new UdpClient (9001);
930 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.24");
931 IPAddress local_addr = IPAddress.Any;
934 client.JoinMulticastGroup (mcast_addr, local_addr);
941 [ExpectedException (typeof(ArgumentNullException))]
942 public void BeginSendNull ()
944 UdpClient client = new UdpClient ();
945 client.BeginSend (null, 0, null, null);
949 static bool BSSent = false;
951 static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
953 private static void BSCallback (IAsyncResult asyncResult)
955 UdpClient client = (UdpClient)asyncResult.AsyncState;
957 BSBytes = client.EndSend (asyncResult);
964 public void BeginSend ()
966 UdpClient client = new UdpClient ();
967 byte[] bytes = new byte[] {10, 11, 12, 13};
970 client.BeginSend (bytes, bytes.Length, new AsyncCallback (BSCallback), client);
971 Assert.Fail ("BeginSend #1");
972 } catch (SocketException ex) {
973 Assert.AreEqual (10057, ex.ErrorCode,
978 client.BeginSend (bytes, bytes.Length, null, new AsyncCallback (BSCallback), client);
979 Assert.Fail ("BeginSend #3");
980 } catch (SocketException ex) {
981 Assert.AreEqual (10057, ex.ErrorCode,
985 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1236);
987 BSCalledBack.Reset ();
989 client.BeginSend (bytes, bytes.Length, ep,
990 new AsyncCallback (BSCallback),
992 if (BSCalledBack.WaitOne (2000, false) == false) {
993 Assert.Fail ("BeginSend wait timed out");
996 Assert.AreEqual (true, BSSent, "BeginSend #5");
997 Assert.AreEqual (4, BSBytes, "BeginSend #6");
1002 static bool BRReceived = false;
1003 static byte[] BRBytes;
1004 static IPEndPoint BRFrom;
1005 static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
1007 private static void BRCallback (IAsyncResult asyncResult)
1009 UdpClient client = (UdpClient)asyncResult.AsyncState;
1011 BRBytes = client.EndReceive (asyncResult, ref BRFrom);
1014 BRCalledBack.Set ();
1018 public void BeginReceive ()
1020 UdpClient client = new UdpClient (1237);
1022 BRCalledBack.Reset ();
1024 client.BeginReceive (BRCallback, client);
1026 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1237);
1027 byte[] send_bytes = new byte[] {10, 11, 12, 13};
1028 client.Send (send_bytes, send_bytes.Length, ep);
1030 if (BRCalledBack.WaitOne (2000, false) == false) {
1031 Assert.Fail ("BeginReceive wait timed out");
1034 Assert.AreEqual (true, BRReceived, "BeginReceive #1");
1035 Assert.AreEqual (4, BRBytes.Length, "BeginReceive #2");
1036 Assert.AreEqual (ep. Port, BRFrom.Port, "BeginReceive #3");
1037 Assert.AreEqual (ep.Address, BRFrom.Address, "BeginReceive #4");
1043 public void Available ()
1045 UdpClient client = new UdpClient (1238);
1046 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1238);
1047 byte[] bytes = new byte[] {10, 11, 12, 13};
1049 client.Send (bytes, bytes.Length, ep);
1050 int avail = client.Available;
1052 Assert.AreEqual (bytes.Length, avail, "Available #1");
1058 [Category ("NotWorking")] // Using PMTU settings workaround on Linux, default true
1059 public void DontFragmentDefault ()
1061 UdpClient client = new UdpClient ();
1063 /* Ignore the docs, testing shows the default
1064 * here is in fact false
1066 Assert.AreEqual (false, client.DontFragment, "DontFragmentDefault");
1072 public void EnableBroadcastDefault ()
1074 UdpClient client = new UdpClient ();
1076 Assert.AreEqual (false, client.EnableBroadcast, "EnableBroadcastDefault");
1081 /* Can't test the default for ExclusiveAddressUse as
1082 * it's different on different versions and service
1086 [Category ("NotWorking")] // Not supported on Linux
1087 public void ExclusiveAddressUseUnbound ()
1089 UdpClient client = new UdpClient ();
1091 client.ExclusiveAddressUse = true;
1093 Assert.AreEqual (true, client.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
1099 [ExpectedException (typeof(InvalidOperationException))]
1100 [Category ("NotWorking")] // Not supported on Linux
1101 public void ExclusiveAddressUseBound ()
1103 UdpClient client = new UdpClient (1239);
1105 client.ExclusiveAddressUse = true;
1111 public void MulticastLoopbackDefault ()
1113 UdpClient client = new UdpClient ();
1115 Assert.AreEqual (true, client.MulticastLoopback, "MulticastLoopbackDefault");
1120 /* No test for Ttl default as it is platform dependent */
1123 class MyUdpClient : UdpClient
1125 public MyUdpClient ()
1129 public MyUdpClient (AddressFamily family)
1134 public MyUdpClient (Int32 port)
1140 public MyUdpClient (IPEndPoint localEP)
1145 public MyUdpClient (int port, AddressFamily family)
1146 : base (port, family)
1150 public MyUdpClient (string hostname, int port)
1151 : base (hostname, port)
1155 public new bool Active {
1156 get { return base.Active; }
1157 set { base.Active = value; }
1161 public new Socket Client {
1162 get { return base.Client; }
1163 set { base.Client = value; }