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 public void Constructor1 ()
25 client = new MyUdpClient ();
27 Assert.IsNotNull (s, "Client");
28 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "Client:AddressFamily");
29 Assert.IsFalse (s.Connected, "Client:Connected");
30 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
31 Assert.IsNull (s.LocalEndPoint, "Client:LocalEndPoint");
32 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "Client:ProtocolType");
33 Assert.IsNull (s.RemoteEndPoint, "Client:RemoteEndPoint");
34 Assert.AreEqual (SocketType.Dgram, s.SocketType, "Client:SocketType");
35 Assert.IsFalse (client.Active, "Active");
36 Assert.IsFalse (client.DontFragment, "DontFragment");
37 Assert.IsFalse (client.EnableBroadcast, "EnableBroadcast");
38 //Assert.IsFalse (client.ExclusiveAddressUse, "ExclusiveAddressUse");
39 Assert.IsTrue (client.MulticastLoopback, "MulticastLoopback");
40 //Assert.AreEqual (32, client.Ttl, "Ttl");
44 [Test] // .ctor (AddressFamily)
45 public void Constructor2 ()
50 client = new MyUdpClient (AddressFamily.InterNetwork);
52 Assert.IsNotNull (s, "#A:Client");
53 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
54 Assert.IsFalse (s.Connected, "#A:Client:Connected");
55 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
56 Assert.IsNull (s.LocalEndPoint, "#A:Client:LocalEndPoint");
57 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
58 Assert.IsNull (s.RemoteEndPoint, "#A:Client:RemoteEndPoint");
59 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
60 Assert.IsFalse (client.Active, "#A:Active");
61 //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
62 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
63 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
64 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
65 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
67 if (!Socket.OSSupportsIPv6)
68 Assert.Ignore ("IPv6 not enabled.");
70 client = new MyUdpClient (AddressFamily.InterNetworkV6);
72 Assert.IsNotNull (s, "#B:Client");
73 Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
74 Assert.IsFalse (s.Connected, "#B:Client:Connected");
75 Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
76 Assert.IsNull (s.LocalEndPoint, "#B:Client:LocalEndPoint");
77 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
78 Assert.IsNull (s.RemoteEndPoint, "#B:Client:RemoteEndPoint");
79 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
80 Assert.IsFalse (client.Active, "#B:Active");
81 //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
82 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
83 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
84 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
85 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
89 [Test] // .ctor (AddressFamily)
90 public void Constructor2_Family_Invalid ()
93 new UdpClient (AddressFamily.NetBios);
95 } catch (ArgumentException ex) {
96 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
97 Assert.IsNull (ex.InnerException, "#A3");
98 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
100 Assert.IsNotNull (ex.Message, "#A4");
101 Assert.AreEqual ("family", ex.ParamName, "#A5");
105 new UdpClient ((AddressFamily) 666);
107 } catch (ArgumentException ex) {
108 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
109 Assert.IsNull (ex.InnerException, "#B3");
110 Assert.IsNotNull (ex.Message, "#B4");
111 Assert.AreEqual ("family", ex.ParamName, "#B5");
115 [Test] // .ctor (Int32)
116 public void Constructor3 ()
121 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort))
124 Assert.IsNotNull (s, "#A:Client");
125 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
126 Assert.IsFalse (s.Connected, "#A:Client:Connected");
127 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
128 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
129 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
130 Assert.IsFalse (client.Active, "#A:Active");
131 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
132 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
133 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
134 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
135 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
136 localEP = s.LocalEndPoint as IPEndPoint;
137 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
138 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
139 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
141 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort))
144 Assert.IsNotNull (s, "#B:Client");
145 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
146 Assert.IsFalse (s.Connected, "#B:Client:Connected");
147 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
148 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
149 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
150 Assert.IsFalse (client.Active, "#B:Active");
151 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
152 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
153 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
154 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
155 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
156 localEP = s.LocalEndPoint as IPEndPoint;
157 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
158 Assert.AreEqual (IPAddress.Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
159 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
160 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
164 [Test] // .ctor (Int32)
165 public void Constructor3_Port_OutOfRange ()
168 new UdpClient (IPEndPoint.MaxPort + 1);
170 } catch (ArgumentOutOfRangeException ex) {
171 // Specified argument was out of the range of valid values
172 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
173 Assert.IsNull (ex.InnerException, "#A3");
174 Assert.IsNotNull (ex.Message, "#A4");
175 Assert.AreEqual ("port", ex.ParamName, "#A5");
179 new UdpClient (IPEndPoint.MinPort - 1);
181 } catch (ArgumentOutOfRangeException ex) {
182 // Specified argument was out of the range of valid values
183 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
184 Assert.IsNull (ex.InnerException, "#A3");
185 Assert.IsNotNull (ex.Message, "#A4");
186 Assert.AreEqual ("port", ex.ParamName, "#A5");
190 [Test] // .ctor (IPEndPoint)
191 public void Constructor4 ()
197 clientEP = new IPEndPoint (IPAddress.Loopback, 8001);
198 using (MyUdpClient client = new MyUdpClient (clientEP))
201 Assert.IsNotNull (s, "#A:Client");
202 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
203 Assert.IsFalse (s.Connected, "#A:Client:Connected");
204 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
205 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
206 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
207 Assert.IsFalse (client.Active, "#A:Active");
208 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
209 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
210 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
211 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
212 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
213 localEP = s.LocalEndPoint as IPEndPoint;
214 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
215 Assert.IsFalse (object.ReferenceEquals (clientEP, localEP), "#A:Client:LocalEndPoint/ReferenceEquality");
216 Assert.AreEqual (clientEP.Address, localEP.Address, "#A:Client:LocalEndPoint/Address");
217 Assert.AreEqual (clientEP.AddressFamily, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
218 Assert.AreEqual (clientEP.Port, localEP.Port, "#A:Client:LocalEndPoint/Port");
222 [Test] // .ctor (IPEndPoint)
223 public void Constructor4_LocalEP_Null ()
226 new UdpClient ((IPEndPoint) null);
228 } catch (ArgumentNullException ex) {
229 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
230 Assert.IsNull (ex.InnerException, "#3");
231 Assert.IsNotNull (ex.Message, "#4");
232 Assert.AreEqual ("localEP", ex.ParamName, "#5");
236 [Test] // .ctor (Int32, AddressFamily)
237 public void Constructor5 ()
242 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MinPort, AddressFamily.InterNetwork))
245 Assert.IsNotNull (s, "#A:Client");
246 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
247 Assert.IsFalse (s.Connected, "#A:Client:Connected");
248 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
249 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
250 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
251 Assert.IsFalse (client.Active, "#A:Active");
252 //Assert.IsFalse (client.DontFragment, "#A:DontFragment");
253 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
254 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
255 Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
256 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
257 localEP = s.LocalEndPoint as IPEndPoint;
258 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
259 Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
260 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
263 if (!Socket.OSSupportsIPv6)
264 Assert.Ignore ("IPv6 not enabled.");
266 using (MyUdpClient client = new MyUdpClient (IPEndPoint.MaxPort, AddressFamily.InterNetworkV6))
269 Assert.IsNotNull (s, "#B:Client");
270 Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
271 Assert.IsFalse (s.Connected, "#B:Client:Connected");
272 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
273 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
274 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
275 Assert.IsFalse (client.Active, "#B:Active");
276 //Assert.IsFalse (client.DontFragment, "#B:DontFragment");
277 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
278 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
279 Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
280 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
281 localEP = s.LocalEndPoint as IPEndPoint;
282 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
283 Assert.AreEqual (IPAddress.IPv6Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
284 Assert.AreEqual (AddressFamily.InterNetworkV6, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
285 Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
289 [Test] // .ctor (Int32, AddressFamily)
290 public void Constructor5_Family_Invalid ()
293 new UdpClient (80, AddressFamily.NetBios);
295 } catch (ArgumentException ex) {
297 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
298 Assert.IsNull (ex.InnerException, "#A3");
299 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
301 Assert.IsNotNull (ex.Message, "#A4");
302 Assert.AreEqual ("family", ex.ParamName, "#A5");
306 new UdpClient (80, (AddressFamily) 666);
308 } catch (ArgumentException ex) {
310 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
311 Assert.IsNull (ex.InnerException, "#B3");
312 // 'UDP' Client can only accept InterNetwork or InterNetworkV6
314 Assert.IsNotNull (ex.Message, "#B4");
315 Assert.AreEqual ("family", ex.ParamName, "#B5");
319 [Test] // .ctor (Int32, AddressFamily)
320 public void Constructor5_Port_OutOfRange ()
323 new UdpClient (IPEndPoint.MaxPort + 1, AddressFamily.InterNetwork);
325 } catch (ArgumentOutOfRangeException ex) {
326 // Specified argument was out of the range of valid values
327 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
328 Assert.IsNull (ex.InnerException, "#A3");
329 Assert.IsNotNull (ex.Message, "#A4");
330 Assert.AreEqual ("port", ex.ParamName, "#A5");
334 new UdpClient (IPEndPoint.MinPort - 1, AddressFamily.InterNetwork);
336 } catch (ArgumentOutOfRangeException ex) {
337 // Specified argument was out of the range of valid values
338 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
339 Assert.IsNull (ex.InnerException, "#A3");
340 Assert.IsNotNull (ex.Message, "#A4");
341 Assert.AreEqual ("port", ex.ParamName, "#A5");
345 [Test] // .ctor (String, Int32)
346 public void Constructor6 ()
352 // UDP port 0 doesn't seem to be valid.
353 using (MyUdpClient client = new MyUdpClient ("127.0.0.1", 53))
356 Assert.IsNotNull (s, "#A:Client");
357 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
358 Assert.IsTrue (s.Connected, "#A:Client:Connected");
359 Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
360 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
361 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
362 Assert.IsTrue (client.Active, "#A:Active");
363 Assert.IsFalse (client.DontFragment, "#A:DontFragment");
364 Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
365 //Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
366 //Assert.IsFalse (client.MulticastLoopback, "#A:MulticastLoopback");
367 //Assert.AreEqual (32, client.Ttl, "#A:Ttl");
368 localEP = s.LocalEndPoint as IPEndPoint;
369 Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
370 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#A:Client:LocalEndPoint/Address");
371 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
373 using (MyUdpClient client = new MyUdpClient ("127.0.0.1", IPEndPoint.MaxPort))
376 Assert.IsNotNull (s, "#B:Client");
377 Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
378 Assert.IsTrue (s.Connected, "#B:Client:Connected");
379 Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
380 Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
381 Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
382 Assert.IsTrue (client.Active, "#B:Active");
383 Assert.IsFalse (client.DontFragment, "#B:DontFragment");
384 Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
385 //Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
386 //Assert.IsFalse (client.MulticastLoopback, "#B:MulticastLoopback");
387 //Assert.AreEqual (32, client.Ttl, "#B:Ttl");
388 localEP = s.LocalEndPoint as IPEndPoint;
389 Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
390 Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#B:Client:LocalEndPoint/Address");
391 Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
395 [Test] // .ctor (String, Int32)
396 public void Constructor6_HostName_Null ()
399 new UdpClient ((string) null, int.MaxValue);
401 } catch (ArgumentNullException ex) {
402 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
403 Assert.IsNull (ex.InnerException, "#3");
404 Assert.IsNotNull (ex.Message, "#4");
405 Assert.AreEqual ("hostname", ex.ParamName, "#5");
409 [Test] // .ctor (String, Int32)
410 public void Constructor6_Port_OutOfRange ()
413 new UdpClient ("local", IPEndPoint.MaxPort + 1);
415 } catch (ArgumentOutOfRangeException ex) {
416 // Specified argument was out of the range of valid values
417 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
418 Assert.IsNull (ex.InnerException, "#A3");
419 Assert.IsNotNull (ex.Message, "#A4");
420 Assert.AreEqual ("port", ex.ParamName, "#A5");
424 new UdpClient ("local", IPEndPoint.MinPort - 1);
426 } catch (ArgumentOutOfRangeException ex) {
427 // Specified argument was out of the range of valid values
428 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
429 Assert.IsNull (ex.InnerException, "#A3");
430 Assert.IsNotNull (ex.Message, "#A4");
431 Assert.AreEqual ("port", ex.ParamName, "#A5");
436 public void UdpClientBroadcastTest ()
438 UdpClient client = new UdpClient ();
439 byte[] bytes = new byte[] {10, 11, 12, 13};
442 client.Send (bytes, bytes.Length, new IPEndPoint (IPAddress.Broadcast, 1235));
448 [Test] // JoinMulticastGroup (IPAddress)
449 public void JoinMulticastGroup1_IPv4 ()
451 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
453 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
454 client.JoinMulticastGroup (mcast_addr);
458 [Test] // JoinMulticastGroup (IPAddress)
459 public void JoinMulticastGroup1_IPv6 ()
461 if (!Socket.OSSupportsIPv6)
462 Assert.Ignore ("IPv6 not enabled.");
464 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
466 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
467 client.JoinMulticastGroup (mcast_addr);
471 [Test] // JoinMulticastGroup (IPAddress)
472 public void JoinMulticastGroup1_MulticastAddr_Null ()
474 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
476 client.JoinMulticastGroup ((IPAddress) null);
478 } catch (ArgumentNullException ex) {
479 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
480 Assert.IsNull (ex.InnerException, "#3");
481 Assert.IsNotNull (ex.Message, "#4");
482 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
487 [Test] // JoinMulticastGroup (IPAddress)
488 public void JoinMulticastGroup1_Socket_Closed ()
490 IPAddress mcast_addr = null;
492 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
495 client.JoinMulticastGroup (mcast_addr);
497 } catch (ObjectDisposedException ex) {
498 // Cannot access a disposed object
499 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
500 Assert.IsNull (ex.InnerException, "#3");
501 Assert.IsNotNull (ex.Message, "#4");
502 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
506 [Test] // JoinMulticastGroup (IPAddress)
507 [Category ("NotWorking")]
508 public void JoinMulticastGroup1_Socket_NotBound ()
510 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
512 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
514 client.JoinMulticastGroup (mcast_addr);
516 } catch (SocketException ex) {
517 // An invalid argument was supplied
518 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
519 Assert.AreEqual (10022, ex.ErrorCode, "#3");
520 Assert.IsNull (ex.InnerException, "#4");
521 Assert.IsNotNull (ex.Message, "#5");
522 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
523 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
529 [Test] // JoinMulticastGroup (In32, IPAddress)
530 public void JoinMulticastGroup2_IPv4 ()
532 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
534 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
536 client.JoinMulticastGroup (0, mcast_addr);
538 } catch (SocketException ex) {
539 // The attempted operation is not supported for the type of
541 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
542 Assert.AreEqual (10045, ex.ErrorCode, "#3");
543 Assert.IsNull (ex.InnerException, "#4");
544 Assert.IsNotNull (ex.Message, "#5");
545 Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
546 Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
551 [Test] // JoinMulticastGroup (In32, IPAddress)
552 public void JoinMulticastGroup2_IPv6 ()
554 if (!Socket.OSSupportsIPv6)
555 Assert.Ignore ("IPv6 not enabled.");
557 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
559 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
560 client.JoinMulticastGroup (0, mcast_addr);
564 [Test] // JoinMulticastGroup (Int32, IPAddress)
565 public void JoinMulticastGroup2_MulticastAddr_Null ()
567 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
569 client.JoinMulticastGroup (0, (IPAddress) null);
571 } catch (ArgumentNullException ex) {
572 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
573 Assert.IsNull (ex.InnerException, "#3");
574 Assert.IsNotNull (ex.Message, "#4");
575 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
580 [Test] // JoinMulticastGroup (Int32, IPAddress)
581 public void JoinMulticastGroup2_Socket_Closed ()
583 if (!Socket.OSSupportsIPv6)
584 Assert.Ignore ("IPv6 not enabled.");
586 IPAddress mcast_addr = null;
588 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234));
591 client.JoinMulticastGroup (0, mcast_addr);
593 } catch (ObjectDisposedException ex) {
594 // Cannot access a disposed object
595 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
596 Assert.IsNull (ex.InnerException, "#3");
597 Assert.IsNotNull (ex.Message, "#4");
598 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
602 [Test] // JoinMulticastGroup (Int32, IPAddress)
603 [Category ("NotWorking")]
604 public void JoinMulticastGroup2_Socket_NotBound ()
606 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
608 UdpClient client = new UdpClient (AddressFamily.InterNetworkV6);
610 client.JoinMulticastGroup (0, mcast_addr);
612 } catch (SocketException ex) {
613 // An invalid argument was supplied
614 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
615 Assert.AreEqual (10022, ex.ErrorCode, "#3");
616 Assert.IsNull (ex.InnerException, "#4");
617 Assert.IsNotNull (ex.Message, "#5");
618 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
619 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
625 [Test] // JoinMulticastGroup (IPAddress, Int32)
626 public void JoinMulticastGroup3_IPv4 ()
628 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
630 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
631 client.JoinMulticastGroup (mcast_addr, 0);
634 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
635 client.JoinMulticastGroup (mcast_addr, 255);
639 [Test] // JoinMulticastGroup (IPAddress, Int32)
640 public void JoinMulticastGroup3_IPv6 ()
642 if (!Socket.OSSupportsIPv6)
643 Assert.Ignore ("IPv6 not enabled.");
645 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
647 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
648 client.JoinMulticastGroup (mcast_addr, 0);
651 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
652 client.JoinMulticastGroup (mcast_addr, 255);
656 [Test] // JoinMulticastGroup (IPAddress, Int32)
657 public void JoinMulticastGroup3_MulticastAddr_Null ()
659 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
661 client.JoinMulticastGroup ((IPAddress) null, int.MaxValue);
663 } catch (ArgumentNullException ex) {
664 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
665 Assert.IsNull (ex.InnerException, "#3");
666 Assert.IsNotNull (ex.Message, "#4");
667 Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
672 [Test] // JoinMulticastGroup (IPAddress, Int32)
673 public void JoinMulticastGroup3_Socket_Closed ()
675 IPAddress mcast_addr = null;
677 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
680 client.JoinMulticastGroup (mcast_addr, 0);
682 } catch (ObjectDisposedException ex) {
683 // Cannot access a disposed object
684 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
685 Assert.IsNull (ex.InnerException, "#3");
686 Assert.IsNotNull (ex.Message, "#4");
687 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
691 [Test] // JoinMulticastGroup (IPAddress, Int32)
692 [Category ("NotWorking")]
693 public void JoinMulticastGroup3_Socket_NotBound ()
695 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
697 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
699 client.JoinMulticastGroup (mcast_addr, 5);
701 } catch (SocketException ex) {
702 // An invalid argument was supplied
703 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
704 Assert.AreEqual (10022, ex.ErrorCode, "#3");
705 Assert.IsNull (ex.InnerException, "#4");
706 Assert.IsNotNull (ex.Message, "#5");
707 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
708 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
714 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
715 public void JoinMulticastGroup4_IPv4 ()
717 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
718 IPAddress local_addr = IPAddress.Any;
720 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
721 client.JoinMulticastGroup (mcast_addr, local_addr);
725 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
726 public void JoinMulticastGroup4_IPv6 ()
728 if (!Socket.OSSupportsIPv6)
729 Assert.Ignore ("IPv6 not enabled.");
731 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
732 IPAddress local_addr = IPAddress.IPv6Any;
734 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
736 client.JoinMulticastGroup (mcast_addr, local_addr);
738 } catch (SocketException ex) {
739 // The attempted operation is not supported for the type of
741 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
742 Assert.AreEqual (10045, ex.ErrorCode, "#3");
743 Assert.IsNull (ex.InnerException, "#4");
744 Assert.IsNotNull (ex.Message, "#5");
745 Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
746 Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
751 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
752 public void JoinMulticastGroup4_LocalAddress_Null ()
754 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
756 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
758 client.JoinMulticastGroup (mcast_addr, (IPAddress) null);
760 } catch (ArgumentNullException ex) {
761 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
762 Assert.IsNull (ex.InnerException, "#3");
763 Assert.IsNotNull (ex.Message, "#4");
764 Assert.AreEqual ("mcint", ex.ParamName, "#5");
769 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
770 public void JoinMulticastGroup4_MulticastAddr_Null ()
772 using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
774 client.JoinMulticastGroup ((IPAddress) null, IPAddress.Loopback);
776 } catch (ArgumentNullException ex) {
777 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
778 Assert.IsNull (ex.InnerException, "#3");
779 Assert.IsNotNull (ex.Message, "#4");
780 Assert.AreEqual ("group", ex.ParamName, "#5");
785 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
786 public void JoinMulticastGroup4_Socket_Closed ()
788 IPAddress mcast_addr = null;
789 IPAddress local_addr = null;
791 UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
794 client.JoinMulticastGroup (mcast_addr, local_addr);
796 } catch (ObjectDisposedException ex) {
797 // Cannot access a disposed object
798 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
799 Assert.IsNull (ex.InnerException, "#3");
800 Assert.IsNotNull (ex.Message, "#4");
801 Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
805 [Test] // JoinMulticastGroup (IPAddress, IPAddress)
806 [Category ("NotWorking")]
807 public void JoinMulticastGroup4_Socket_NotBound ()
809 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
810 IPAddress local_addr = Dns.GetHostEntry (string.Empty).AddressList [0];
812 UdpClient client = new UdpClient (AddressFamily.InterNetwork);
814 client.JoinMulticastGroup (mcast_addr, local_addr);
816 } catch (SocketException ex) {
817 // An invalid argument was supplied
818 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
819 Assert.AreEqual (10022, ex.ErrorCode, "#3");
820 Assert.IsNull (ex.InnerException, "#4");
821 Assert.IsNotNull (ex.Message, "#5");
822 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
823 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
830 public void CloseInReceive ()
832 UdpClient client = null;
833 var rnd = new Random ();
834 for (int i = 0, max = 5; i < max; i++) {
835 int port = rnd.Next (1025, 65534);
837 client = new UdpClient (port);
839 } catch (Exception) {
845 ManualResetEvent ready = new ManualResetEvent (false);
846 bool got_exc = false;
848 Task receive_task = Task.Factory.StartNew (() => {
849 IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
852 client.Receive(ref ep);
853 } catch (SocketException) {
860 ready.WaitOne (2000);
864 Assert.IsTrue (receive_task.Wait (1000));
865 Assert.IsTrue (got_exc);
868 // Test for bug 324033
870 public void JoinMulticastGroupWithLocal ()
872 UdpClient client = new UdpClient (9001);
873 IPAddress mcast_addr = IPAddress.Parse ("224.0.0.24");
874 IPAddress local_addr = IPAddress.Any;
877 client.JoinMulticastGroup (mcast_addr, local_addr);
884 [ExpectedException (typeof(ArgumentNullException))]
885 public void BeginSendNull ()
887 UdpClient client = new UdpClient ();
888 client.BeginSend (null, 0, null, null);
892 static bool BSSent = false;
894 static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
896 private static void BSCallback (IAsyncResult asyncResult)
898 UdpClient client = (UdpClient)asyncResult.AsyncState;
900 BSBytes = client.EndSend (asyncResult);
907 public void BeginSend ()
909 UdpClient client = new UdpClient ();
910 byte[] bytes = new byte[] {10, 11, 12, 13};
913 client.BeginSend (bytes, bytes.Length, new AsyncCallback (BSCallback), client);
914 Assert.Fail ("BeginSend #1");
915 } catch (SocketException ex) {
916 Assert.AreEqual (10057, ex.ErrorCode,
921 client.BeginSend (bytes, bytes.Length, null, new AsyncCallback (BSCallback), client);
922 Assert.Fail ("BeginSend #3");
923 } catch (SocketException ex) {
924 Assert.AreEqual (10057, ex.ErrorCode,
928 IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1236);
930 BSCalledBack.Reset ();
932 client.BeginSend (bytes, bytes.Length, ep,
933 new AsyncCallback (BSCallback),
935 if (BSCalledBack.WaitOne (2000, false) == false) {
936 Assert.Fail ("BeginSend wait timed out");
939 Assert.AreEqual (true, BSSent, "BeginSend #5");
940 Assert.AreEqual (4, BSBytes, "BeginSend #6");
945 static bool BRReceived = false;
946 static byte[] BRBytes;
947 static IPEndPoint BRFrom;
948 static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
950 private static void BRCallback (IAsyncResult asyncResult)
952 UdpClient client = (UdpClient)asyncResult.AsyncState;
954 BRBytes = client.EndReceive (asyncResult, ref BRFrom);
961 public void BeginReceive ()
963 UdpClient client = new UdpClient (1237);
965 BRCalledBack.Reset ();
967 client.BeginReceive (BRCallback, client);
969 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1237);
970 byte[] send_bytes = new byte[] {10, 11, 12, 13};
971 client.Send (send_bytes, send_bytes.Length, ep);
973 if (BRCalledBack.WaitOne (2000, false) == false) {
974 Assert.Fail ("BeginReceive wait timed out");
977 Assert.AreEqual (true, BRReceived, "BeginReceive #1");
978 Assert.AreEqual (4, BRBytes.Length, "BeginReceive #2");
979 Assert.AreEqual (ep. Port, BRFrom.Port, "BeginReceive #3");
980 Assert.AreEqual (ep.Address, BRFrom.Address, "BeginReceive #4");
986 public void Available ()
988 using (UdpClient client = new UdpClient (1238)) {
989 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1238);
990 byte[] bytes = new byte[] {10, 11, 12, 13};
992 int res = client.Send (bytes, bytes.Length, ep);
993 Assert.AreEqual (bytes.Length, res, "Send");
995 // that might happen too quickly, data sent and not yet received/available
997 int avail = client.Available;
999 Assert.AreEqual (bytes.Length, avail, "Available #1");
1006 [Category ("NotWorking")] // Using PMTU settings workaround on Linux, default true
1007 public void DontFragmentDefault ()
1009 UdpClient client = new UdpClient ();
1011 /* Ignore the docs, testing shows the default
1012 * here is in fact false
1014 Assert.AreEqual (false, client.DontFragment, "DontFragmentDefault");
1020 public void EnableBroadcastDefault ()
1022 UdpClient client = new UdpClient ();
1024 Assert.AreEqual (false, client.EnableBroadcast, "EnableBroadcastDefault");
1029 /* Can't test the default for ExclusiveAddressUse as
1030 * it's different on different versions and service
1034 [Category ("NotWorking")] // Not supported on Linux
1035 public void ExclusiveAddressUseUnbound ()
1037 UdpClient client = new UdpClient ();
1039 client.ExclusiveAddressUse = true;
1041 Assert.AreEqual (true, client.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
1047 [ExpectedException (typeof(InvalidOperationException))]
1048 [Category ("NotWorking")] // Not supported on Linux
1049 public void ExclusiveAddressUseBound ()
1051 UdpClient client = new UdpClient (1239);
1053 client.ExclusiveAddressUse = true;
1059 public void MulticastLoopbackDefault ()
1061 UdpClient client = new UdpClient ();
1063 Assert.AreEqual (true, client.MulticastLoopback, "MulticastLoopbackDefault");
1068 /* No test for Ttl default as it is platform dependent */
1070 class MyUdpClient : UdpClient
1072 public MyUdpClient ()
1076 public MyUdpClient (AddressFamily family)
1081 public MyUdpClient (Int32 port)
1087 public MyUdpClient (IPEndPoint localEP)
1092 public MyUdpClient (int port, AddressFamily family)
1093 : base (port, family)
1097 public MyUdpClient (string hostname, int port)
1098 : base (hostname, port)
1102 public new bool Active {
1103 get { return base.Active; }
1104 set { base.Active = value; }