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 public class UdpClientTest {
20 #if FEATURE_NO_BSD_SOCKETS
21 [ExpectedException (typeof (PlatformNotSupportedException))]
23 public void Constructor1 ()
28 client = new MyUdpClient ();
30 Assert.IsNotNull (s, "Client");
31 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "Client:AddressFamily");
32 Assert.IsFalse (s.Connected, "Client:Connected");
33 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
34 Assert.IsNull (s.LocalEndPoint, "Client:LocalEndPoint");
35 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "Client:ProtocolType");
36 Assert.IsNull (s.RemoteEndPoint, "Client:RemoteEndPoint");
37 Assert.AreEqual (SocketType.Dgram, s.SocketType, "Client:SocketType");
38 Assert.IsFalse (client.Active, "Active");
39 Assert.IsFalse (client.DontFragment, "DontFragment");
40 Assert.IsFalse (client.EnableBroadcast, "EnableBroadcast");
41 //Assert.IsFalse (client.ExclusiveAddressUse, "ExclusiveAddressUse");
42 Assert.IsTrue (client.MulticastLoopback, "MulticastLoopback");
43 //Assert.AreEqual (32, client.Ttl, "Ttl");
47 [Test] // .ctor (AddressFamily)
48 #if FEATURE_NO_BSD_SOCKETS
49 [ExpectedException (typeof (PlatformNotSupportedException))]
51 public void Constructor2 ()
56 client = new MyUdpClient (AddressFamily.InterNetwork);
58 Assert.IsNotNull (s, "#A:Client");
59 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
60 Assert.IsFalse (s.Connected, "#A:Client:Connected");
61 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
62 Assert.IsNull (s.LocalEndPoint, "#A:Client:LocalEndPoint");
63 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
64 Assert.IsNull (s.RemoteEndPoint, "#A:Client:RemoteEndPoint");
65 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
66 Assert.IsFalse (client.Active, "#A:Active");
67 //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
68 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
69 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
70 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
71 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
73 if (!Socket.OSSupportsIPv6)
74 Assert.Ignore ("IPv6 not enabled.");
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");
81 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
82 Assert.IsNull (s.LocalEndPoint, "#B:Client:LocalEndPoint");
83 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
84 Assert.IsNull (s.RemoteEndPoint, "#B:Client:RemoteEndPoint");
85 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
86 Assert.IsFalse (client.Active, "#B:Active");
87 //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
88 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
89 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
90 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
91 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
95 [Test] // .ctor (AddressFamily)
96 #if FEATURE_NO_BSD_SOCKETS
97 [ExpectedException (typeof (PlatformNotSupportedException))]
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");
107 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
109 Assert.IsNotNull (ex.Message, "#A4");
110 Assert.AreEqual ("family", ex.ParamName, "#A5");
114 new UdpClient ((AddressFamily) 666);
116 } catch (ArgumentException ex) {
117 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
118 Assert.IsNull (ex.InnerException, "#B3");
119 Assert.IsNotNull (ex.Message, "#B4");
120 Assert.AreEqual ("family", ex.ParamName, "#B5");
124 [Test] // .ctor (Int32)
125 #if FEATURE_NO_BSD_SOCKETS
126 [ExpectedException (typeof (PlatformNotSupportedException))]
128 public void Constructor3 ()
133 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort))
136 Assert.IsNotNull (s, "#A:Client");
137 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
138 Assert.IsFalse (s.Connected, "#A:Client:Connected");
139 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
140 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
141 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
142 Assert.IsFalse (client.Active, "#A:Active");
143 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
144 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
145 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
146 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
147 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
148 localEP = s.LocalEndPoint as IPEndPoint;
149 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
150 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
151 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
153 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort))
156 Assert.IsNotNull (s, "#B:Client");
157 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
158 Assert.IsFalse (s.Connected, "#B:Client:Connected");
159 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
160 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
161 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
162 Assert.IsFalse (client.Active, "#B:Active");
163 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
164 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
165 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
166 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
167 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
168 localEP = s.LocalEndPoint as IPEndPoint;
169 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
170 Assert.AreEqual (IPAddress.Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
171 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
172 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
176 [Test] // .ctor (Int32)
177 #if FEATURE_NO_BSD_SOCKETS
178 [ExpectedException (typeof (PlatformNotSupportedException))]
180 public void Constructor3_Port_OutOfRange ()
183 new UdpClient (IPEndPoint.MaxPort + 1);
185 } catch (ArgumentOutOfRangeException ex) {
186 // Specified argument was out of the range of valid values
187 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
188 Assert.IsNull (ex.InnerException, "#A3");
189 Assert.IsNotNull (ex.Message, "#A4");
190 Assert.AreEqual ("port", ex.ParamName, "#A5");
194 new UdpClient (IPEndPoint.MinPort - 1);
196 } catch (ArgumentOutOfRangeException ex) {
197 // Specified argument was out of the range of valid values
198 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
199 Assert.IsNull (ex.InnerException, "#A3");
200 Assert.IsNotNull (ex.Message, "#A4");
201 Assert.AreEqual ("port", ex.ParamName, "#A5");
205 [Test] // .ctor (IPEndPoint)
206 #if FEATURE_NO_BSD_SOCKETS
207 [ExpectedException (typeof (PlatformNotSupportedException))]
209 public void Constructor4 ()
215 clientEP = new IPEndPoint (IPAddress.Loopback, 8001);
216 using (MyUdpClient client = new MyUdpClient (clientEP))
219 Assert.IsNotNull (s, "#A:Client");
220 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
221 Assert.IsFalse (s.Connected, "#A:Client:Connected");
222 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
223 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
224 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
225 Assert.IsFalse (client.Active, "#A:Active");
226 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
227 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
228 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
229 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
230 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
231 localEP = s.LocalEndPoint as IPEndPoint;
232 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
233 Assert.IsFalse (object.ReferenceEquals (clientEP, localEP), "#A:Client:LocalEndPoint/ReferenceEquality");
234 Assert.AreEqual (clientEP.Address, localEP.Address, "#A:Client:LocalEndPoint/Address");
235 Assert.AreEqual (clientEP.AddressFamily, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
236 Assert.AreEqual (clientEP.Port, localEP.Port, "#A:Client:LocalEndPoint/Port");
240 [Test] // .ctor (IPEndPoint)
241 #if FEATURE_NO_BSD_SOCKETS
242 [ExpectedException (typeof (PlatformNotSupportedException))]
244 public void Constructor4_LocalEP_Null ()
247 new UdpClient ((IPEndPoint) null);
249 } catch (ArgumentNullException ex) {
250 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
251 Assert.IsNull (ex.InnerException, "#3");
252 Assert.IsNotNull (ex.Message, "#4");
253 Assert.AreEqual ("localEP", ex.ParamName, "#5");
257 [Test] // .ctor (Int32, AddressFamily)
258 #if FEATURE_NO_BSD_SOCKETS
259 [ExpectedException (typeof (PlatformNotSupportedException))]
261 public void Constructor5 ()
266 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort, AddressFamily.InterNetwork))
269 Assert.IsNotNull (s, "#A:Client");
270 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
271 Assert.IsFalse (s.Connected, "#A:Client:Connected");
272 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
273 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
274 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
275 Assert.IsFalse (client.Active, "#A:Active");
276 //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
277 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
278 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
279 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
280 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
281 localEP = s.LocalEndPoint as IPEndPoint;
282 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
283 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
284 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
287 if (!Socket.OSSupportsIPv6)
288 Assert.Ignore ("IPv6 not enabled.");
290 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort, AddressFamily.InterNetworkV6))
293 Assert.IsNotNull (s, "#B:Client");
294 Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
295 Assert.IsFalse (s.Connected, "#B:Client:Connected");
296 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
297 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
298 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
299 Assert.IsFalse (client.Active, "#B:Active");
300 //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
301 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
302 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
303 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
304 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
305 localEP = s.LocalEndPoint as IPEndPoint;
306 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
307 Assert.AreEqual (IPAddress.IPv6Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
308 Assert.AreEqual (AddressFamily.InterNetworkV6, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
309 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
313 [Test] // .ctor (Int32, AddressFamily)
314 #if FEATURE_NO_BSD_SOCKETS
315 [ExpectedException (typeof (PlatformNotSupportedException))]
317 public void Constructor5_Family_Invalid ()
320 new UdpClient (80, AddressFamily.NetBios);
322 } catch (ArgumentException ex) {
324 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
325 Assert.IsNull (ex.InnerException, "#A3");
326 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
328 Assert.IsNotNull (ex.Message, "#A4");
329 Assert.AreEqual ("family", ex.ParamName, "#A5");
333 new UdpClient (80, (AddressFamily) 666);
335 } catch (ArgumentException ex) {
337 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
338 Assert.IsNull (ex.InnerException, "#B3");
339 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
341 Assert.IsNotNull (ex.Message, "#B4");
342 Assert.AreEqual ("family", ex.ParamName, "#B5");
346 [Test] // .ctor (Int32, AddressFamily)
347 #if FEATURE_NO_BSD_SOCKETS
348 [ExpectedException (typeof (PlatformNotSupportedException))]
350 public void Constructor5_Port_OutOfRange ()
353 new UdpClient (IPEndPoint.MaxPort + 1, AddressFamily.InterNetwork);
355 } catch (ArgumentOutOfRangeException ex) {
356 // Specified argument was out of the range of valid values
357 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
358 Assert.IsNull (ex.InnerException, "#A3");
359 Assert.IsNotNull (ex.Message, "#A4");
360 Assert.AreEqual ("port", ex.ParamName, "#A5");
364 new UdpClient (IPEndPoint.MinPort - 1, AddressFamily.InterNetwork);
366 } catch (ArgumentOutOfRangeException ex) {
367 // Specified argument was out of the range of valid values
368 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
369 Assert.IsNull (ex.InnerException, "#A3");
370 Assert.IsNotNull (ex.Message, "#A4");
371 Assert.AreEqual ("port", ex.ParamName, "#A5");
375 [Test] // .ctor (String, Int32)
376 #if FEATURE_NO_BSD_SOCKETS
377 [ExpectedException (typeof (PlatformNotSupportedException))]
379 public void Constructor6 ()
385 // UDP port 0 doesn't seem to be valid.
386 using (MyUdpClient client = new MyUdpClient ("127.0.0.1", 53))
389 Assert.IsNotNull (s, "#A:Client");
390 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
391 Assert.IsTrue (s.Connected, "#A:Client:Connected");
392 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
393 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
394 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
395 Assert.IsTrue (client.Active, "#A:Active");
396 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
397 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
398 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
399 //Assert.IsFalse (client.MulticastLoopback, "#A:MulticastLoopback");
400 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
401 localEP = s.LocalEndPoint as IPEndPoint;
402 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
403 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#A:Client:LocalEndPoint/Address");
404 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
406 using (MyUdpClient client = new MyUdpClient ("127.0.0.1", IPEndPoint.MaxPort))
409 Assert.IsNotNull (s, "#B:Client");
410 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
411 Assert.IsTrue (s.Connected, "#B:Client:Connected");
412 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
413 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
414 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
415 Assert.IsTrue (client.Active, "#B:Active");
416 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
417 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
418 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
419 //Assert.IsFalse (client.MulticastLoopback, "#B:MulticastLoopback");
420 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
421 localEP = s.LocalEndPoint as IPEndPoint;
422 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
423 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#B:Client:LocalEndPoint/Address");
424 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
428 [Test] // .ctor (String, Int32)
429 #if FEATURE_NO_BSD_SOCKETS
430 [ExpectedException (typeof (PlatformNotSupportedException))]
432 public void Constructor6_HostName_Null ()
435 new UdpClient ((string) null, int.MaxValue);
437 } catch (ArgumentNullException ex) {
438 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
439 Assert.IsNull (ex.InnerException, "#3");
440 Assert.IsNotNull (ex.Message, "#4");
441 Assert.AreEqual ("hostname", ex.ParamName, "#5");
445 [Test] // .ctor (String, Int32)
446 #if FEATURE_NO_BSD_SOCKETS
447 [ExpectedException (typeof (PlatformNotSupportedException))]
449 public void Constructor6_Port_OutOfRange ()
452 new UdpClient ("local", IPEndPoint.MaxPort + 1);
454 } catch (ArgumentOutOfRangeException ex) {
455 // Specified argument was out of the range of valid values
456 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
457 Assert.IsNull (ex.InnerException, "#A3");
458 Assert.IsNotNull (ex.Message, "#A4");
459 Assert.AreEqual ("port", ex.ParamName, "#A5");
463 new UdpClient ("local", IPEndPoint.MinPort - 1);
465 } catch (ArgumentOutOfRangeException ex) {
466 // Specified argument was out of the range of valid values
467 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
468 Assert.IsNull (ex.InnerException, "#A3");
469 Assert.IsNotNull (ex.Message, "#A4");
470 Assert.AreEqual ("port", ex.ParamName, "#A5");
475 #if FEATURE_NO_BSD_SOCKETS
476 [ExpectedException (typeof (PlatformNotSupportedException))]
478 public void UdpClientBroadcastTest ()
480 UdpClient client = new UdpClient ();
481 byte[] bytes = new byte[] {10, 11, 12, 13};
484 client.Send (bytes, bytes.Length, new IPEndPoint (IPAddress.Broadcast, 1235));
490 [Test] // JoinMulticastGroup (IPAddress)
491 #if FEATURE_NO_BSD_SOCKETS
492 [ExpectedException (typeof (PlatformNotSupportedException))]
494 public void JoinMulticastGroup1_IPv4 ()
496 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
498 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
499 client.JoinMulticastGroup (mcast_addr);
503 [Test] // JoinMulticastGroup (IPAddress)
504 #if FEATURE_NO_BSD_SOCKETS
505 [ExpectedException (typeof (PlatformNotSupportedException))]
507 public void JoinMulticastGroup1_IPv6 ()
509 if (!Socket.OSSupportsIPv6)
510 Assert.Ignore ("IPv6 not enabled.");
512 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
514 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
515 client.JoinMulticastGroup (mcast_addr);
519 [Test] // JoinMulticastGroup (IPAddress)
520 #if FEATURE_NO_BSD_SOCKETS
521 [ExpectedException (typeof (PlatformNotSupportedException))]
523 public void JoinMulticastGroup1_MulticastAddr_Null ()
525 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
527 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");
538 [Test] // JoinMulticastGroup (IPAddress)
539 #if FEATURE_NO_BSD_SOCKETS
540 [ExpectedException (typeof (PlatformNotSupportedException))]
542 public void JoinMulticastGroup1_Socket_Closed ()
544 IPAddress mcast_addr = null;
546 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
549 client.JoinMulticastGroup (mcast_addr);
551 } catch (ObjectDisposedException ex) {
552 // Cannot access a disposed object
553 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
554 Assert.IsNull (ex.InnerException, "#3");
555 Assert.IsNotNull (ex.Message, "#4");
556 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
560 [Test] // JoinMulticastGroup (IPAddress)
561 #if FEATURE_NO_BSD_SOCKETS
562 [ExpectedException (typeof (PlatformNotSupportedException))]
564 public void JoinMulticastGroup1_Socket_NotBound ()
566 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
568 using (UdpClient client = new UdpClient (AddressFamily.InterNetwork)) {
569 client.JoinMulticastGroup (mcast_addr);
573 [Test] // JoinMulticastGroup (In32, IPAddress)
574 #if FEATURE_NO_BSD_SOCKETS
575 [ExpectedException (typeof (PlatformNotSupportedException))]
577 public void JoinMulticastGroup2_IPv4 ()
579 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
581 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
583 client.JoinMulticastGroup (0, mcast_addr);
585 } catch (SocketException ex) {
586 // The attempted operation is not supported for the type of
588 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
589 Assert.AreEqual (10045, ex.ErrorCode, "#3");
590 Assert.IsNull (ex.InnerException, "#4");
591 Assert.IsNotNull (ex.Message, "#5");
592 Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
593 Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
598 [Test] // JoinMulticastGroup (In32, IPAddress)
599 #if FEATURE_NO_BSD_SOCKETS
600 [ExpectedException (typeof (PlatformNotSupportedException))]
602 public void JoinMulticastGroup2_IPv6 ()
604 if (!Socket.OSSupportsIPv6)
605 Assert.Ignore ("IPv6 not enabled.");
607 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
609 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
610 client.JoinMulticastGroup (0, mcast_addr);
614 [Test] // JoinMulticastGroup (Int32, IPAddress)
615 #if FEATURE_NO_BSD_SOCKETS
616 [ExpectedException (typeof (PlatformNotSupportedException))]
618 public void JoinMulticastGroup2_MulticastAddr_Null ()
620 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
622 client.JoinMulticastGroup (0, (IPAddress) null);
624 } catch (ArgumentNullException ex) {
625 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
626 Assert.IsNull (ex.InnerException, "#3");
627 Assert.IsNotNull (ex.Message, "#4");
628 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
633 [Test] // JoinMulticastGroup (Int32, IPAddress)
634 #if FEATURE_NO_BSD_SOCKETS
635 [ExpectedException (typeof (PlatformNotSupportedException))]
637 public void JoinMulticastGroup2_Socket_Closed ()
639 if (!Socket.OSSupportsIPv6)
640 Assert.Ignore ("IPv6 not enabled.");
642 IPAddress mcast_addr = null;
644 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234));
647 client.JoinMulticastGroup (0, mcast_addr);
649 } catch (ObjectDisposedException ex) {
650 // Cannot access a disposed object
651 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
652 Assert.IsNull (ex.InnerException, "#3");
653 Assert.IsNotNull (ex.Message, "#4");
654 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
658 [Test] // JoinMulticastGroup (Int32, IPAddress)
659 #if FEATURE_NO_BSD_SOCKETS
660 [ExpectedException (typeof (PlatformNotSupportedException))]
662 public void JoinMulticastGroup2_Socket_NotBound ()
664 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
666 using (UdpClient client = new UdpClient (AddressFamily.InterNetworkV6)) {
667 client.JoinMulticastGroup (0, mcast_addr);
671 [Test] // JoinMulticastGroup (IPAddress, Int32)
672 #if FEATURE_NO_BSD_SOCKETS
673 [ExpectedException (typeof (PlatformNotSupportedException))]
675 public void JoinMulticastGroup3_IPv4 ()
677 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
679 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
680 client.JoinMulticastGroup (mcast_addr, 0);
683 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
684 client.JoinMulticastGroup (mcast_addr, 255);
688 [Test] // JoinMulticastGroup (IPAddress, Int32)
689 #if FEATURE_NO_BSD_SOCKETS
690 [ExpectedException (typeof (PlatformNotSupportedException))]
692 public void JoinMulticastGroup3_IPv6 ()
694 if (!Socket.OSSupportsIPv6)
695 Assert.Ignore ("IPv6 not enabled.");
697 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
699 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
700 client.JoinMulticastGroup (mcast_addr, 0);
703 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
704 client.JoinMulticastGroup (mcast_addr, 255);
708 [Test] // JoinMulticastGroup (IPAddress, Int32)
709 #if FEATURE_NO_BSD_SOCKETS
710 [ExpectedException (typeof (PlatformNotSupportedException))]
712 public void JoinMulticastGroup3_MulticastAddr_Null ()
714 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
716 client.JoinMulticastGroup ((IPAddress) null, int.MaxValue);
718 } catch (ArgumentNullException ex) {
719 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
720 Assert.IsNull (ex.InnerException, "#3");
721 Assert.IsNotNull (ex.Message, "#4");
722 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
727 [Test] // JoinMulticastGroup (IPAddress, Int32)
728 #if FEATURE_NO_BSD_SOCKETS
729 [ExpectedException (typeof (PlatformNotSupportedException))]
731 public void JoinMulticastGroup3_Socket_Closed ()
733 IPAddress mcast_addr = null;
735 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
738 client.JoinMulticastGroup (mcast_addr, 0);
740 } catch (ObjectDisposedException ex) {
741 // Cannot access a disposed object
742 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
743 Assert.IsNull (ex.InnerException, "#3");
744 Assert.IsNotNull (ex.Message, "#4");
745 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
749 [Test] // JoinMulticastGroup (IPAddress, Int32)
750 #if FEATURE_NO_BSD_SOCKETS
751 [ExpectedException (typeof (PlatformNotSupportedException))]
753 public void JoinMulticastGroup3_Socket_NotBound ()
755 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
757 using (UdpClient client = new UdpClient (AddressFamily.InterNetwork)) {
758 client.JoinMulticastGroup (mcast_addr, 5);
762 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
763 #if FEATURE_NO_BSD_SOCKETS
764 [ExpectedException (typeof (PlatformNotSupportedException))]
766 public void JoinMulticastGroup4_IPv4 ()
768 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
769 IPAddress local_addr = IPAddress.Any;
771 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
772 client.JoinMulticastGroup (mcast_addr, local_addr);
776 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
777 #if FEATURE_NO_BSD_SOCKETS
778 [ExpectedException (typeof (PlatformNotSupportedException))]
780 public void JoinMulticastGroup4_IPv6 ()
782 if (!Socket.OSSupportsIPv6)
783 Assert.Ignore ("IPv6 not enabled.");
785 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
786 IPAddress local_addr = IPAddress.IPv6Any;
788 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
790 client.JoinMulticastGroup (mcast_addr, local_addr);
792 } catch (SocketException ex) {
793 // The attempted operation is not supported for the type of
795 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
796 Assert.AreEqual (10045, ex.ErrorCode, "#3");
797 Assert.IsNull (ex.InnerException, "#4");
798 Assert.IsNotNull (ex.Message, "#5");
799 Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
800 Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
805 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
806 #if FEATURE_NO_BSD_SOCKETS
807 [ExpectedException (typeof (PlatformNotSupportedException))]
809 public void JoinMulticastGroup4_LocalAddress_Null ()
811 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
813 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
815 client.JoinMulticastGroup (mcast_addr, (IPAddress) null);
817 } catch (ArgumentNullException ex) {
818 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
819 Assert.IsNull (ex.InnerException, "#3");
820 Assert.IsNotNull (ex.Message, "#4");
821 Assert.AreEqual ("mcint", ex.ParamName, "#5");
826 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
827 #if FEATURE_NO_BSD_SOCKETS
828 [ExpectedException (typeof (PlatformNotSupportedException))]
830 public void JoinMulticastGroup4_MulticastAddr_Null ()
832 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
834 client.JoinMulticastGroup ((IPAddress) null, IPAddress.Loopback);
836 } catch (ArgumentNullException ex) {
837 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
838 Assert.IsNull (ex.InnerException, "#3");
839 Assert.IsNotNull (ex.Message, "#4");
840 Assert.AreEqual ("group", ex.ParamName, "#5");
845 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
846 #if FEATURE_NO_BSD_SOCKETS
847 [ExpectedException (typeof (PlatformNotSupportedException))]
849 public void JoinMulticastGroup4_Socket_Closed ()
851 IPAddress mcast_addr = null;
852 IPAddress local_addr = null;
854 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
857 client.JoinMulticastGroup (mcast_addr, local_addr);
859 } catch (ObjectDisposedException ex) {
860 // Cannot access a disposed object
861 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
862 Assert.IsNull (ex.InnerException, "#3");
863 Assert.IsNotNull (ex.Message, "#4");
864 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
868 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
869 #if FEATURE_NO_BSD_SOCKETS
870 [ExpectedException (typeof (PlatformNotSupportedException))]
872 public void JoinMulticastGroup4_Socket_NotBound ()
874 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
875 IPAddress local_addr = Dns.GetHostEntry (string.Empty).AddressList [0];
877 using (UdpClient client = new UdpClient (AddressFamily.InterNetwork)) {
878 client.JoinMulticastGroup (mcast_addr, local_addr);
883 #if FEATURE_NO_BSD_SOCKETS
884 [ExpectedException (typeof (PlatformNotSupportedException))]
886 public void CloseInReceive ()
888 UdpClient client = null;
889 var rnd = new Random ();
890 for (int i = 0, max = 5; i < max; i++) {
891 int port = rnd.Next (1025, 65534);
893 client = new UdpClient (port);
895 } catch (Exception) {
901 ManualResetEvent ready = new ManualResetEvent (false);
902 bool got_exc = false;
904 Task receive_task = Task.Factory.StartNew (() => {
905 IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
908 client.Receive(ref ep);
909 } catch (SocketException) {
916 ready.WaitOne (2000);
920 Assert.IsTrue (receive_task.Wait (1000));
921 Assert.IsTrue (got_exc);
924 // Test for bug 324033
926 #if FEATURE_NO_BSD_SOCKETS
927 [ExpectedException (typeof (PlatformNotSupportedException))]
929 public void JoinMulticastGroupWithLocal ()
931 UdpClient client = new UdpClient (9001);
932 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.24");
933 IPAddress local_addr = IPAddress.Any;
936 client.JoinMulticastGroup (mcast_addr, local_addr);
943 #if FEATURE_NO_BSD_SOCKETS
944 [ExpectedException (typeof (PlatformNotSupportedException))]
946 [ExpectedException (typeof(ArgumentNullException))]
948 public void BeginSendNull ()
950 UdpClient client = new UdpClient ();
951 client.BeginSend (null, 0, null, null);
955 static bool BSSent = false;
957 static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
959 private static void BSCallback (IAsyncResult asyncResult)
961 UdpClient client = (UdpClient)asyncResult.AsyncState;
963 BSBytes = client.EndSend (asyncResult);
970 #if FEATURE_NO_BSD_SOCKETS
971 [ExpectedException (typeof (PlatformNotSupportedException))]
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 IPAddress[] addresses = Dns.GetHostEntry (string.Empty).AddressList;
995 IPEndPoint ep = null;
996 foreach (IPAddress a in addresses) {
997 if (a.AddressFamily == AddressFamily.InterNetwork) {
998 ep = new IPEndPoint (a, 1236);
1002 BSCalledBack.Reset ();
1004 client.BeginSend (bytes, bytes.Length, ep,
1005 new AsyncCallback (BSCallback),
1007 if (BSCalledBack.WaitOne (2000, false) == false) {
1008 Assert.Fail ("BeginSend wait timed out");
1011 Assert.AreEqual (true, BSSent, "BeginSend #5");
1012 Assert.AreEqual (4, BSBytes, "BeginSend #6");
1017 static bool BRReceived = false;
1018 static byte[] BRBytes;
1019 static IPEndPoint BRFrom;
1020 static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
1022 private static void BRCallback (IAsyncResult asyncResult)
1024 UdpClient client = (UdpClient)asyncResult.AsyncState;
1026 BRBytes = client.EndReceive (asyncResult, ref BRFrom);
1029 BRCalledBack.Set ();
1033 #if FEATURE_NO_BSD_SOCKETS
1034 [ExpectedException (typeof (PlatformNotSupportedException))]
1036 public void BeginReceive ()
1038 UdpClient client = new UdpClient (1237);
1040 BRCalledBack.Reset ();
1042 client.BeginReceive (BRCallback, client);
1044 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1237);
1045 byte[] send_bytes = new byte[] {10, 11, 12, 13};
1046 client.Send (send_bytes, send_bytes.Length, ep);
1048 if (BRCalledBack.WaitOne (2000, false) == false) {
1049 Assert.Fail ("BeginReceive wait timed out");
1052 Assert.AreEqual (true, BRReceived, "BeginReceive #1");
1053 Assert.AreEqual (4, BRBytes.Length, "BeginReceive #2");
1054 Assert.AreEqual (ep. Port, BRFrom.Port, "BeginReceive #3");
1055 Assert.AreEqual (ep.Address, BRFrom.Address, "BeginReceive #4");
1061 #if FEATURE_NO_BSD_SOCKETS
1062 [ExpectedException (typeof (PlatformNotSupportedException))]
1064 public void Available ()
1066 using (UdpClient client = new UdpClient (1238)) {
1067 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1238);
1068 byte[] bytes = new byte[] {10, 11, 12, 13};
1070 int res = client.Send (bytes, bytes.Length, ep);
1071 Assert.AreEqual (bytes.Length, res, "Send");
1073 // that might happen too quickly, data sent and not yet received/available
1075 int avail = client.Available;
1077 Assert.AreEqual (bytes.Length, avail, "Available #1");
1084 [Category ("NotWorking")] // Using PMTU settings workaround on Linux, default true
1085 public void DontFragmentDefault ()
1087 UdpClient client = new UdpClient ();
1089 /* Ignore the docs, testing shows the default
1090 * here is in fact false
1092 Assert.AreEqual (false, client.DontFragment, "DontFragmentDefault");
1098 #if FEATURE_NO_BSD_SOCKETS
1099 [ExpectedException (typeof (PlatformNotSupportedException))]
1101 public void EnableBroadcastDefault ()
1103 UdpClient client = new UdpClient ();
1105 Assert.AreEqual (false, client.EnableBroadcast, "EnableBroadcastDefault");
1110 /* Can't test the default for ExclusiveAddressUse as
1111 * it's different on different versions and service
1115 [Category ("NotWorking")] // Not supported on Linux
1116 public void ExclusiveAddressUseUnbound ()
1118 UdpClient client = new UdpClient ();
1120 client.ExclusiveAddressUse = true;
1122 Assert.AreEqual (true, client.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
1128 [ExpectedException (typeof(InvalidOperationException))]
1129 [Category ("NotWorking")] // Not supported on Linux
1130 public void ExclusiveAddressUseBound ()
1132 UdpClient client = new UdpClient (1239);
1134 client.ExclusiveAddressUse = true;
1140 #if FEATURE_NO_BSD_SOCKETS
1141 [ExpectedException (typeof (PlatformNotSupportedException))]
1143 public void MulticastLoopbackDefault ()
1145 UdpClient client = new UdpClient ();
1147 Assert.AreEqual (true, client.MulticastLoopback, "MulticastLoopbackDefault");
1153 #if FEATURE_NO_BSD_SOCKETS
1154 [ExpectedException (typeof (PlatformNotSupportedException))]
1156 public void ReceiveIPv6 ()
1158 if (!Socket.OSSupportsIPv6)
1159 Assert.Ignore ("IPv6 not enabled.");
1162 using(var udpClient = new UdpClient (PORT, AddressFamily.InterNetworkV6))
1163 using(var udpClient2 = new UdpClient (PORT+1, AddressFamily.InterNetworkV6))
1165 var dataSent = new byte [] {1,2,3};
1166 udpClient2.SendAsync (dataSent, dataSent.Length, "::1", PORT);
1168 IPEndPoint endPoint = new IPEndPoint (IPAddress.IPv6Any, 0);
1169 var data = udpClient.Receive (ref endPoint);
1171 Assert.AreEqual (dataSent.Length, data.Length);
1175 /* No test for Ttl default as it is platform dependent */
1177 class MyUdpClient : UdpClient
1179 public MyUdpClient ()
1183 public MyUdpClient (AddressFamily family)
1188 public MyUdpClient (Int32 port)
1194 public MyUdpClient (IPEndPoint localEP)
1199 public MyUdpClient (int port, AddressFamily family)
1200 : base (port, family)
1204 public MyUdpClient (string hostname, int port)
1205 : base (hostname, port)
1209 public new bool Active {
1210 get { return base.Active; }
1211 set { base.Active = value; }