Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UdpClientTest.cs « System.Net.Sockets « Test « System « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 79028e155ab329fccfcff8ffee8d0c28c664dbf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
// System.Net.Sockets.UdpClientTest.cs
//
// Authors:
//	Chris Bacon <chris.bacon@docobo.co.uk>
//	Gert Driesen <drieseng@users.sourceforge.net>
//

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

using NUnit.Framework;

namespace MonoTests.System.Net.Sockets {
#if TARGET_JVM
	[Ignore("UdpClient is not supported - since UDP sockets are not supported")]
#endif
	[TestFixture]
	public class UdpClientTest {
		[Test] // .ctor ()
		public void Constructor1 ()
		{
			MyUdpClient client;
			Socket s;

			client = new MyUdpClient ();
			s = client.Client;
			Assert.IsNotNull (s, "Client");
			Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "Client:AddressFamily");
			Assert.IsFalse (s.Connected, "Client:Connected");
#if NET_2_0
			Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
#endif
			Assert.IsNull (s.LocalEndPoint, "Client:LocalEndPoint");
			Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "Client:ProtocolType");
			Assert.IsNull (s.RemoteEndPoint, "Client:RemoteEndPoint");
			Assert.AreEqual (SocketType.Dgram, s.SocketType, "Client:SocketType");
			Assert.IsFalse (client.Active, "Active");
#if NET_2_0
			Assert.IsFalse (client.DontFragment, "DontFragment");
			Assert.IsFalse (client.EnableBroadcast, "EnableBroadcast");
			//Assert.IsFalse (client.ExclusiveAddressUse, "ExclusiveAddressUse");
			Assert.IsTrue (client.MulticastLoopback, "MulticastLoopback");
			//Assert.AreEqual (32, client.Ttl, "Ttl");
#endif
		}

		[Test] // .ctor (AddressFamily)
		public void Constructor2 ()
		{
			MyUdpClient client;
			Socket s;

			client = new MyUdpClient (AddressFamily.InterNetwork);
			s = client.Client;
			Assert.IsNotNull (s, "#A:Client");
			Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
			Assert.IsFalse (s.Connected, "#A:Client:Connected");
#if NET_2_0
			Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
#endif
			Assert.IsNull (s.LocalEndPoint, "#A:Client:LocalEndPoint");
			Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
			Assert.IsNull (s.RemoteEndPoint, "#A:Client:RemoteEndPoint");
			Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
			Assert.IsFalse (client.Active, "#A:Active");
#if NET_2_0
			//Assert.IsFalse (client.DontFragment, "#A:DontFragment");
			Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
			//Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
			Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
			//Assert.AreEqual (32, client.Ttl, "#A:Ttl");
#endif

			client = new MyUdpClient (AddressFamily.InterNetworkV6);
			s = client.Client;
			Assert.IsNotNull (s, "#B:Client");
			Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
			Assert.IsFalse (s.Connected, "#B:Client:Connected");
#if NET_2_0
			Assert.IsFalse (s.IsBound, "#A:Client:IsBound");
#endif
			Assert.IsNull (s.LocalEndPoint, "#B:Client:LocalEndPoint");
			Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
			Assert.IsNull (s.RemoteEndPoint, "#B:Client:RemoteEndPoint");
			Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
			Assert.IsFalse (client.Active, "#B:Active");
#if NET_2_0
			//Assert.IsFalse (client.DontFragment, "#B:DontFragment");
			Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
			//Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
			Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
			//Assert.AreEqual (32, client.Ttl, "#B:Ttl");
#endif
		}

		[Test] // .ctor (AddressFamily)
		public void Constructor2_Family_Invalid ()
		{
			try {
				new UdpClient (AddressFamily.NetBios);
				Assert.Fail ("#A1");
			} catch (ArgumentException ex) {
				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
				Assert.IsNull (ex.InnerException, "#A3");
#if NET_2_0
				// 'UDP' Client can only accept InterNetwork or InterNetworkV6
				// addresses
				Assert.IsNotNull (ex.Message, "#A4");
				Assert.AreEqual ("family", ex.ParamName, "#A5");
#else
				Assert.AreEqual ("family", ex.Message, "#A4");
				Assert.IsNull (ex.ParamName, "#A5");
#endif
			}

			try {
				new UdpClient ((AddressFamily) 666);
				Assert.Fail ("#B1");
			} catch (ArgumentException ex) {
				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
				Assert.IsNull (ex.InnerException, "#B3");
#if NET_2_0
				Assert.IsNotNull (ex.Message, "#B4");
				Assert.AreEqual ("family", ex.ParamName, "#B5");
#else
				Assert.AreEqual ("family", ex.Message, "#B4");
				Assert.IsNull (ex.ParamName, "#B5");
#endif
			}
		}

		[Test] // .ctor (Int32)
		public void Constructor3 ()
		{
			MyUdpClient client;
			Socket s;
			IPEndPoint localEP;

			client = new MyUdpClient (IPEndPoint.MinPort);
			s = client.Client;
			Assert.IsNotNull (s, "#A:Client");
			Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
			Assert.IsFalse (s.Connected, "#A:Client:Connected");
#if NET_2_0
			Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
#endif
			Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
			Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
			Assert.IsFalse (client.Active, "#A:Active");
#if NET_2_0
			Assert.IsFalse (client.DontFragment, "#A:DontFragment");
			Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
			//Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
			Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
			//Assert.AreEqual (32, client.Ttl, "#A:Ttl");
#endif
			localEP = s.LocalEndPoint as IPEndPoint;
			Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
			Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
			Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");

			client = new MyUdpClient (IPEndPoint.MaxPort);
			s = client.Client;
			Assert.IsNotNull (s, "#B:Client");
			Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
			Assert.IsFalse (s.Connected, "#B:Client:Connected");
#if NET_2_0
			Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
#endif
			Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
			Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
			Assert.IsFalse (client.Active, "#B:Active");
#if NET_2_0
			Assert.IsFalse (client.DontFragment, "#B:DontFragment");
			Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
			//Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
			Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
			//Assert.AreEqual (32, client.Ttl, "#B:Ttl");
#endif
			localEP = s.LocalEndPoint as IPEndPoint;
			Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
			Assert.AreEqual (IPAddress.Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
			Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
			Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
		}

		[Test] // .ctor (Int32)
		public void Constructor3_Port_OutOfRange ()
		{
			try {
				new UdpClient (IPEndPoint.MaxPort + 1);
				Assert.Fail ("#A1");
			} catch (ArgumentOutOfRangeException ex) {
				// Specified argument was out of the range of valid values
				Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
				Assert.IsNull (ex.InnerException, "#A3");
				Assert.IsNotNull (ex.Message, "#A4");
				Assert.AreEqual ("port", ex.ParamName, "#A5");
			}

			try {
				new UdpClient (IPEndPoint.MinPort - 1);
				Assert.Fail ("#A1");
			} catch (ArgumentOutOfRangeException ex) {
				// Specified argument was out of the range of valid values
				Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
				Assert.IsNull (ex.InnerException, "#A3");
				Assert.IsNotNull (ex.Message, "#A4");
				Assert.AreEqual ("port", ex.ParamName, "#A5");
			}
		}

		[Test] // .ctor (IPEndPoint)
		public void Constructor4 ()
		{
			MyUdpClient client;
			Socket s;
			IPEndPoint localEP;
			IPEndPoint clientEP;

			clientEP = new IPEndPoint (IPAddress.Loopback, 8001);
			client = new MyUdpClient (clientEP);
			s = client.Client;
			Assert.IsNotNull (s, "#A:Client");
			Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
			Assert.IsFalse (s.Connected, "#A:Client:Connected");
#if NET_2_0
			Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
#endif
			Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
			Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
			Assert.IsFalse (client.Active, "#A:Active");
#if NET_2_0
			Assert.IsFalse (client.DontFragment, "#A:DontFragment");
			Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
			//Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
			Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
			//Assert.AreEqual (32, client.Ttl, "#A:Ttl");
#endif
			localEP = s.LocalEndPoint as IPEndPoint;
			Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
			Assert.IsFalse (object.ReferenceEquals (clientEP, localEP), "#A:Client:LocalEndPoint/ReferenceEquality");
			Assert.AreEqual (clientEP.Address, localEP.Address, "#A:Client:LocalEndPoint/Address");
			Assert.AreEqual (clientEP.AddressFamily, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");
			Assert.AreEqual (clientEP.Port, localEP.Port, "#A:Client:LocalEndPoint/Port");
		}

		[Test] // .ctor (IPEndPoint)
		public void Constructor4_LocalEP_Null ()
		{
			try {
				new UdpClient ((IPEndPoint) null);
				Assert.Fail ("#1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.AreEqual ("localEP", ex.ParamName, "#5");
			}
		}

		[Test] // .ctor (Int32, AddressFamily)
		public void Constructor5 ()
		{
			MyUdpClient client;
			Socket s;
			IPEndPoint localEP;

			client = new MyUdpClient (IPEndPoint.MinPort, AddressFamily.InterNetwork);
			s = client.Client;
			Assert.IsNotNull (s, "#A:Client");
			Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
			Assert.IsFalse (s.Connected, "#A:Client:Connected");
#if NET_2_0
			Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
#endif
			Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
			Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
			Assert.IsFalse (client.Active, "#A:Active");
#if NET_2_0
			//Assert.IsFalse (client.DontFragment, "#A:DontFragment");
			Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
			//Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
			Assert.IsTrue (client.MulticastLoopback, "#A:MulticastLoopback");
			//Assert.AreEqual (32, client.Ttl, "#A:Ttl");
#endif
			localEP = s.LocalEndPoint as IPEndPoint;
			Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
			Assert.AreEqual (IPAddress.Any, localEP.Address, "#A:Client:LocalEndPoint/Address");
			Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");

			client = new MyUdpClient (IPEndPoint.MaxPort, AddressFamily.InterNetworkV6);
			s = client.Client;
			Assert.IsNotNull (s, "#B:Client");
			Assert.AreEqual (AddressFamily.InterNetworkV6, s.AddressFamily, "#B:Client:AddressFamily");
			Assert.IsFalse (s.Connected, "#B:Client:Connected");
#if NET_2_0
			Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
#endif
			Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
			Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
			Assert.IsFalse (client.Active, "#B:Active");
#if NET_2_0
			//Assert.IsFalse (client.DontFragment, "#B:DontFragment");
			Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
			//Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
			Assert.IsTrue (client.MulticastLoopback, "#B:MulticastLoopback");
			//Assert.AreEqual (32, client.Ttl, "#B:Ttl");
#endif
			localEP = s.LocalEndPoint as IPEndPoint;
			Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
			Assert.AreEqual (IPAddress.IPv6Any, localEP.Address, "#B:Client:LocalEndPoint/Address");
			Assert.AreEqual (AddressFamily.InterNetworkV6, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
			Assert.AreEqual (IPEndPoint.MaxPort, localEP.Port, "#B:Client:LocalEndPoint/Port");
		}

		[Test] // .ctor (Int32, AddressFamily)
		public void Constructor5_Family_Invalid ()
		{
			try {
				new UdpClient (80, AddressFamily.NetBios);
				Assert.Fail ("#A1");
			} catch (ArgumentException ex) {
				// family
				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
				Assert.IsNull (ex.InnerException, "#A3");
#if NET_2_0
				// 'UDP' Client can only accept InterNetwork or InterNetworkV6
				// addresses
				Assert.IsNotNull (ex.Message, "#A4");
				Assert.AreEqual ("family", ex.ParamName, "#A5");
#else
				Assert.AreEqual ("family", ex.Message, "#A4");
				Assert.IsNull (ex.ParamName, "#A5");
#endif
			}

			try {
				new UdpClient (80, (AddressFamily) 666);
				Assert.Fail ("#B1");
			} catch (ArgumentException ex) {
				// family
				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
				Assert.IsNull (ex.InnerException, "#B3");
#if NET_2_0
				// 'UDP' Client can only accept InterNetwork or InterNetworkV6
				// addresses
				Assert.IsNotNull (ex.Message, "#B4");
				Assert.AreEqual ("family", ex.ParamName, "#B5");
#else
				Assert.AreEqual ("family", ex.Message, "#B4");
				Assert.IsNull (ex.ParamName, "#B5");
#endif
			}
		}

		[Test] // .ctor (Int32, AddressFamily)
		public void Constructor5_Port_OutOfRange ()
		{
			try {
				new UdpClient (IPEndPoint.MaxPort + 1, AddressFamily.InterNetwork);
				Assert.Fail ("#A1");
			} catch (ArgumentOutOfRangeException ex) {
				// Specified argument was out of the range of valid values
				Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
				Assert.IsNull (ex.InnerException, "#A3");
				Assert.IsNotNull (ex.Message, "#A4");
				Assert.AreEqual ("port", ex.ParamName, "#A5");
			}

			try {
				new UdpClient (IPEndPoint.MinPort - 1, AddressFamily.InterNetwork);
				Assert.Fail ("#A1");
			} catch (ArgumentOutOfRangeException ex) {
				// Specified argument was out of the range of valid values
				Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
				Assert.IsNull (ex.InnerException, "#A3");
				Assert.IsNotNull (ex.Message, "#A4");
				Assert.AreEqual ("port", ex.ParamName, "#A5");
			}
		}

		[Test] // .ctor (String, Int32)
		public void Constructor6 ()
		{
			MyUdpClient client;
			Socket s;
			IPEndPoint localEP;

			// Bug #5503
			// UDP port 0 doesn't seem to be valid.
			client = new MyUdpClient ("127.0.0.1", 53);
			s = client.Client;
			Assert.IsNotNull (s, "#A:Client");
			Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#A:Client:AddressFamily");
			Assert.IsTrue (s.Connected, "#A:Client:Connected");
#if NET_2_0
			Assert.IsTrue (s.IsBound, "#A:Client:IsBound");
#endif
			Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#A:Client:ProtocolType");
			Assert.AreEqual (SocketType.Dgram, s.SocketType, "#A:Client:SocketType");
			Assert.IsTrue (client.Active, "#A:Active");
#if NET_2_0
			Assert.IsFalse (client.DontFragment, "#A:DontFragment");
			Assert.IsFalse (client.EnableBroadcast, "#A:EnableBroadcast");
			//Assert.IsFalse (client.ExclusiveAddressUse, "#A:ExclusiveAddressUse");
			//Assert.IsFalse (client.MulticastLoopback, "#A:MulticastLoopback");
			//Assert.AreEqual (32, client.Ttl, "#A:Ttl");
#endif
			localEP = s.LocalEndPoint as IPEndPoint;
			Assert.IsNotNull (localEP, "#A:Client:LocalEndpoint");
			Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#A:Client:LocalEndPoint/Address");
			Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#A:Client:LocalEndPoint/AddressFamily");

			client = new MyUdpClient ("127.0.0.1", IPEndPoint.MaxPort);
			s = client.Client;
			Assert.IsNotNull (s, "#B:Client");
			Assert.AreEqual (AddressFamily.InterNetwork, s.AddressFamily, "#B:Client:AddressFamily");
			Assert.IsTrue (s.Connected, "#B:Client:Connected");
#if NET_2_0
			Assert.IsTrue (s.IsBound, "#B:Client:IsBound");
#endif
			Assert.AreEqual (ProtocolType.Udp, s.ProtocolType, "#B:Client:ProtocolType");
			Assert.AreEqual (SocketType.Dgram, s.SocketType, "#B:Client:SocketType");
			Assert.IsTrue (client.Active, "#B:Active");
#if NET_2_0
			Assert.IsFalse (client.DontFragment, "#B:DontFragment");
			Assert.IsFalse (client.EnableBroadcast, "#B:EnableBroadcast");
			//Assert.IsFalse (client.ExclusiveAddressUse, "#B:ExclusiveAddressUse");
			//Assert.IsFalse (client.MulticastLoopback, "#B:MulticastLoopback");
			//Assert.AreEqual (32, client.Ttl, "#B:Ttl");
#endif
			localEP = s.LocalEndPoint as IPEndPoint;
			Assert.IsNotNull (localEP, "#B:Client:LocalEndpoint");
			Assert.AreEqual (IPAddress.Loopback, localEP.Address, "#B:Client:LocalEndPoint/Address");
			Assert.AreEqual (AddressFamily.InterNetwork, localEP.AddressFamily, "#B:Client:LocalEndPoint/AddressFamily");
		}

		[Test] // .ctor (String, Int32)
		public void Constructor6_HostName_Null ()
		{
			try {
				new UdpClient ((string) null, int.MaxValue);
				Assert.Fail ("#1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.AreEqual ("hostname", ex.ParamName, "#5");
			}
		}

		[Test] // .ctor (String, Int32)
		public void Constructor6_Port_OutOfRange ()
		{
			try {
				new UdpClient ("local", IPEndPoint.MaxPort + 1);
				Assert.Fail ("#A1");
			} catch (ArgumentOutOfRangeException ex) {
				// Specified argument was out of the range of valid values
				Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
				Assert.IsNull (ex.InnerException, "#A3");
				Assert.IsNotNull (ex.Message, "#A4");
				Assert.AreEqual ("port", ex.ParamName, "#A5");
			}

			try {
				new UdpClient ("local", IPEndPoint.MinPort - 1);
				Assert.Fail ("#A1");
			} catch (ArgumentOutOfRangeException ex) {
				// Specified argument was out of the range of valid values
				Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
				Assert.IsNull (ex.InnerException, "#A3");
				Assert.IsNotNull (ex.Message, "#A4");
				Assert.AreEqual ("port", ex.ParamName, "#A5");
			}
		}

		[Test]
		public void UdpClientBroadcastTest () 
		{
			UdpClient client = new UdpClient ();
			byte[] bytes = new byte[] {10, 11, 12, 13};

			try {
				client.Send (bytes, bytes.Length, new IPEndPoint (IPAddress.Broadcast, 1235));
			} finally {
				client.Close ();
			}
		}

		[Test] // JoinMulticastGroup (IPAddress)
		public void JoinMulticastGroup1_IPv4 ()
		{
			IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");

			using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
				client.JoinMulticastGroup (mcast_addr);
			}
		}

		[Test] // JoinMulticastGroup (IPAddress)
		[Category ("NotOnMac")]
		public void JoinMulticastGroup1_IPv6 ()
		{
#if NET_2_0
			if (!Socket.OSSupportsIPv6)
#else
			if (!Socket.SupportsIPv6)
#endif
				Assert.Ignore ("IPv6 not enabled.");

			IPAddress mcast_addr = IPAddress.Parse ("ff02::1");

			using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
				client.JoinMulticastGroup (mcast_addr);
			}
		}

		[Test] // JoinMulticastGroup (IPAddress)
		public void JoinMulticastGroup1_MulticastAddr_Null ()
		{
			using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
				try {
					client.JoinMulticastGroup ((IPAddress) null);
					Assert.Fail ("#1");
#if NET_2_0
				} catch (ArgumentNullException ex) {
					Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
					Assert.IsNull (ex.InnerException, "#3");
					Assert.IsNotNull (ex.Message, "#4");
					Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
				}
#else
				} catch (NullReferenceException ex) {
					Assert.AreEqual (typeof (NullReferenceException), ex.GetType (), "#2");
					Assert.IsNull (ex.InnerException, "#3");
					Assert.IsNotNull (ex.Message, "#4");
				}
#endif
			}
		}

		[Test] // JoinMulticastGroup (IPAddress)
		public void JoinMulticastGroup1_Socket_Closed ()
		{
			IPAddress mcast_addr = null;

			UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234));
			client.Close ();
			try {
				client.JoinMulticastGroup (mcast_addr);
				Assert.Fail ("#1");
			} catch (ObjectDisposedException ex) {
				// Cannot access a disposed object
				Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
			}
		}

		[Test] // JoinMulticastGroup (IPAddress)
		[Category ("NotWorking")]
		public void JoinMulticastGroup1_Socket_NotBound ()
		{
			IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");

			UdpClient client = new UdpClient (AddressFamily.InterNetwork);
			try {
				client.JoinMulticastGroup (mcast_addr);
				Assert.Fail ("#1");
			} catch (SocketException ex) {
				// An invalid argument was supplied
				Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
				Assert.AreEqual (10022, ex.ErrorCode, "#3");
				Assert.IsNull (ex.InnerException, "#4");
				Assert.IsNotNull (ex.Message, "#5");
				Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
#if NET_2_0
				Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
#endif
			} finally {
				client.Close ();
			}
		}

		[Test] // JoinMulticastGroup (In32, IPAddress)
		public void JoinMulticastGroup2_IPv4 ()
		{
			IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");

			using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
				try {
					client.JoinMulticastGroup (0, mcast_addr);
					Assert.Fail ("#1");
				} catch (SocketException ex) {
					// The attempted operation is not supported for the type of
					// object referenced
					Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
					Assert.AreEqual (10045, ex.ErrorCode, "#3");
					Assert.IsNull (ex.InnerException, "#4");
					Assert.IsNotNull (ex.Message, "#5");
					Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
#if NET_2_0
					Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
#endif
				}
			}
		}

		[Test] // JoinMulticastGroup (In32, IPAddress)
		[Category ("NotOnMac")]
		public void JoinMulticastGroup2_IPv6 ()
		{
#if NET_2_0
			if (!Socket.OSSupportsIPv6)
#else
			if (!Socket.SupportsIPv6)
#endif
				Assert.Ignore ("IPv6 not enabled.");

			IPAddress mcast_addr = IPAddress.Parse ("ff02::1");

			using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
				client.JoinMulticastGroup (0, mcast_addr);
			}
		}

		[Test] // JoinMulticastGroup (Int32, IPAddress)
		public void JoinMulticastGroup2_MulticastAddr_Null ()
		{
			using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
				try {
					client.JoinMulticastGroup (0, (IPAddress) null);
					Assert.Fail ("#1");
				} catch (ArgumentNullException ex) {
					Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
					Assert.IsNull (ex.InnerException, "#3");
					Assert.IsNotNull (ex.Message, "#4");
					Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
				}
			}
		}

		[Test] // JoinMulticastGroup (Int32, IPAddress)
		public void JoinMulticastGroup2_Socket_Closed ()
		{
			IPAddress mcast_addr = null;

			UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234));
			client.Close ();
			try {
				client.JoinMulticastGroup (0, mcast_addr);
				Assert.Fail ("#1");
			} catch (ObjectDisposedException ex) {
				// Cannot access a disposed object
				Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
			}
		}

		[Test] // JoinMulticastGroup (Int32, IPAddress)
		[Category ("NotWorking")]
		public void JoinMulticastGroup2_Socket_NotBound ()
		{
			IPAddress mcast_addr = IPAddress.Parse ("ff02::1");

			UdpClient client = new UdpClient (AddressFamily.InterNetworkV6);
			try {
				client.JoinMulticastGroup (0, mcast_addr);
				Assert.Fail ("#1");
			} catch (SocketException ex) {
				// An invalid argument was supplied
				Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
				Assert.AreEqual (10022, ex.ErrorCode, "#3");
				Assert.IsNull (ex.InnerException, "#4");
				Assert.IsNotNull (ex.Message, "#5");
				Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
#if NET_2_0
				Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
#endif
			} finally {
				client.Close ();
			}
		}

		[Test] // JoinMulticastGroup (IPAddress, Int32)
		public void JoinMulticastGroup3_IPv4 ()
		{
			IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");

			using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
				client.JoinMulticastGroup (mcast_addr, 0);
			}

			using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
				client.JoinMulticastGroup (mcast_addr, 255);
			}
		}

		[Test] // JoinMulticastGroup (IPAddress, Int32)
		[Category ("NotOnMac")]
		public void JoinMulticastGroup3_IPv6 ()
		{
#if NET_2_0
			if (!Socket.OSSupportsIPv6)
#else
			if (!Socket.SupportsIPv6)
#endif
				Assert.Ignore ("IPv6 not enabled.");

			IPAddress mcast_addr = IPAddress.Parse ("ff02::1");

			using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
				client.JoinMulticastGroup (mcast_addr, 0);
			}

			using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
				client.JoinMulticastGroup (mcast_addr, 255);
			}
		}

		[Test] // JoinMulticastGroup (IPAddress, Int32)
		public void JoinMulticastGroup3_MulticastAddr_Null ()
		{
			using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
				try {
					client.JoinMulticastGroup ((IPAddress) null, int.MaxValue);
					Assert.Fail ("#1");
				} catch (ArgumentNullException ex) {
					Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
					Assert.IsNull (ex.InnerException, "#3");
					Assert.IsNotNull (ex.Message, "#4");
					Assert.AreEqual ("multicastAddr", ex.ParamName, "#5");
				}
			}
		}

		[Test] // JoinMulticastGroup (IPAddress, Int32)
		public void JoinMulticastGroup3_Socket_Closed ()
		{
			IPAddress mcast_addr = null;

			UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
			client.Close ();
			try {
				client.JoinMulticastGroup (mcast_addr, 0);
				Assert.Fail ("#1");
			} catch (ObjectDisposedException ex) {
				// Cannot access a disposed object
				Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
			}
		}

		[Test] // JoinMulticastGroup (IPAddress, Int32)
		[Category ("NotWorking")]
		public void JoinMulticastGroup3_Socket_NotBound ()
		{
			IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");

			UdpClient client = new UdpClient (AddressFamily.InterNetwork);
			try {
				client.JoinMulticastGroup (mcast_addr, 5);
				Assert.Fail ("#1");
			} catch (SocketException ex) {
				// An invalid argument was supplied
				Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
				Assert.AreEqual (10022, ex.ErrorCode, "#3");
				Assert.IsNull (ex.InnerException, "#4");
				Assert.IsNotNull (ex.Message, "#5");
				Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
#if NET_2_0
				Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
#endif
			} finally {
				client.Close ();
			}
		}

