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;
12 using System.Threading.Tasks;
14 using NUnit.Framework;
16 namespace MonoTests.System.Net.Sockets {
18 [Category ("RequiresBSDSockets")]
19 public class UdpClientTest {
21 public void Constructor1 ()
26 client = new MyUdpClient ();
28 Assert.IsNotNull (s, "Client");
29 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "Client:AddressFamily");
30 Assert.IsFalse (s.Connected, "Client:Connected");
31 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
32 Assert.IsNull (s.LocalEndPoint, "Client:LocalEndPoint");
33 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "Client:ProtocolType");
34 Assert.IsNull (s.RemoteEndPoint, "Client:RemoteEndPoint");
35 Assert.AreEqual (SocketType.Dgram, s.SocketType, "Client:SocketType");
36 Assert.IsFalse (client.Active, "Active");
37 Assert.IsFalse (client.DontFragment, "DontFragment");
38 Assert.IsFalse (client.EnableBroadcast, "EnableBroadcast");
39 //Assert.IsFalse (client.ExclusiveAddressUse, "ExclusiveAddressUse");
40 Assert.IsTrue (client.MulticastLoopback, "MulticastLoopback");
41 //Assert.AreEqual (32, client.Ttl, "Ttl");
45 [Test] // .ctor (AddressFamily)
46 public void Constructor2 ()
51 client = new MyUdpClient (AddressFamily.InterNetwork);
53 Assert.IsNotNull (s, "#A:Client");
54 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
55 Assert.IsFalse (s.Connected, "#A:Client:Connected");
56 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
57 Assert.IsNull (s.LocalEndPoint, "#A:Client:LocalEndPoint");
58 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
59 Assert.IsNull (s.RemoteEndPoint, "#A:Client:RemoteEndPoint");
60 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
61 Assert.IsFalse (client.Active, "#A:Active");
62 //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
63 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
64 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
65 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
66 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
68 if (!Socket.OSSupportsIPv6)
69 Assert.Ignore ("IPv6 not enabled.");
71 client = new MyUdpClient (AddressFamily.InterNetworkV6);
73 Assert.IsNotNull (s, "#B:Client");
74 Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
75 Assert.IsFalse (s.Connected, "#B:Client:Connected");
76 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
77 Assert.IsNull (s.LocalEndPoint, "#B:Client:LocalEndPoint");
78 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
79 Assert.IsNull (s.RemoteEndPoint, "#B:Client:RemoteEndPoint");
80 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
81 Assert.IsFalse (client.Active, "#B:Active");
82 //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
83 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
84 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
85 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
86 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
90 [Test] // .ctor (AddressFamily)
91 public void Constructor2_Family_Invalid ()
94 new UdpClient (AddressFamily.NetBios);
96 } catch (ArgumentException ex) {
97 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
98 Assert.IsNull (ex.InnerException, "#A3");
99 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
101 Assert.IsNotNull (ex.Message, "#A4");
102 Assert.AreEqual ("family", ex.ParamName, "#A5");
106 new UdpClient ((AddressFamily) 666);
108 } catch (ArgumentException ex) {
109 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
110 Assert.IsNull (ex.InnerException, "#B3");
111 Assert.IsNotNull (ex.Message, "#B4");
112 Assert.AreEqual ("family", ex.ParamName, "#B5");
116 [Test] // .ctor (Int32)
117 public void Constructor3 ()
122 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort))
125 Assert.IsNotNull (s, "#A:Client");
126 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
127 Assert.IsFalse (s.Connected, "#A:Client:Connected");
128 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
129 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
130 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
131 Assert.IsFalse (client.Active, "#A:Active");
132 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
133 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
134 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
135 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
136 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
137 localEP = s.LocalEndPoint as IPEndPoint;
138 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
139 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
140 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
142 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort))
145 Assert.IsNotNull (s, "#B:Client");
146 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
147 Assert.IsFalse (s.Connected, "#B:Client:Connected");
148 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
149 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
150 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
151 Assert.IsFalse (client.Active, "#B:Active");
152 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
153 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
154 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
155 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
156 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
157 localEP = s.LocalEndPoint as IPEndPoint;
158 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
159 Assert.AreEqual (IPAddress.Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
160 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
161 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
165 [Test] // .ctor (Int32)
166 public void Constructor3_Port_OutOfRange ()
169 new UdpClient (IPEndPoint.MaxPort + 1);
171 } catch (ArgumentOutOfRangeException ex) {
172 // Specified argument was out of the range of valid values
173 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
174 Assert.IsNull (ex.InnerException, "#A3");
175 Assert.IsNotNull (ex.Message, "#A4");
176 Assert.AreEqual ("port", ex.ParamName, "#A5");
180 new UdpClient (IPEndPoint.MinPort - 1);
182 } catch (ArgumentOutOfRangeException ex) {
183 // Specified argument was out of the range of valid values
184 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
185 Assert.IsNull (ex.InnerException, "#A3");
186 Assert.IsNotNull (ex.Message, "#A4");
187 Assert.AreEqual ("port", ex.ParamName, "#A5");
191 [Test] // .ctor (IPEndPoint)
192 public void Constructor4 ()
198 clientEP = new IPEndPoint (IPAddress.Loopback, 8001);
199 using (MyUdpClient client = new MyUdpClient (clientEP))
202 Assert.IsNotNull (s, "#A:Client");
203 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
204 Assert.IsFalse (s.Connected, "#A:Client:Connected");
205 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
206 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
207 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
208 Assert.IsFalse (client.Active, "#A:Active");
209 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
210 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
211 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
212 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
213 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
214 localEP = s.LocalEndPoint as IPEndPoint;
215 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
216 Assert.IsFalse (object.ReferenceEquals (clientEP, localEP), "#A:Client:LocalEndPoint/ReferenceEquality");
217 Assert.AreEqual (clientEP.Address, localEP.Address, "#A:Client:LocalEndPoint/Address");
218 Assert.AreEqual (clientEP.AddressFamily, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
219 Assert.AreEqual (clientEP.Port, localEP.Port, "#A:Client:LocalEndPoint/Port");
223 [Test] // .ctor (IPEndPoint)
224 public void Constructor4_LocalEP_Null ()
227 new UdpClient ((IPEndPoint) null);
229 } catch (ArgumentNullException ex) {
230 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
231 Assert.IsNull (ex.InnerException, "#3");
232 Assert.IsNotNull (ex.Message, "#4");
233 Assert.AreEqual ("localEP", ex.ParamName, "#5");
237 [Test] // .ctor (Int32, AddressFamily)
238 public void Constructor5 ()
243 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort, AddressFamily.InterNetwork))
246 Assert.IsNotNull (s, "#A:Client");
247 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
248 Assert.IsFalse (s.Connected, "#A:Client:Connected");
249 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
250 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
251 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
252 Assert.IsFalse (client.Active, "#A:Active");
253 //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
254 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
255 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
256 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
257 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
258 localEP = s.LocalEndPoint as IPEndPoint;
259 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
260 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
261 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
264 if (!Socket.OSSupportsIPv6)
265 Assert.Ignore ("IPv6 not enabled.");
267 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort, AddressFamily.InterNetworkV6))
270 Assert.IsNotNull (s, "#B:Client");
271 Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
272 Assert.IsFalse (s.Connected, "#B:Client:Connected");
273 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
274 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
275 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
276 Assert.IsFalse (client.Active, "#B:Active");
277 //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
278 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
279 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
280 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
281 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
282 localEP = s.LocalEndPoint as IPEndPoint;
283 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
284 Assert.AreEqual (IPAddress.IPv6Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
285 Assert.AreEqual (AddressFamily.InterNetworkV6, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
286 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
290 [Test] // .ctor (Int32, AddressFamily)
291 public void Constructor5_Family_Invalid ()
294 new UdpClient (80, AddressFamily.NetBios);
296 } catch (ArgumentException ex) {
298 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
299 Assert.IsNull (ex.InnerException, "#A3");
300 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
302 Assert.IsNotNull (ex.Message, "#A4");
303 Assert.AreEqual ("family", ex.ParamName, "#A5");
307 new UdpClient (80, (AddressFamily) 666);
309 } catch (ArgumentException ex) {
311 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
312 Assert.IsNull (ex.InnerException, "#B3");
313 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
315 Assert.IsNotNull (ex.Message, "#B4");
316 Assert.AreEqual ("family", ex.ParamName, "#B5");
320 [Test] // .ctor (Int32, AddressFamily)
321 public void Constructor5_Port_OutOfRange ()
324 new UdpClient (IPEndPoint.MaxPort + 1, AddressFamily.InterNetwork);
326 } catch (ArgumentOutOfRangeException ex) {
327 // Specified argument was out of the range of valid values
328 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
329 Assert.IsNull (ex.InnerException, "#A3");
330 Assert.IsNotNull (ex.Message, "#A4");
331 Assert.AreEqual ("port", ex.ParamName, "#A5");
335 new UdpClient (IPEndPoint.MinPort - 1, AddressFamily.InterNetwork);
337 } catch (ArgumentOutOfRangeException ex) {
338 // Specified argument was out of the range of valid values
339 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
340 Assert.IsNull (ex.InnerException, "#A3");
341 Assert.IsNotNull (ex.Message, "#A4");
342 Assert.AreEqual ("port", ex.ParamName, "#A5");
346 [Test] // .ctor (String, Int32)
347 public void Constructor6 ()
353 // UDP port 0 doesn't seem to be valid.
354 using (MyUdpClient client = new MyUdpClient ("127.0.0.1", 53))
357 Assert.IsNotNull (s, "#A:Client");
358 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
359 Assert.IsTrue (s.Connected, "#A:Client:Connected");
360 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
361 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
362 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
363 Assert.IsTrue (client.Active, "#A:Active");
364 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
365 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
366 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
367 //Assert.IsFalse (client.MulticastLoopback, "#A:MulticastLoopback");
368 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
369 localEP = s.LocalEndPoint as IPEndPoint;
370 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
371 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#A:Client:LocalEndPoint/Address");
372 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
374 using (MyUdpClient client = new MyUdpClient ("127.0.0.1", IPEndPoint.MaxPort))
377 Assert.IsNotNull (s, "#B:Client");
378 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
379 Assert.IsTrue (s.Connected, "#B:Client:Connected");
380 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
381 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
382 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
383 Assert.IsTrue (client.Active, "#B:Active");
384 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
385 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
386 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
387 //Assert.IsFalse (client.MulticastLoopback, "#B:MulticastLoopback");
388 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
389 localEP = s.LocalEndPoint as IPEndPoint;
390 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
391 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#B:Client:LocalEndPoint/Address");
392 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
396 [Test] // .ctor (String, Int32)
397 public void Constructor6_HostName_Null ()
400 new UdpClient ((string) null, int.MaxValue);
402 } catch (ArgumentNullException ex) {
403 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
404 Assert.IsNull (ex.InnerException, "#3");
405 Assert.IsNotNull (ex.Message, "#4");
406 Assert.AreEqual ("hostname", ex.ParamName, "#5");
410 [Test] // .ctor (String, Int32)
411 public void Constructor6_Port_OutOfRange ()
414 new UdpClient ("local", IPEndPoint.MaxPort + 1);
416 } catch (ArgumentOutOfRangeException ex) {
417 // Specified argument was out of the range of valid values
418 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
419 Assert.IsNull (ex.InnerException, "#A3");
420 Assert.IsNotNull (ex.Message, "#A4");
421 Assert.AreEqual ("port", ex.ParamName, "#A5");
425 new UdpClient ("local", IPEndPoint.MinPort - 1);
427 } catch (ArgumentOutOfRangeException ex) {
428 // Specified argument was out of the range of valid values
429 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
430 Assert.IsNull (ex.InnerException, "#A3");
431 Assert.IsNotNull (ex.Message, "#A4");
432 Assert.AreEqual ("port", ex.ParamName, "#A5");
437 public void UdpClientBroadcastTest ()
439 UdpClient client = new UdpClient ();
440 byte[] bytes = new byte[] {10, 11, 12, 13};
443 client.Send (bytes, bytes.Length, new IPEndPoint (IPAddress.Broadcast, 1235));
449 [Test] // JoinMulticastGroup (IPAddress)
450 public void JoinMulticastGroup1_IPv4 ()
452 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
454 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
455 client.JoinMulticastGroup (mcast_addr);
459 [Test] // JoinMulticastGroup (IPAddress)
460 public void JoinMulticastGroup1_IPv6 ()
462 if (!Socket.OSSupportsIPv6)
463 Assert.Ignore ("IPv6 not enabled.");
465 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
467 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
468 client.JoinMulticastGroup (mcast_addr);
472 [Test] // JoinMulticastGroup (IPAddress)
473 public void JoinMulticastGroup1_MulticastAddr_Null ()
475 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
477 client.JoinMulticastGroup ((IPAddress) null);
479 } catch (ArgumentNullException ex) {
480 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
481 Assert.IsNull (ex.InnerException, "#3");
482 Assert.IsNotNull (ex.Message, "#4");
483 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
488 [Test] // JoinMulticastGroup (IPAddress)
489 public void JoinMulticastGroup1_Socket_Closed ()
491 IPAddress mcast_addr = null;
493 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
496 client.JoinMulticastGroup (mcast_addr);
498 } catch (ObjectDisposedException ex) {
499 // Cannot access a disposed object
500 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
501 Assert.IsNull (ex.InnerException, "#3");
502 Assert.IsNotNull (ex.Message, "#4");
503 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
507 [Test] // JoinMulticastGroup (IPAddress)
508 [Category ("NotWorking")]
509 public void JoinMulticastGroup1_Socket_NotBound ()
511 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
513 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
515 client.JoinMulticastGroup (mcast_addr);
517 } catch (SocketException ex) {
518 // An invalid argument was supplied
519 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
520 Assert.AreEqual (10022, ex.ErrorCode, "#3");
521 Assert.IsNull (ex.InnerException, "#4");
522 Assert.IsNotNull (ex.Message, "#5");
523 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
524 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
530 [Test] // JoinMulticastGroup (In32, IPAddress)
531 public void JoinMulticastGroup2_IPv4 ()
533 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
535 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
537 client.JoinMulticastGroup (0, mcast_addr);
539 } catch (SocketException ex) {
540 // The attempted operation is not supported for the type of
542 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
543 Assert.AreEqual (10045, ex.ErrorCode, "#3");
544 Assert.IsNull (ex.InnerException, "#4");
545 Assert.IsNotNull (ex.Message, "#5");
546 Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
547 Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
552 [Test] // JoinMulticastGroup (In32, IPAddress)
553 public void JoinMulticastGroup2_IPv6 ()
555 if (!Socket.OSSupportsIPv6)
556 Assert.Ignore ("IPv6 not enabled.");
558 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
560 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
561 client.JoinMulticastGroup (0, mcast_addr);
565 [Test] // JoinMulticastGroup (Int32, IPAddress)
566 public void JoinMulticastGroup2_MulticastAddr_Null ()
568 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
570 client.JoinMulticastGroup (0, (IPAddress) null);
572 } catch (ArgumentNullException ex) {
573 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
574 Assert.IsNull (ex.InnerException, "#3");
575 Assert.IsNotNull (ex.Message, "#4");
576 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
581 [Test] // JoinMulticastGroup (Int32, IPAddress)
582 public void JoinMulticastGroup2_Socket_Closed ()
584 if (!Socket.OSSupportsIPv6)
585 Assert.Ignore ("IPv6 not enabled.");
587 IPAddress mcast_addr = null;
589 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234));
592 client.JoinMulticastGroup (0, mcast_addr);
594 } catch (ObjectDisposedException ex) {
595 // Cannot access a disposed object
596 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
597 Assert.IsNull (ex.InnerException, "#3");
598 Assert.IsNotNull (ex.Message, "#4");
599 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
603 [Test] // JoinMulticastGroup (Int32, IPAddress)
604 [Category ("NotWorking")]
605 public void JoinMulticastGroup2_Socket_NotBound ()
607 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
609 UdpClient client = new UdpClient (AddressFamily.InterNetworkV6);
611 client.JoinMulticastGroup (0, mcast_addr);
613 } catch (SocketException ex) {
614 // An invalid argument was supplied
615 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
616 Assert.AreEqual (10022, ex.ErrorCode, "#3");
617 Assert.IsNull (ex.InnerException, "#4");
618 Assert.IsNotNull (ex.Message, "#5");
619 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
620 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
626 [Test] // JoinMulticastGroup (IPAddress, Int32)
627 public void JoinMulticastGroup3_IPv4 ()
629 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
631 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
632 client.JoinMulticastGroup (mcast_addr, 0);
635 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
636 client.JoinMulticastGroup (mcast_addr, 255);
640 [Test] // JoinMulticastGroup (IPAddress, Int32)
641 public void JoinMulticastGroup3_IPv6 ()
643 if (!Socket.OSSupportsIPv6)
644 Assert.Ignore ("IPv6 not enabled.");
646 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
648 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
649 client.JoinMulticastGroup (mcast_addr, 0);
652 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
653 client.JoinMulticastGroup (mcast_addr, 255);
657 [Test] // JoinMulticastGroup (IPAddress, Int32)
658 public void JoinMulticastGroup3_MulticastAddr_Null ()
660 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
662 client.JoinMulticastGroup ((IPAddress) null, int.MaxValue);
664 } catch (ArgumentNullException ex) {
665 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
666 Assert.IsNull (ex.InnerException, "#3");
667 Assert.IsNotNull (ex.Message, "#4");
668 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
673 [Test] // JoinMulticastGroup (IPAddress, Int32)
674 public void JoinMulticastGroup3_Socket_Closed ()
676 IPAddress mcast_addr = null;
678 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
681 client.JoinMulticastGroup (mcast_addr, 0);
683 } catch (ObjectDisposedException ex) {
684 // Cannot access a disposed object
685 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
686 Assert.IsNull (ex.InnerException, "#3");
687 Assert.IsNotNull (ex.Message, "#4");
688 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
692 [Test] // JoinMulticastGroup (IPAddress, Int32)
693 [Category ("NotWorking")]
694 public void JoinMulticastGroup3_Socket_NotBound ()
696 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
698 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
700 client.JoinMulticastGroup (mcast_addr, 5);
702 } catch (SocketException ex) {
703 // An invalid argument was supplied
704 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
705 Assert.AreEqual (10022, ex.ErrorCode, "#3");
706 Assert.IsNull (ex.InnerException, "#4");
707 Assert.IsNotNull (ex.Message, "#5");
708 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
709 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
715 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
716 public void JoinMulticastGroup4_IPv4 ()
718 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
719 IPAddress local_addr = IPAddress.Any;
721 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
722 client.JoinMulticastGroup (mcast_addr, local_addr);
726 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
727 public void JoinMulticastGroup4_IPv6 ()
729 if (!Socket.OSSupportsIPv6)
730 Assert.Ignore ("IPv6 not enabled.");
732 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
733 IPAddress local_addr = IPAddress.IPv6Any;
735 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
737 client.JoinMulticastGroup (mcast_addr, local_addr);
739 } catch (SocketException ex) {
740 // The attempted operation is not supported for the type of
742 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
743 Assert.AreEqual (10045, ex.ErrorCode, "#3");
744 Assert.IsNull (ex.InnerException, "#4");
745 Assert.IsNotNull (ex.Message, "#5");
746 Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
747 Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
752 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
753 public void JoinMulticastGroup4_LocalAddress_Null ()
755 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
757 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
759 client.JoinMulticastGroup (mcast_addr, (IPAddress) null);
761 } catch (ArgumentNullException ex) {
762 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
763 Assert.IsNull (ex.InnerException, "#3");
764 Assert.IsNotNull (ex.Message, "#4");
765 Assert.AreEqual ("mcint", ex.ParamName, "#5");
770 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
771 public void JoinMulticastGroup4_MulticastAddr_Null ()
773 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
775 client.JoinMulticastGroup ((IPAddress) null, IPAddress.Loopback);
777 } catch (ArgumentNullException ex) {
778 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
779 Assert.IsNull (ex.InnerException, "#3");
780 Assert.IsNotNull (ex.Message, "#4");
781 Assert.AreEqual ("group", ex.ParamName, "#5");
786 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
787 public void JoinMulticastGroup4_Socket_Closed ()
789 IPAddress mcast_addr = null;
790 IPAddress local_addr = null;
792 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
795 client.JoinMulticastGroup (mcast_addr, local_addr);
797 } catch (ObjectDisposedException ex) {
798 // Cannot access a disposed object
799 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
800 Assert.IsNull (ex.InnerException, "#3");
801 Assert.IsNotNull (ex.Message, "#4");
802 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
806 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
807 [Category ("NotWorking")]
808 public void JoinMulticastGroup4_Socket_NotBound ()
810 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
811 IPAddress local_addr = Dns.GetHostEntry (string.Empty).AddressList [0];
813 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
815 client.JoinMulticastGroup (mcast_addr, local_addr);
817 } catch (SocketException ex) {
818 // An invalid argument was supplied
819 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
820 Assert.AreEqual (10022, ex.ErrorCode, "#3");
821 Assert.IsNull (ex.InnerException, "#4");
822 Assert.IsNotNull (ex.Message, "#5");
823 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
824 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
831 public void CloseInReceive ()
833 UdpClient client = null;
834 var rnd = new Random ();
835 for (int i = 0, max = 5; i < max; i++) {
836 int port = rnd.Next (1025, 65534);
838 client = new UdpClient (port);
840 } catch (Exception) {
846 ManualResetEvent ready = new ManualResetEvent (false);
847 bool got_exc = false;
849 Task receive_task = Task.Factory.StartNew (() => {
850 IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
853 client.Receive(ref ep);
854 } catch (SocketException) {
861 ready.WaitOne (2000);
865 Assert.IsTrue (receive_task.Wait (1000));
866 Assert.IsTrue (got_exc);
869 // Test for bug 324033
871 public void JoinMulticastGroupWithLocal ()
873 UdpClient client = new UdpClient (9001);
874 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.24");
875 IPAddress local_addr = IPAddress.Any;
878 client.JoinMulticastGroup (mcast_addr, local_addr);
885 [ExpectedException (typeof(ArgumentNullException))]
886 public void BeginSendNull ()
888 UdpClient client = new UdpClient ();
889 client.BeginSend (null, 0, null, null);
893 static bool BSSent = false;
895 static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
897 private static void BSCallback (IAsyncResult asyncResult)
899 UdpClient client = (UdpClient)asyncResult.AsyncState;
901 BSBytes = client.EndSend (asyncResult);
908 public void BeginSend ()
910 UdpClient client = new UdpClient ();
911 byte[] bytes = new byte[] {10, 11, 12, 13};
914 client.BeginSend (bytes, bytes.Length, new AsyncCallback (BSCallback), client);
915 Assert.Fail ("BeginSend #1");
916 } catch (SocketException ex) {
917 Assert.AreEqual (10057, ex.ErrorCode,
922 client.BeginSend (bytes, bytes.Length, null, new AsyncCallback (BSCallback), client);
923 Assert.Fail ("BeginSend #3");
924 } catch (SocketException ex) {
925 Assert.AreEqual (10057, ex.ErrorCode,
929 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1236);
931 BSCalledBack.Reset ();
933 client.BeginSend (bytes, bytes.Length, ep,
934 new AsyncCallback (BSCallback),
936 if (BSCalledBack.WaitOne (2000, false) == false) {
937 Assert.Fail ("BeginSend wait timed out");
940 Assert.AreEqual (true, BSSent, "BeginSend #5");
941 Assert.AreEqual (4, BSBytes, "BeginSend #6");
946 static bool BRReceived = false;
947 static byte[] BRBytes;
948 static IPEndPoint BRFrom;
949 static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
951 private static void BRCallback (IAsyncResult asyncResult)
953 UdpClient client = (UdpClient)asyncResult.AsyncState;
955 BRBytes = client.EndReceive (asyncResult, ref BRFrom);
962 public void BeginReceive ()
964 UdpClient client = new UdpClient (1237);
966 BRCalledBack.Reset ();
968 client.BeginReceive (BRCallback, client);
970 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1237);
971 byte[] send_bytes = new byte[] {10, 11, 12, 13};
972 client.Send (send_bytes, send_bytes.Length, ep);
974 if (BRCalledBack.WaitOne (2000, false) == false) {
975 Assert.Fail ("BeginReceive wait timed out");
978 Assert.AreEqual (true, BRReceived, "BeginReceive #1");
979 Assert.AreEqual (4, BRBytes.Length, "BeginReceive #2");
980 Assert.AreEqual (ep. Port, BRFrom.Port, "BeginReceive #3");
981 Assert.AreEqual (ep.Address, BRFrom.Address, "BeginReceive #4");
987 public void Available ()
989 using (UdpClient client = new UdpClient (1238)) {
990 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1238);
991 byte[] bytes = new byte[] {10, 11, 12, 13};
993 int res = client.Send (bytes, bytes.Length, ep);
994 Assert.AreEqual (bytes.Length, res, "Send");
996 // that might happen too quickly, data sent and not yet received/available
998 int avail = client.Available;
1000 Assert.AreEqual (bytes.Length, avail, "Available #1");
1007 [Category ("NotWorking")] // Using PMTU settings workaround on Linux, default true
1008 public void DontFragmentDefault ()
1010 UdpClient client = new UdpClient ();
1012 /* Ignore the docs, testing shows the default
1013 * here is in fact false
1015 Assert.AreEqual (false, client.DontFragment, "DontFragmentDefault");
1021 public void EnableBroadcastDefault ()
1023 UdpClient client = new UdpClient ();
1025 Assert.AreEqual (false, client.EnableBroadcast, "EnableBroadcastDefault");
1030 /* Can't test the default for ExclusiveAddressUse as
1031 * it's different on different versions and service
1035 [Category ("NotWorking")] // Not supported on Linux
1036 public void ExclusiveAddressUseUnbound ()
1038 UdpClient client = new UdpClient ();
1040 client.ExclusiveAddressUse = true;
1042 Assert.AreEqual (true, client.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
1048 [ExpectedException (typeof(InvalidOperationException))]
1049 [Category ("NotWorking")] // Not supported on Linux
1050 public void ExclusiveAddressUseBound ()
1052 UdpClient client = new UdpClient (1239);
1054 client.ExclusiveAddressUse = true;
1060 public void MulticastLoopbackDefault ()
1062 UdpClient client = new UdpClient ();
1064 Assert.AreEqual (true, client.MulticastLoopback, "MulticastLoopbackDefault");
1070 public void ReceiveIPv6 ()
1072 if (!Socket.OSSupportsIPv6)
1073 Assert.Ignore ("IPv6 not enabled.");
1076 using(var udpClient = new UdpClient (PORT, AddressFamily.InterNetworkV6))
1077 using(var udpClient2 = new UdpClient (PORT+1, AddressFamily.InterNetworkV6))
1079 var dataSent = new byte [] {1,2,3};
1080 udpClient2.SendAsync (dataSent, dataSent.Length, "::1", PORT);
1082 IPEndPoint endPoint = new IPEndPoint (IPAddress.IPv6Any, 0);
1083 var data = udpClient.Receive (ref endPoint);
1085 Assert.AreEqual (dataSent.Length, data.Length);
1089 /* No test for Ttl default as it is platform dependent */
1091 class MyUdpClient : UdpClient
1093 public MyUdpClient ()
1097 public MyUdpClient (AddressFamily family)
1102 public MyUdpClient (Int32 port)
1108 public MyUdpClient (IPEndPoint localEP)
1113 public MyUdpClient (int port, AddressFamily family)
1114 : base (port, family)
1118 public MyUdpClient (string hostname, int port)
1119 : base (hostname, port)
1123 public new bool Active {
1124 get { return base.Active; }
1125 set { base.Active = value; }