#if NET_2_0
		[Test] // JoinMulticastGroup (IPAddress, IPAddress)
		public void JoinMulticastGroup4_IPv4 ()
		{
			IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
			IPAddress local_addr = IPAddress.Any;

			using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234))) {
				client.JoinMulticastGroup (mcast_addr, local_addr);
			}
		}

		[Test] // JoinMulticastGroup (IPAddress, IPAddress)
		public void JoinMulticastGroup4_IPv6 ()
		{
			if (!Socket.OSSupportsIPv6)
				Assert.Ignore ("IPv6 not enabled.");

			IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
			IPAddress local_addr = IPAddress.IPv6Any;

			using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.IPv6Any, 1234))) {
				try {
					client.JoinMulticastGroup (mcast_addr, local_addr);
					Assert.Fail ("#1");
				} catch (SocketException ex) {
					// The attempted operation is not supported for the type of
					// object referenced
					Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
					Assert.AreEqual (10045, ex.ErrorCode, "#3");
					Assert.IsNull (ex.InnerException, "#4");
					Assert.IsNotNull (ex.Message, "#5");
					Assert.AreEqual (10045, ex.NativeErrorCode, "#6");
					Assert.AreEqual (SocketError.OperationNotSupported, ex.SocketErrorCode, "#7");
				}
			}
		}

		[Test] // JoinMulticastGroup (IPAddress, IPAddress)
		public void JoinMulticastGroup4_LocalAddress_Null ()
		{
			IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");

			using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
				try {
					client.JoinMulticastGroup (mcast_addr, (IPAddress) null);
					Assert.Fail ("#1");
				} catch (ArgumentNullException ex) {
					Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
					Assert.IsNull (ex.InnerException, "#3");
					Assert.IsNotNull (ex.Message, "#4");
					Assert.AreEqual ("mcint", ex.ParamName, "#5");
				}
			}
		}

		[Test] // JoinMulticastGroup (IPAddress, IPAddress)
		public void JoinMulticastGroup4_MulticastAddr_Null ()
		{
			using (UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Loopback, 1234))) {
				try {
					client.JoinMulticastGroup ((IPAddress) null, IPAddress.Loopback);
					Assert.Fail ("#1");
				} catch (ArgumentNullException ex) {
					Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
					Assert.IsNull (ex.InnerException, "#3");
					Assert.IsNotNull (ex.Message, "#4");
					Assert.AreEqual ("group", ex.ParamName, "#5");
				}
			}
		}

		[Test] // JoinMulticastGroup (IPAddress, IPAddress)
		public void JoinMulticastGroup4_Socket_Closed ()
		{
			IPAddress mcast_addr = null;
			IPAddress local_addr = null;

			UdpClient client = new UdpClient (new IPEndPoint (IPAddress.Any, 1234));
			client.Close ();
			try {
				client.JoinMulticastGroup (mcast_addr, local_addr);
				Assert.Fail ("#1");
			} catch (ObjectDisposedException ex) {
				// Cannot access a disposed object
				Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
				Assert.IsNull (ex.InnerException, "#3");
				Assert.IsNotNull (ex.Message, "#4");
				Assert.AreEqual (typeof (UdpClient).FullName, ex.ObjectName, "#5");
			}
		}

		[Test] // JoinMulticastGroup (IPAddress, IPAddress)
		[Category ("NotWorking")]
		public void JoinMulticastGroup4_Socket_NotBound ()
		{
			IPAddress mcast_addr = IPAddress.Parse ("224.0.0.23");
			IPAddress local_addr = Dns.GetHostEntry (string.Empty).AddressList [0];

			UdpClient client = new UdpClient (AddressFamily.InterNetwork);
			try {
				client.JoinMulticastGroup (mcast_addr, local_addr);
				Assert.Fail ("#1");
			} catch (SocketException ex) {
				// An invalid argument was supplied
				Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
				Assert.AreEqual (10022, ex.ErrorCode, "#3");
				Assert.IsNull (ex.InnerException, "#4");
				Assert.IsNotNull (ex.Message, "#5");
				Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
				Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
			} finally {
				client.Close ();
			}
		}

		[Test]
		public void CloseInReceive ()
		{
			UdpClient client = new UdpClient (50000);
			new Thread(delegate() {
				Thread.Sleep(2000);
				client.Close();
				}).Start();

			bool got_exc = false;
			IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
			try {
				client.Receive(ref ep);
			} catch (SocketException) {
				got_exc = true;
			} finally {
				client.Close ();
			}
			Assert.IsTrue (got_exc);
		}

		// Test for bug 324033
		[Test]
		public void JoinMulticastGroupWithLocal ()
		{
			UdpClient client = new UdpClient (9001);
			IPAddress mcast_addr = IPAddress.Parse ("224.0.0.24");
			IPAddress local_addr = IPAddress.Any;

			try {
				client.JoinMulticastGroup (mcast_addr, local_addr);
			} finally {
				client.Close ();
			}
		}
		
		[Test]
		[ExpectedException (typeof(ArgumentNullException))]
		public void BeginSendNull ()
		{
			UdpClient client = new UdpClient ();
			client.BeginSend (null, 0, null, null);
			client.Close ();
		}
		
		static bool BSSent = false;
		static int BSBytes;
		static ManualResetEvent BSCalledBack = new ManualResetEvent (false);
		
		private static void BSCallback (IAsyncResult asyncResult)
		{
			UdpClient client = (UdpClient)asyncResult.AsyncState;
			
			BSBytes = client.EndSend (asyncResult);
			
			BSSent = true;
			BSCalledBack.Set ();
		}
		
		[Test]
		[Category ("NotOnMac")]
		public void BeginSend ()
		{
			UdpClient client = new UdpClient ();
			byte[] bytes = new byte[] {10, 11, 12, 13};

			try {
				client.BeginSend (bytes, bytes.Length, new AsyncCallback (BSCallback), client);
				Assert.Fail ("BeginSend #1");
			} catch (SocketException ex) {
				Assert.AreEqual (10057, ex.ErrorCode,
						 "BeginSend #2");
			}
			
			try {
				client.BeginSend (bytes, bytes.Length, null, new AsyncCallback (BSCallback), client);
				Assert.Fail ("BeginSend #3");
			} catch (SocketException ex) {
				Assert.AreEqual (10057, ex.ErrorCode,
						 "BeginSend #4");
			}

			IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1236);
			
			BSCalledBack.Reset ();
			
			client.BeginSend (bytes, bytes.Length, ep,
					  new AsyncCallback (BSCallback),
					  client);
			if (BSCalledBack.WaitOne (2000, false) == false) {
				Assert.Fail ("BeginSend wait timed out");
			}
			
			Assert.AreEqual (true, BSSent, "BeginSend #5");
			Assert.AreEqual (4, BSBytes, "BeginSend #6");

			client.Close ();
		}
		
		static bool BRReceived = false;
		static byte[] BRBytes;
		static IPEndPoint BRFrom;
		static ManualResetEvent BRCalledBack = new ManualResetEvent (false);
		
		private static void BRCallback (IAsyncResult asyncResult)
		{
			UdpClient client = (UdpClient)asyncResult.AsyncState;
			
			BRBytes = client.EndReceive (asyncResult, ref BRFrom);
			
			BRReceived = true;
			BRCalledBack.Set ();
		}
		
		[Test]
		[Category ("NotOnMac")]
		public void BeginReceive ()
		{
			UdpClient client = new UdpClient (1237);
			
			BRCalledBack.Reset ();
			
			client.BeginReceive (BRCallback, client);

			IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1237);
			byte[] send_bytes = new byte[] {10, 11, 12, 13};
			client.Send (send_bytes, send_bytes.Length, ep);

			if (BRCalledBack.WaitOne (2000, false) == false) {
				Assert.Fail ("BeginReceive wait timed out");
			}
			
			Assert.AreEqual (true, BRReceived, "BeginReceive #1");
			Assert.AreEqual (4, BRBytes.Length, "BeginReceive #2");
			Assert.AreEqual (ep. Port, BRFrom.Port, "BeginReceive #3");
			Assert.AreEqual (ep.Address, BRFrom.Address, "BeginReceive #4");

			client.Close ();
		}
		
		[Test]
		[Category ("NotOnMac")]
		public void Available ()
		{
			UdpClient client = new UdpClient (1238);
			IPEndPoint ep = new IPEndPoint (Dns.GetHostEntry (string.Empty).AddressList[0], 1238);
			byte[] bytes = new byte[] {10, 11, 12, 13};
			
			client.Send (bytes, bytes.Length, ep);
			int avail = client.Available;
			
			Assert.AreEqual (bytes.Length, avail, "Available #1");

			client.Close ();
		}
		
		[Test]
		[Category ("NotWorking")]  // Using PMTU settings workaround on Linux, default true
		public void DontFragmentDefault ()
		{
			UdpClient client = new UdpClient ();
			
			/* Ignore the docs, testing shows the default
			 * here is in fact false
			 */
			Assert.AreEqual (false, client.DontFragment, "DontFragmentDefault");

			client.Close ();
		}
		
		[Test]
		public void EnableBroadcastDefault ()
		{
			UdpClient client = new UdpClient ();
			
			Assert.AreEqual (false, client.EnableBroadcast, "EnableBroadcastDefault");

			client.Close ();
		}
		
		/* Can't test the default for ExclusiveAddressUse as
		 * it's different on different versions and service
		 * packs of windows
		 */
		[Test]
		[Category ("NotWorking")] // Not supported on Linux
		public void ExclusiveAddressUseUnbound ()
		{
			UdpClient client = new UdpClient ();

			client.ExclusiveAddressUse = true;

			Assert.AreEqual (true, client.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");

			client.Close ();
		}
		
		[Test]
		[ExpectedException (typeof(InvalidOperationException))]
		[Category ("NotWorking")] // Not supported on Linux
		public void ExclusiveAddressUseBound ()
		{
			UdpClient client = new UdpClient (1239);

			client.ExclusiveAddressUse = true;

			client.Close ();
		}
		
		[Test]
		public void MulticastLoopbackDefault ()
		{
			UdpClient client = new UdpClient ();
			
			Assert.AreEqual (true, client.MulticastLoopback, "MulticastLoopbackDefault");

			client.Close ();
		}
		
		/* No test for Ttl default as it is platform dependent */
#endif

		class MyUdpClient : UdpClient
		{
			public MyUdpClient ()
			{
			}

			public MyUdpClient (AddressFamily family)
				: base (family)
			{
			}

			public MyUdpClient (Int32 port)
				: base (port)
			{
			}


			public MyUdpClient (IPEndPoint localEP)
				: base (localEP)
			{
			}

			public MyUdpClient (int port, AddressFamily family)
				: base (port, family)
			{
			}

			public MyUdpClient (string hostname, int port)
				: base (hostname, port)
			{
			}

			public new bool Active {
				get { return base.Active; }
				set { base.Active = value; }
			}

#if ONLY_1_1
			public new Socket Client {
				get { return base.Client; }
				set { base.Client = value; }
			}
#endif
		}
	}
}