1 // System.Net.Sockets.SocketTest.cs
4 // Brad Fitzpatrick (brad@danga.com)
5 // Gonzalo Paniagua Javier (gonzalo@novell.com)
7 // (C) Copyright 2003 Brad Fitzpatrick
8 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
12 using System.Diagnostics;
14 using System.Collections;
15 using System.Threading;
16 using System.Reflection;
17 using System.Text.RegularExpressions;
18 using System.Threading.Tasks;
20 using System.Net.Sockets;
21 using NUnit.Framework;
24 using System.Collections.Generic;
26 using MonoTests.Helpers;
28 namespace MonoTests.System.Net.Sockets
31 public class SocketTest
33 // note: also used in SocketCas tests
34 public const string BogusAddress = "192.168.244.244";
35 public const int BogusPort = 23483;
38 #if FEATURE_NO_BSD_SOCKETS
39 [ExpectedException (typeof (PlatformNotSupportedException))]
41 public void ConnectIPAddressAny ()
43 IPEndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
45 /* UDP sockets use Any to disconnect
47 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
52 } catch (SocketException ex) {
53 Assert.AreEqual (10049, ex.ErrorCode, "#2");
58 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
63 } catch (SocketException ex) {
64 Assert.AreEqual (10049, ex.ErrorCode, "#4");
69 [Ignore ("Bug #75158")] // Looks like MS fails after the .ctor, when you try to use the socket
70 public void IncompatibleAddress ()
72 IPEndPoint epIPv6 = new IPEndPoint (IPAddress.IPv6Any,
73 NetworkHelpers.FindFreePort ());
76 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)) {
81 } catch (SocketException ex) {
82 // address incompatible with protocol
83 int expectedError = 10047;
84 Assert.AreEqual (expectedError, ex.ErrorCode,
90 [Category ("InetAccess")]
91 #if FEATURE_NO_BSD_SOCKETS
92 [ExpectedException (typeof (PlatformNotSupportedException))]
94 public void BogusEndConnect ()
96 IPAddress ipOne = IPAddress.Parse (BogusAddress);
97 IPEndPoint ipEP = new IPEndPoint (ipOne, BogusPort);
98 Socket sock = new Socket (ipEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
99 IAsyncResult ar = sock.BeginConnect (ipEP, null, null);
102 // should raise an exception because connect was bogus
103 sock.EndConnect (ar);
105 } catch (SocketException ex) {
106 // Actual error code depends on network configuration.
107 var error = (SocketError) ex.ErrorCode;
108 Assert.That (error == SocketError.TimedOut ||
109 error == SocketError.ConnectionRefused ||
110 error == SocketError.NetworkUnreachable ||
111 error == SocketError.HostUnreachable, "#2");
116 [ExpectedException (typeof (ArgumentNullException))]
117 public void SelectEmpty ()
119 ArrayList list = new ArrayList ();
120 Socket.Select (list, list, list, 1000);
123 private bool BlockingConnect (bool block, int port)
125 IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, port);
126 Socket server = new Socket(AddressFamily.InterNetwork,
130 server.Blocking=block;
134 Socket conn = new Socket (AddressFamily.InterNetwork,
139 Socket client = null;
140 var sw = Stopwatch.StartNew ();
141 while (sw.ElapsedMilliseconds < 100)
144 client = server.Accept();
147 catch (SocketException ex) {
148 if (ex.SocketErrorCode == SocketError.WouldBlock)
153 Assert.IsNotNull (client, "Couldn't accept a client connection within 100ms.");
154 bool client_block = client.Blocking;
160 return(client_block);
164 #if FEATURE_NO_BSD_SOCKETS
165 [ExpectedException (typeof (PlatformNotSupportedException))]
167 public void AcceptBlockingStatus()
170 var port = NetworkHelpers.FindFreePort ();
172 block = BlockingConnect(true, port);
173 Assert.AreEqual (block, true, "BlockingStatus01");
175 block = BlockingConnect(false, port);
176 Assert.AreEqual (block, false, "BlockingStatus02");
179 static bool CFAConnected = false;
180 static ManualResetEvent CFACalledBack;
182 private static void CFACallback (IAsyncResult asyncResult)
184 Socket sock = (Socket)asyncResult.AsyncState;
185 CFAConnected = sock.Connected;
187 if (sock.Connected) {
188 sock.EndConnect (asyncResult);
191 CFACalledBack.Set ();
194 [Test] // Connect (IPEndPoint)
195 public void Connect1_RemoteEP_Null ()
197 Socket s = new Socket (AddressFamily.InterNetwork,
198 SocketType.Stream, ProtocolType.Tcp);
200 s.Connect ((IPEndPoint) null);
202 } catch (ArgumentNullException ex) {
203 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
204 Assert.IsNull (ex.InnerException, "#3");
205 Assert.IsNotNull (ex.Message, "#4");
206 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
211 #if FEATURE_NO_BSD_SOCKETS
212 [ExpectedException (typeof (PlatformNotSupportedException))]
214 public void ConnectFailAsync ()
216 Socket sock = new Socket (AddressFamily.InterNetwork,
219 sock.Blocking = false;
220 CFACalledBack = new ManualResetEvent (false);
221 CFACalledBack.Reset ();
223 /* Need a port that is not being used for
226 sock.BeginConnect (new IPEndPoint (IPAddress.Loopback,
227 NetworkHelpers.FindFreePort ()),
228 new AsyncCallback (CFACallback),
230 CFACalledBack.WaitOne ();
232 Assert.AreEqual (CFAConnected, false, "ConnectFail");
236 #if FEATURE_NO_BSD_SOCKETS
237 [ExpectedException (typeof (PlatformNotSupportedException))]
239 public void SetSocketOptionBoolean ()
241 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
242 Socket sock = new Socket (ep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
244 sock.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
250 #if FEATURE_NO_BSD_SOCKETS
251 [ExpectedException (typeof (PlatformNotSupportedException))]
253 public void TestSelect1 ()
255 Socket srv = CreateServer (NetworkHelpers.FindFreePort ());
256 ClientSocket clnt = new ClientSocket (srv.LocalEndPoint);
257 Thread th = new Thread (new ThreadStart (clnt.ConnectSleepClose));
263 ArrayList list = new ArrayList ();
264 ArrayList empty = new ArrayList ();
266 Socket.Select (list, empty, empty, 100);
267 Assert.AreEqual (0, empty.Count, "#01");
268 Assert.AreEqual (1, list.Count, "#02");
269 Socket.Select (empty, list, empty, 100);
270 Assert.AreEqual (0, empty.Count, "#03");
271 Assert.AreEqual (1, list.Count, "#04");
272 Socket.Select (list, empty, empty, -1);
273 Assert.AreEqual (0, empty.Count, "#05");
274 Assert.AreEqual (1, list.Count, "#06");
275 // Need to read the 10 bytes from the client to avoid a RST
276 byte [] bytes = new byte [10];
285 static Socket CreateServer (int port)
287 Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
288 sock.Bind (new IPEndPoint (IPAddress.Loopback, port));
297 public ClientSocket (EndPoint ep)
300 sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
303 public void ConnectSleepClose ()
312 byte [] b = new byte [10];
317 byte[] buf = new byte[100];
320 [ExpectedException (typeof (ObjectDisposedException))]
321 public void Disposed2 ()
323 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
330 [ExpectedException (typeof (ObjectDisposedException))]
331 public void Disposed6 ()
333 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
340 [ExpectedException (typeof (ObjectDisposedException))]
341 public void Disposed7 ()
343 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
350 [ExpectedException (typeof (ObjectDisposedException))]
351 public void Disposed15 ()
353 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
360 [ExpectedException (typeof (ObjectDisposedException))]
361 public void Disposed16 ()
363 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
370 [ExpectedException (typeof (ObjectDisposedException))]
371 public void Disposed17 ()
373 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
380 [ExpectedException (typeof (ObjectDisposedException))]
381 public void Disposed18 ()
383 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
386 s.Send (buf, 0, 10, 0);
390 #if FEATURE_NO_BSD_SOCKETS
391 [ExpectedException (typeof (PlatformNotSupportedException))]
393 [ExpectedException (typeof (ObjectDisposedException))]
395 public void Disposed19 ()
397 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
398 EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
401 s.SendTo (buf, 0, ep);
405 #if FEATURE_NO_BSD_SOCKETS
406 [ExpectedException (typeof (PlatformNotSupportedException))]
408 [ExpectedException (typeof (ObjectDisposedException))]
410 public void Disposed20 ()
412 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
413 EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
416 s.SendTo (buf, 10, 0, ep);
420 #if FEATURE_NO_BSD_SOCKETS
421 [ExpectedException (typeof (PlatformNotSupportedException))]
423 [ExpectedException (typeof (ObjectDisposedException))]
425 public void Disposed21 ()
427 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
428 EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
431 s.SendTo (buf, 0, 10, 0, ep);
435 #if FEATURE_NO_BSD_SOCKETS
436 [ExpectedException (typeof (PlatformNotSupportedException))]
438 [ExpectedException (typeof (ObjectDisposedException))]
440 public void Disposed22 ()
442 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
443 EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
450 [ExpectedException (typeof (ObjectDisposedException))]
451 public void Disposed23 ()
453 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
460 #if FEATURE_NO_BSD_SOCKETS
461 [ExpectedException (typeof (PlatformNotSupportedException))]
463 public void GetHashCodeTest ()
465 Socket server = new Socket (AddressFamily.InterNetwork,
466 SocketType.Stream, ProtocolType.Tcp);
467 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
468 NetworkHelpers.FindFreePort ());
472 Socket client = new Socket (AddressFamily.InterNetwork,
473 SocketType.Stream, ProtocolType.Tcp);
474 int hashcodeA = client.GetHashCode ();
476 int hashcodeB = client.GetHashCode ();
477 Assert.AreEqual (hashcodeA, hashcodeB, "#1");
479 int hashcodeC = client.GetHashCode ();
480 Assert.AreEqual (hashcodeB, hashcodeC, "#2");
484 static ManualResetEvent SocketError_event = new ManualResetEvent (false);
486 private static void SocketError_callback (IAsyncResult ar)
488 Socket sock = (Socket)ar.AsyncState;
491 sock.EndConnect (ar);
494 SocketError_event.Set ();
498 [Category ("RequiresBSDSockets")] // This verifies particular error codes, which we don't care about when nothing's working anyway.
499 public void SocketErrorTest ()
501 Socket sock = new Socket (AddressFamily.InterNetwork,
504 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
507 SocketError_event.Reset ();
509 sock.Blocking = false;
510 sock.BeginConnect (ep, new AsyncCallback(SocketError_callback),
513 if (SocketError_event.WaitOne (2000, false) == false) {
514 Assert.Fail ("SocketError wait timed out");
517 Assert.AreEqual (false, sock.Connected, "SocketError #1");
521 error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
522 Assert.AreEqual (10061, error, "SocketError #2");
524 error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
525 Assert.AreEqual (10061, error, "SocketError #3");
532 public void SocketInformationCtor ()
537 public void DontFragmentDefaultTcp ()
539 Socket sock = new Socket (AddressFamily.InterNetwork,
543 Assert.AreEqual (false, sock.DontFragment, "DontFragmentDefaultTcp");
549 [Category ("NotWorking")] // DontFragment doesn't work
550 public void DontFragmentChangeTcp ()
552 Socket sock = new Socket (AddressFamily.InterNetwork,
556 sock.DontFragment = true;
558 Assert.AreEqual (true, sock.DontFragment, "DontFragmentChangeTcp");
564 public void DontFragmentDefaultUdp ()
566 Socket sock = new Socket (AddressFamily.InterNetwork,
570 Assert.AreEqual (false, sock.DontFragment, "DontFragmentDefaultUdp");
576 [Category ("NotWorking")] // DontFragment doesn't work
577 public void DontFragmentChangeUdp ()
579 Socket sock = new Socket (AddressFamily.InterNetwork,
583 sock.DontFragment = true;
585 Assert.AreEqual (true, sock.DontFragment, "DontFragmentChangeUdp");
591 [ExpectedException (typeof(ObjectDisposedException))]
592 public void DontFragmentClosed ()
594 Socket sock = new Socket (AddressFamily.InterNetwork,
600 bool val = sock.DontFragment;
604 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
605 public void DontFragment ()
607 Socket sock = new Socket (AddressFamily.NetBios,
608 SocketType.Seqpacket,
609 ProtocolType.Unspecified);
612 sock.DontFragment = true;
613 Assert.Fail ("DontFragment #1");
614 } catch (NotSupportedException) {
621 public void EnableBroadcastDefaultTcp ()
623 Socket sock = new Socket (AddressFamily.InterNetwork,
628 bool value = sock.EnableBroadcast;
629 Assert.Fail ("EnableBroadcastDefaultTcp #1");
630 } catch (SocketException ex) {
631 Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastDefaultTcp #2");
638 public void EnableBroadcastDefaultUdp ()
640 Socket sock = new Socket (AddressFamily.InterNetwork,
644 Assert.AreEqual (false, sock.EnableBroadcast, "EnableBroadcastDefaultUdp");
650 public void EnableBroadcastChangeTcp ()
652 Socket sock = new Socket (AddressFamily.InterNetwork,
657 sock.EnableBroadcast = true;
658 Assert.Fail ("EnableBroadcastChangeTcp #1");
659 } catch (SocketException ex) {
660 Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastChangeTcp #2");
667 public void EnableBroadcastChangeUdp ()
669 Socket sock = new Socket (AddressFamily.InterNetwork,
673 sock.EnableBroadcast = true;
675 Assert.AreEqual (true, sock.EnableBroadcast, "EnableBroadcastChangeUdp");
681 [ExpectedException (typeof(ObjectDisposedException))]
682 public void EnableBroadcastClosed ()
684 Socket sock = new Socket (AddressFamily.InterNetwork,
690 bool val = sock.EnableBroadcast;
693 /* Can't test the default for ExclusiveAddressUse as
694 * it's different on different versions and service
698 [Category ("NotWorking")] // Not supported on Linux
699 public void ExclusiveAddressUseUnbound ()
701 Socket sock = new Socket (AddressFamily.InterNetwork,
705 sock.ExclusiveAddressUse = true;
707 Assert.AreEqual (true, sock.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
713 [ExpectedException (typeof(InvalidOperationException))]
714 [Category ("NotWorking")] // Not supported on Linux
715 public void ExclusiveAddressUseBound ()
717 Socket sock = new Socket (AddressFamily.InterNetwork,
721 sock.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
722 sock.ExclusiveAddressUse = true;
727 [ExpectedException (typeof(ObjectDisposedException))]
728 public void ExclusiveAddressUseClosed ()
730 Socket sock = new Socket (AddressFamily.InterNetwork,
736 bool val = sock.ExclusiveAddressUse;
740 #if FEATURE_NO_BSD_SOCKETS
741 [ExpectedException (typeof (PlatformNotSupportedException))]
743 public void IsBoundTcp ()
745 Socket sock = new Socket (AddressFamily.InterNetwork,
748 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
751 Assert.AreEqual (false, sock.IsBound, "IsBoundTcp #1");
754 Assert.AreEqual (true, sock.IsBound, "IsBoundTcp #2");
758 Socket sock2 = new Socket (AddressFamily.InterNetwork,
762 Assert.AreEqual (false, sock2.IsBound, "IsBoundTcp #3");
765 Assert.AreEqual (true, sock2.IsBound, "IsBoundTcp #4");
768 Assert.AreEqual (true, sock2.IsBound, "IsBoundTcp #5");
771 Assert.AreEqual (true, sock.IsBound, "IsBoundTcp #6");
775 #if FEATURE_NO_BSD_SOCKETS
776 [ExpectedException (typeof (PlatformNotSupportedException))]
778 public void IsBoundUdp ()
780 Socket sock = new Socket (AddressFamily.InterNetwork,
783 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
786 Assert.AreEqual (false, sock.IsBound, "IsBoundUdp #1");
789 Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #2");
792 Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #3");
795 sock = new Socket (AddressFamily.InterNetwork,
799 Assert.AreEqual (false, sock.IsBound, "IsBoundUdp #4");
802 Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #5");
805 Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #6");
809 /* Should not throw an exception */
810 public void IsBoundClosed ()
812 Socket sock = new Socket (AddressFamily.InterNetwork,
818 bool val = sock.IsBound;
821 /* Nothing much to test for LingerState */
824 public void MulticastLoopbackDefaultTcp ()
826 Socket sock = new Socket (AddressFamily.InterNetwork,
831 bool value = sock.MulticastLoopback;
832 Assert.Fail ("MulticastLoopbackDefaultTcp #1");
833 } catch (SocketException ex) {
834 Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackDefaultTcp #2");
841 public void MulticastLoopbackChangeTcp ()
843 Socket sock = new Socket (AddressFamily.InterNetwork,
848 sock.MulticastLoopback = false;
849 Assert.Fail ("MulticastLoopbackChangeTcp #1");
850 } catch (SocketException ex) {
851 Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackChangeTcp #2");
858 public void MulticastLoopbackDefaultUdp ()
860 Socket sock = new Socket (AddressFamily.InterNetwork,
864 Assert.AreEqual (true, sock.MulticastLoopback, "MulticastLoopbackDefaultUdp");
870 public void MulticastLoopbackChangeUdp ()
872 Socket sock = new Socket (AddressFamily.InterNetwork,
876 sock.MulticastLoopback = false;
878 Assert.AreEqual (false, sock.MulticastLoopback, "MulticastLoopbackChangeUdp");
884 [ExpectedException (typeof(ObjectDisposedException))]
885 public void MulticastLoopbackClosed ()
887 Socket sock = new Socket (AddressFamily.InterNetwork,
893 bool val = sock.MulticastLoopback;
896 /* OSSupportsIPv6 depends on the environment */
899 [Category("NotWorking")] // We have different defaults for perf reasons
900 public void ReceiveBufferSizeDefault ()
902 Socket sock = new Socket (AddressFamily.InterNetwork,
906 Assert.AreEqual (8192, sock.ReceiveBufferSize, "ReceiveBufferSizeDefault");
912 [Category("NotWorking")] // We have different defaults for perf reasons
913 public void ReceiveBufferSizeDefaultUdp ()
915 Socket sock = new Socket (AddressFamily.InterNetwork,
919 Assert.AreEqual (8192, sock.ReceiveBufferSize, "ReceiveBufferSizeDefaultUdp");
925 public void ReceiveBufferSizeChange ()
927 Socket sock = new Socket (AddressFamily.InterNetwork,
931 sock.ReceiveBufferSize = 16384;
933 Assert.AreEqual (16384, sock.ReceiveBufferSize, "ReceiveBufferSizeChange");
939 [Category("NotWorking")] // We cannot totally remove buffers (minimum is set to 256
940 public void BuffersCheck_None ()
942 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
943 int original = s.ReceiveBufferSize;
944 s.ReceiveBufferSize = 0;
945 Assert.AreEqual (0, s.ReceiveBufferSize, "ReceiveBufferSize " + original.ToString ());
947 original = s.SendBufferSize;
948 s.SendBufferSize = 0;
949 Assert.AreEqual (0, s.SendBufferSize, "SendBufferSize " + original.ToString ());
954 [ExpectedException (typeof(ObjectDisposedException))]
955 public void ReceiveBufferSizeClosed ()
957 Socket sock = new Socket (AddressFamily.InterNetwork,
963 int val = sock.ReceiveBufferSize;
967 [Category("NotWorking")] // We have different defaults for perf reasons
968 public void SendBufferSizeDefault ()
970 Socket sock = new Socket (AddressFamily.InterNetwork,
974 Assert.AreEqual (8192, sock.SendBufferSize, "SendBufferSizeDefault");
980 [Category("NotWorking")] // We have different defaults for perf reasons
981 public void SendBufferSizeDefaultUdp ()
983 Socket sock = new Socket (AddressFamily.InterNetwork,
987 Assert.AreEqual (8192, sock.SendBufferSize, "SendBufferSizeDefaultUdp");
993 public void SendBufferSizeChange ()
995 Socket sock = new Socket (AddressFamily.InterNetwork,
999 sock.SendBufferSize = 16384;
1001 Assert.AreEqual (16384, sock.SendBufferSize, "SendBufferSizeChange");
1007 [ExpectedException (typeof(ObjectDisposedException))]
1008 public void SendBufferSizeClosed ()
1010 Socket sock = new Socket (AddressFamily.InterNetwork,
1016 int val = sock.SendBufferSize;
1019 /* No test for TTL default as it's platform dependent */
1021 public void TtlChange ()
1023 Socket sock = new Socket (AddressFamily.InterNetwork,
1029 Assert.AreEqual (255, sock.Ttl, "TtlChange");
1035 public void TtlChangeOverflow ()
1037 Socket sock = new Socket (AddressFamily.InterNetwork,
1043 Assert.Fail ("TtlChangeOverflow #1");
1044 } catch (ArgumentOutOfRangeException ex) {
1045 Assert.AreEqual ("value", ex.ParamName,
1046 "TtlChangeOverflow #2");
1052 /* Apparently you can set TTL=0 on the ms runtime!!
1055 Assert.Fail ("TtlChangeOverflow #4");
1056 } catch (SocketException ex) {
1057 Assert.AreEqual (10022, ex.ErrorCode,
1058 "TtlChangeOverflow #5");
1065 [ExpectedException (typeof(ObjectDisposedException))]
1066 public void TtlClosed ()
1068 Socket sock = new Socket (AddressFamily.InterNetwork,
1078 public void UseOnlyOverlappedIODefault ()
1080 Socket sock = new Socket (AddressFamily.InterNetwork,
1084 Assert.AreEqual (false, sock.UseOnlyOverlappedIO, "UseOnlyOverlappedIODefault");
1090 // We need this because the Linux kernel in certain configurations
1091 // will end up rounding up the values passed on to the kernel
1092 // for socket send/receive timeouts.
1094 int Approximate (int target, int value)
1098 if (value > target-10 && value < target+10)
1104 public void UseOnlyOverlappedIOChange ()
1106 Socket sock = new Socket (AddressFamily.InterNetwork,
1110 sock.UseOnlyOverlappedIO = true;
1112 Assert.AreEqual (true, sock.UseOnlyOverlappedIO, "UseOnlyOverlappedIOChange");
1118 /* Should not throw an exception */
1119 public void UseOnlyOverlappedIOClosed ()
1121 Socket sock = new Socket (AddressFamily.InterNetwork,
1127 bool val = sock.UseOnlyOverlappedIO;
1131 public void SendTimeoutDefault ()
1133 Socket sock = new Socket (AddressFamily.InterNetwork,
1137 Assert.AreEqual (0, sock.SendTimeout, "SendTimeoutDefault");
1143 public void SendTimeoutChange ()
1145 Socket sock = new Socket (AddressFamily.InterNetwork,
1149 /* Should be rounded up to 500, according to
1150 * the MSDN docs, but the MS runtime doesn't
1152 sock.SendTimeout = 50;
1153 Assert.AreEqual (50, Approximate (50, sock.SendTimeout), "SendTimeoutChange #1");
1155 sock.SendTimeout = 2000;
1156 Assert.AreEqual (2000, Approximate (2000, sock.SendTimeout), "SendTimeoutChange #2");
1158 sock.SendTimeout = 0;
1159 Assert.AreEqual (0, Approximate (0, sock.SendTimeout), "SendTimeoutChange #3");
1161 /* Should be the same as setting 0 */
1162 sock.SendTimeout = -1;
1163 Assert.AreEqual (0, sock.SendTimeout, "SendTimeoutChange #4");
1165 sock.SendTimeout = 65536;
1166 Assert.AreEqual (65536, Approximate (65536, sock.SendTimeout), "SendTimeoutChange #5");
1169 sock.SendTimeout = -2;
1170 Assert.Fail ("SendTimeoutChange #8");
1171 } catch (ArgumentOutOfRangeException) {
1178 [ExpectedException (typeof(ObjectDisposedException))]
1179 public void SendTimeoutClosed ()
1181 Socket sock = new Socket (AddressFamily.InterNetwork,
1187 int val = sock.SendTimeout;
1191 public void ReceiveTimeoutDefault ()
1193 Socket sock = new Socket (AddressFamily.InterNetwork,
1197 Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutDefault");
1203 public void ReceiveTimeoutChange ()
1205 Socket sock = new Socket (AddressFamily.InterNetwork,
1209 sock.ReceiveTimeout = 50;
1210 Assert.AreEqual (50, Approximate (50, sock.ReceiveTimeout), "ReceiveTimeoutChange #1");
1212 sock.ReceiveTimeout = 2000;
1213 Assert.AreEqual (2000, Approximate (2000, sock.ReceiveTimeout), "ReceiveTimeoutChange #2");
1215 sock.ReceiveTimeout = 0;
1216 Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutChange #3");
1218 /* Should be the same as setting 0 */
1219 sock.ReceiveTimeout = -1;
1220 Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutChange #4");
1222 sock.ReceiveTimeout = 65536;
1223 Assert.AreEqual (65536, Approximate (65536, sock.ReceiveTimeout), "ReceiveTimeoutChange #5");
1226 sock.ReceiveTimeout = -2;
1227 Assert.Fail ("ReceiveTimeoutChange #8");
1228 } catch (ArgumentOutOfRangeException) {
1235 [ExpectedException (typeof(ObjectDisposedException))]
1236 public void ReceiveTimeoutClosed ()
1238 Socket sock = new Socket (AddressFamily.InterNetwork,
1244 int val = sock.ReceiveTimeout;
1248 public void NoDelayDefaultTcp ()
1250 Socket sock = new Socket (AddressFamily.InterNetwork,
1254 Assert.AreEqual (false, sock.NoDelay, "NoDelayDefaultTcp");
1260 public void NoDelayChangeTcp ()
1262 Socket sock = new Socket (AddressFamily.InterNetwork,
1266 sock.NoDelay = true;
1268 Assert.AreEqual (true, sock.NoDelay, "NoDelayChangeTcp");
1274 public void NoDelayDefaultUdp ()
1276 Socket sock = new Socket (AddressFamily.InterNetwork,
1281 bool val = sock.NoDelay;
1282 Assert.Fail ("NoDelayDefaultUdp #1");
1283 } catch (SocketException ex) {
1284 Assert.AreEqual (10042, ex.ErrorCode,
1285 "NoDelayDefaultUdp #2");
1292 public void NoDelayChangeUdp ()
1294 Socket sock = new Socket (AddressFamily.InterNetwork,
1299 sock.NoDelay = true;
1300 Assert.Fail ("NoDelayChangeUdp #1");
1301 } catch (SocketException ex) {
1302 Assert.AreEqual (10042, ex.ErrorCode,
1303 "NoDelayChangeUdp #2");
1310 [ExpectedException (typeof(ObjectDisposedException))]
1311 public void NoDelayClosed ()
1313 Socket sock = new Socket (AddressFamily.InterNetwork,
1319 bool val = sock.NoDelay;
1322 static bool BAAccepted = false;
1323 static Socket BASocket = null;
1324 static ManualResetEvent BACalledBack = new ManualResetEvent (false);
1326 private static void BACallback (IAsyncResult asyncResult)
1328 Socket sock = (Socket)asyncResult.AsyncState;
1330 BASocket = sock.EndAccept (asyncResult);
1333 BACalledBack.Set ();
1337 [ExpectedException (typeof(InvalidOperationException))]
1338 public void BeginAcceptNotBound ()
1340 Socket sock = new Socket (AddressFamily.InterNetwork,
1344 sock.BeginAccept (BACallback, sock);
1350 #if FEATURE_NO_BSD_SOCKETS
1351 [ExpectedException (typeof (PlatformNotSupportedException))]
1353 [ExpectedException (typeof(InvalidOperationException))]
1355 public void BeginAcceptNotListening ()
1357 Socket sock = new Socket (AddressFamily.InterNetwork,
1361 sock.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
1363 sock.BeginAccept (BACallback, sock);
1369 #if FEATURE_NO_BSD_SOCKETS
1370 [ExpectedException (typeof (PlatformNotSupportedException))]
1372 public void BeginAccept ()
1374 Socket sock = new Socket (AddressFamily.InterNetwork,
1377 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1378 NetworkHelpers.FindFreePort ());
1383 BACalledBack.Reset ();
1385 sock.BeginAccept (BACallback, sock);
1387 Socket conn = new Socket (AddressFamily.InterNetwork,
1393 if (BACalledBack.WaitOne (2000, false) == false) {
1394 Assert.Fail ("BeginAccept wait timed out");
1397 Assert.AreEqual (true, BAAccepted, "BeginAccept #1");
1398 Assert.AreEqual (true, BASocket.Connected, "BeginAccept #2");
1399 Assert.AreEqual (false, sock.Connected, "BeginAccept #3");
1400 Assert.AreEqual (true, conn.Connected, "BeginAccept #4");
1408 [ExpectedException (typeof(ObjectDisposedException))]
1409 public void BeginAcceptClosed ()
1411 Socket sock = new Socket (AddressFamily.InterNetwork,
1417 sock.BeginAccept (BACallback, sock);
1420 static bool BADAccepted = false;
1421 static Socket BADSocket = null;
1422 static byte[] BADBytes;
1423 static int BADByteCount;
1424 static ManualResetEvent BADCalledBack = new ManualResetEvent (false);
1426 private static void BADCallback (IAsyncResult asyncResult)
1428 Socket sock = (Socket)asyncResult.AsyncState;
1430 BADSocket = sock.EndAccept (out BADBytes,
1435 BADCalledBack.Set ();
1439 #if FEATURE_NO_BSD_SOCKETS
1440 [ExpectedException (typeof (PlatformNotSupportedException))]
1442 public void BeginAcceptData ()
1444 Socket sock = new Socket (AddressFamily.InterNetwork,
1447 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1448 NetworkHelpers.FindFreePort ());
1453 BADCalledBack.Reset ();
1455 sock.BeginAccept (256, BADCallback, sock);
1457 Socket conn = new Socket (AddressFamily.InterNetwork,
1460 byte[] send_bytes = new byte[] {10, 11, 12, 13};
1463 conn.Send (send_bytes);
1465 if (BADCalledBack.WaitOne (2000, false) == false) {
1466 Assert.Fail ("BeginAcceptData wait timed out");
1469 Assert.AreEqual (true, BADAccepted, "BeginAcceptData #1");
1470 Assert.AreEqual (true, BADSocket.Connected, "BeginAcceptData #2");
1471 Assert.AreEqual (false, sock.Connected, "BeginAcceptData #3");
1472 Assert.AreEqual (true, conn.Connected, "BeginAcceptData #4");
1473 Assert.AreEqual (send_bytes.Length, BADByteCount, "BeginAcceptData #5");
1475 /* The MS runtime gives the returned data in a
1476 * much bigger array. TODO: investigate
1477 * whether it the size correlates to the first
1478 * parameter in BeginAccept()
1480 Assert.IsFalse (BADBytes.Length == send_bytes.Length,
1481 "BeginAcceptData #6");
1483 for(int i = 0; i < send_bytes.Length; i++) {
1484 Assert.AreEqual (send_bytes[i], BADBytes[i], "BeginAcceptData #" + (i+7).ToString ());
1493 [ExpectedException (typeof(ObjectDisposedException))]
1494 public void BeginAcceptDataClosed ()
1496 Socket sock = new Socket (AddressFamily.InterNetwork,
1502 sock.BeginAccept (256, BADCallback, sock);
1506 #if FEATURE_NO_BSD_SOCKETS
1507 [ExpectedException (typeof (PlatformNotSupportedException))]
1509 public void BeginAcceptSocketUdp ()
1511 Socket sock = new Socket (AddressFamily.InterNetwork,
1514 Socket acc = new Socket (AddressFamily.InterNetwork,
1518 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1519 NetworkHelpers.FindFreePort ());
1525 sock.BeginAccept (acc, 256, BADCallback, sock);
1526 Assert.Fail ("BeginAcceptSocketUdp #1");
1527 } catch (SocketException ex) {
1528 Assert.AreEqual (10022, ex.ErrorCode, "BeginAcceptSocketUdp #2");
1536 #if FEATURE_NO_BSD_SOCKETS
1537 [ExpectedException (typeof (PlatformNotSupportedException))]
1539 public void BeginAcceptSocketBound ()
1541 Socket sock = new Socket (AddressFamily.InterNetwork,
1544 Socket acc = new Socket (AddressFamily.InterNetwork,
1548 IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
1549 NetworkHelpers.FindFreePort ());
1551 IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
1552 NetworkHelpers.FindFreePort ());
1560 sock.BeginAccept (acc, 256, BADCallback, sock);
1561 Assert.Fail ("BeginAcceptSocketBound #1");
1562 } catch (InvalidOperationException) {
1570 #if FEATURE_NO_BSD_SOCKETS
1571 [ExpectedException (typeof (PlatformNotSupportedException))]
1573 public void BeginAcceptSocket ()
1575 Socket sock = new Socket (AddressFamily.InterNetwork,
1578 Socket acc = new Socket (AddressFamily.InterNetwork,
1582 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1583 NetworkHelpers.FindFreePort ());
1588 BADCalledBack.Reset ();
1590 sock.BeginAccept (acc, 256, BADCallback, sock);
1592 Socket conn = new Socket (AddressFamily.InterNetwork,
1595 byte[] send_bytes = new byte[] {10, 11, 12, 13};
1598 conn.Send (send_bytes);
1600 if (BADCalledBack.WaitOne (2000, false) == false) {
1601 Assert.Fail ("BeginAcceptSocket wait timed out");
1604 Assert.AreEqual (true, BADAccepted, "BeginAcceptSocket #1");
1605 Assert.AreEqual (true, BADSocket.Connected, "BeginAcceptSocket #2");
1606 Assert.AreEqual (false, sock.Connected, "BeginAcceptSocket #3");
1607 Assert.AreEqual (true, conn.Connected, "BeginAcceptSocket #4");
1608 Assert.AreEqual (send_bytes.Length, BADByteCount, "BeginAcceptSocket #5");
1609 Assert.AreEqual (AddressFamily.InterNetwork, acc.AddressFamily, "BeginAcceptSocket #6");
1610 Assert.AreEqual (SocketType.Stream, acc.SocketType, "BeginAcceptSocket #7");
1611 Assert.AreEqual (ProtocolType.Tcp, acc.ProtocolType, "BeginAcceptSocket #8");
1612 Assert.AreEqual (conn.LocalEndPoint, acc.RemoteEndPoint, "BeginAcceptSocket #9");
1614 /* The MS runtime gives the returned data in a
1615 * much bigger array. TODO: investigate
1616 * whether it the size correlates to the first
1617 * parameter in BeginAccept()
1619 Assert.IsFalse (BADBytes.Length == send_bytes.Length,
1620 "BeginAcceptSocket #10");
1622 for(int i = 0; i < send_bytes.Length; i++) {
1623 Assert.AreEqual (send_bytes[i], BADBytes[i], "BeginAcceptSocket #" + (i+11).ToString ());
1633 public void BeginAcceptSocketClosed ()
1635 Socket sock = new Socket (AddressFamily.InterNetwork,
1638 Socket acc = new Socket (AddressFamily.InterNetwork,
1645 sock.BeginAccept (acc, 256, BADCallback, null);
1646 Assert.Fail ("BeginAcceptSocketClosed #1");
1647 } catch (ObjectDisposedException) {
1654 #if FEATURE_NO_BSD_SOCKETS
1655 [ExpectedException (typeof (PlatformNotSupportedException))]
1657 public void BeginAcceptSocketAccClosed ()
1659 Socket sock = new Socket (AddressFamily.InterNetwork,
1662 Socket acc = new Socket (AddressFamily.InterNetwork,
1665 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1666 NetworkHelpers.FindFreePort ());
1673 BADCalledBack.Reset ();
1676 sock.BeginAccept (acc, 256, BADCallback, null);
1677 Assert.Fail ("BeginAcceptSocketAccClosed #1");
1678 } catch (ObjectDisposedException) {
1684 static bool BCConnected = false;
1685 static ManualResetEvent BCCalledBack = new ManualResetEvent (false);
1687 private static void BCCallback (IAsyncResult asyncResult)
1689 Socket sock = (Socket)asyncResult.AsyncState;
1692 sock.EndConnect (asyncResult);
1693 } catch (Exception e) {
1694 Console.WriteLine ("BCCallback exception:");
1695 Console.WriteLine (e);
1702 BCCalledBack.Set ();
1706 #if FEATURE_NO_BSD_SOCKETS
1707 [ExpectedException (typeof (PlatformNotSupportedException))]
1709 public void BeginConnectAddressPort ()
1711 Socket sock = new Socket (AddressFamily.InterNetwork,
1714 Socket listen = new Socket (AddressFamily.InterNetwork,
1717 IPAddress ip = IPAddress.Loopback;
1718 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
1723 BCCalledBack.Reset ();
1725 BCConnected = false;
1727 sock.BeginConnect (ip, ep.Port, BCCallback, sock);
1729 if (BCCalledBack.WaitOne (2000, false) == false) {
1730 Assert.Fail ("BeginConnectAddressPort wait timed out");
1733 Assert.AreEqual (true, BCConnected, "BeginConnectAddressPort #1");
1740 public void BeginConnectAddressPortNull ()
1742 Socket sock = new Socket (AddressFamily.InterNetwork,
1745 IPAddress ip = null;
1748 sock.BeginConnect (ip, 1244, BCCallback,
1750 Assert.Fail ("BeginConnectAddressPortNull #1");
1751 } catch (ArgumentNullException) {
1758 #if FEATURE_NO_BSD_SOCKETS
1759 [ExpectedException (typeof (PlatformNotSupportedException))]
1761 public void BeginConnectAddressPortListen ()
1763 Socket sock = new Socket (AddressFamily.InterNetwork,
1766 IPAddress ip = IPAddress.Loopback;
1767 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
1773 sock.BeginConnect (ip, ep.Port, BCCallback, sock);
1774 Assert.Fail ("BeginConnectAddressPortListen #1");
1775 } catch (InvalidOperationException) {
1782 [ExpectedException (typeof(ObjectDisposedException))]
1783 public void BeginConnectAddressPortClosed ()
1785 Socket sock = new Socket (AddressFamily.InterNetwork,
1788 IPAddress ip = IPAddress.Loopback;
1792 sock.BeginConnect (ip, 1244, BCCallback, sock);
1796 [Category ("NotOnMac")]
1798 * This is not a Mono bug.
1800 * By default, only 127.0.0.1 is enabled and you must explicitly
1801 * enable additional addresses using 'sudo ifconfig lo0 alias 127.0.0.1'.
1803 * I tested this on Mac OS 10.7.4; a 'ping 127.0.0.2' does not work
1804 * until I add that alias.
1807 public void BeginConnectMultiple ()
1809 Socket sock = new Socket (AddressFamily.InterNetwork,
1812 Socket listen = new Socket (AddressFamily.InterNetwork,
1815 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1816 NetworkHelpers.FindFreePort ());
1817 IPAddress[] ips = new IPAddress[4];
1819 ips[0] = IPAddress.Parse ("127.0.0.4");
1820 ips[1] = IPAddress.Parse ("127.0.0.3");
1821 ips[2] = IPAddress.Parse ("127.0.0.2");
1822 ips[3] = IPAddress.Parse ("127.0.0.1");
1827 BCCalledBack.Reset ();
1829 BCConnected = false;
1831 sock.BeginConnect (ips, ep.Port, BCCallback, sock);
1833 /* Longer wait here, because the ms runtime
1834 * takes a lot longer to not connect
1837 if (BCCalledBack.WaitOne (30000, false) == false) {
1838 Assert.Fail ("BeginConnectMultiple wait failed");
1842 Assert.IsTrue (BCCalledBack.WaitOne (30000), "#0");
1844 Assert.AreEqual (true, BCConnected, "BeginConnectMultiple #1");
1845 Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "BeginConnectMultiple #2");
1846 IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
1848 Assert.AreEqual (IPAddress.Loopback, remep.Address, "BeginConnectMultiple #2");
1855 #if FEATURE_NO_BSD_SOCKETS
1856 [ExpectedException (typeof (PlatformNotSupportedException))]
1858 public void BeginConnectMultiple2 ()
1860 Socket sock = new Socket (AddressFamily.InterNetwork,
1863 Socket listen = new Socket (AddressFamily.InterNetwork,
1867 // Need at least two addresses.
1868 var ips = Dns.GetHostAddresses (string.Empty);
1870 Assert.Ignore ("This test needs at least two IP addresses.");
1872 var allIps = new IPAddress [ips.Length + 1];
1873 allIps [0] = IPAddress.Loopback;
1874 ips.CopyTo (allIps, 1);
1877 * Only bind to the loopback interface, so all the non-loopback
1878 * IP addresses will fail. BeginConnect()/EndConnect() should
1879 * succeed it it can connect to at least one of the requested
1882 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
1887 BCCalledBack.Reset ();
1889 BCConnected = false;
1891 sock.BeginConnect (allIps, ep.Port, BCCallback, sock);
1893 /* Longer wait here, because the ms runtime
1894 * takes a lot longer to not connect
1896 if (BCCalledBack.WaitOne (10000, false) == false) {
1897 Assert.Fail ("BeginConnectMultiple2 wait failed");
1900 Assert.AreEqual (true, BCConnected, "BeginConnectMultiple2 #1");
1901 Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "BeginConnectMultiple2 #2");
1902 IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
1904 Assert.AreEqual (IPAddress.Loopback, remep.Address, "BeginConnectMultiple2 #2");
1912 public void BeginConnectMultipleNull ()
1914 Socket sock = new Socket (AddressFamily.InterNetwork,
1917 IPAddress[] ips = null;
1920 sock.BeginConnect (ips, 1246, BCCallback,
1922 Assert.Fail ("BeginConnectMultipleNull #1");
1923 } catch (ArgumentNullException) {
1930 #if FEATURE_NO_BSD_SOCKETS
1931 [ExpectedException (typeof (PlatformNotSupportedException))]
1933 public void BeginConnectMultipleListen ()
1935 Socket sock = new Socket (AddressFamily.InterNetwork,
1938 IPAddress[] ips = new IPAddress[4];
1939 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1940 NetworkHelpers.FindFreePort ());
1942 ips[0] = IPAddress.Parse ("127.0.0.4");
1943 ips[1] = IPAddress.Parse ("127.0.0.3");
1944 ips[2] = IPAddress.Parse ("127.0.0.2");
1945 ips[3] = IPAddress.Parse ("127.0.0.1");
1951 sock.BeginConnect (ips, ep.Port, BCCallback,
1953 Assert.Fail ("BeginConnectMultipleListen #1");
1954 } catch (InvalidOperationException) {
1961 [ExpectedException (typeof(ObjectDisposedException))]
1962 public void BeginConnectMultipleClosed ()
1964 Socket sock = new Socket (AddressFamily.InterNetwork,
1967 IPAddress[] ips = new IPAddress[4];
1969 ips[0] = IPAddress.Parse ("127.0.0.4");
1970 ips[1] = IPAddress.Parse ("127.0.0.3");
1971 ips[2] = IPAddress.Parse ("127.0.0.2");
1972 ips[3] = IPAddress.Parse ("127.0.0.1");
1976 sock.BeginConnect (ips, 1247, BCCallback, sock);
1980 public void BeginConnectHostPortNull ()
1982 Socket sock = new Socket (AddressFamily.InterNetwork,
1987 sock.BeginConnect ((string)null, 0,
1989 Assert.Fail ("BeginConnectHostPort #1");
1990 } catch (ArgumentNullException) {
1997 #if FEATURE_NO_BSD_SOCKETS
1998 [ExpectedException (typeof (PlatformNotSupportedException))]
2000 public void BeginConnectHostPortListen ()
2002 Socket sock = new Socket (AddressFamily.InterNetwork,
2005 IPAddress ip = IPAddress.Loopback;
2006 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
2012 sock.BeginConnect ("localhost", ep.Port,
2014 Assert.Fail ("BeginConnectHostPortListen #1");
2015 } catch (InvalidOperationException) {
2022 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
2023 public void BeginConnectHostPortNotIP ()
2025 Socket sock = new Socket (AddressFamily.NetBios,
2026 SocketType.Seqpacket,
2027 ProtocolType.Unspecified);
2030 sock.BeginConnect ("localhost", 0, BCCallback,
2032 Assert.Fail ("BeginConnectHostPortNotIP #1");
2033 } catch (NotSupportedException) {
2040 [ExpectedException (typeof(ObjectDisposedException))]
2041 public void BeginConnectHostPortClosed ()
2043 Socket sock = new Socket (AddressFamily.InterNetwork,
2049 sock.BeginConnect ("localhost", 0, BCCallback, sock);
2052 static bool BDDisconnected = false;
2053 static ManualResetEvent BDCalledBack = new ManualResetEvent (false);
2055 private static void BDCallback (IAsyncResult asyncResult)
2057 Socket sock = (Socket)asyncResult.AsyncState;
2059 sock.EndDisconnect (asyncResult);
2060 BDDisconnected = true;
2062 BDCalledBack.Set ();
2066 [Category ("NotDotNet")] // "Needs XP or later"
2067 #if FEATURE_NO_BSD_SOCKETS
2068 [ExpectedException (typeof (PlatformNotSupportedException))]
2070 public void BeginDisconnect ()
2072 Socket sock = new Socket (AddressFamily.InterNetwork,
2075 Socket listen = new Socket (AddressFamily.InterNetwork,
2078 IPAddress ip = IPAddress.Loopback;
2079 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
2084 sock.Connect (ip, ep.Port);
2086 Assert.AreEqual (true, sock.Connected, "BeginDisconnect #1");
2088 sock.Shutdown (SocketShutdown.Both);
2090 BDCalledBack.Reset ();
2091 BDDisconnected = false;
2093 sock.BeginDisconnect (false, BDCallback, sock);
2095 if (BDCalledBack.WaitOne (2000, false) == false) {
2096 Assert.Fail ("BeginDisconnect wait timed out");
2099 Assert.AreEqual (true, BDDisconnected, "BeginDisconnect #2");
2100 Assert.AreEqual (false, sock.Connected, "BeginDisconnect #3");
2107 public void BeginReceiveSocketError ()
2112 public void BeginReceiveGeneric ()
2117 public void BeginReceiveGenericSocketError ()
2121 private static void BSCallback (IAsyncResult asyncResult)
2123 Socket sock = (Socket)asyncResult.AsyncState;
2125 sock.EndSend (asyncResult);
2129 public void BeginSendNotConnected ()
2131 Socket sock = new Socket (AddressFamily.InterNetwork,
2135 byte[] send_bytes = new byte[] {10, 11, 12, 13};
2138 sock.BeginSend (send_bytes, 0,
2140 SocketFlags.None, BSCallback,
2142 Assert.Fail ("BeginSendNotConnected #1");
2143 } catch (SocketException ex) {
2144 Assert.AreEqual (10057, ex.ErrorCode, "BeginSendNotConnected #2");
2151 public void BeginSendSocketError ()
2156 public void BeginSendGeneric ()
2161 public void BeginSendGenericSocketError ()
2166 #if FEATURE_NO_BSD_SOCKETS
2167 [ExpectedException (typeof (PlatformNotSupportedException))]
2169 public void BindTwice ()
2171 Socket sock = new Socket (AddressFamily.InterNetwork,
2174 IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
2175 NetworkHelpers.FindFreePort ());
2176 IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
2177 NetworkHelpers.FindFreePort ());
2183 Assert.Fail ("BindTwice #1");
2184 } catch (SocketException ex) {
2185 Assert.AreEqual (10022, ex.ErrorCode, "BindTwice #2");
2192 #if FEATURE_NO_BSD_SOCKETS
2193 [ExpectedException (typeof (PlatformNotSupportedException))]
2195 public void Close ()
2197 Socket sock = new Socket (AddressFamily.InterNetwork,
2200 Socket listen = new Socket (AddressFamily.InterNetwork,
2203 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2204 NetworkHelpers.FindFreePort ());
2211 Assert.AreEqual (true, sock.Connected, "Close #1");
2215 Thread.Sleep (3000);
2217 Assert.AreEqual (false, sock.Connected, "Close #2");
2223 #if FEATURE_NO_BSD_SOCKETS
2224 [ExpectedException (typeof (PlatformNotSupportedException))]
2226 public void ConnectAddressPort ()
2228 Socket sock = new Socket (AddressFamily.InterNetwork,
2231 Socket listen = new Socket (AddressFamily.InterNetwork,
2234 IPAddress ip = IPAddress.Loopback;
2235 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
2240 sock.Connect (ip, ep.Port);
2242 Assert.AreEqual (true, sock.Connected, "ConnectAddressPort #1");
2249 public void ConnectAddressPortNull ()
2251 Socket sock = new Socket (AddressFamily.InterNetwork,
2254 IPAddress ip = null;
2257 sock.Connect (ip, 1249);
2258 Assert.Fail ("ConnectAddressPortNull #1");
2259 } catch (ArgumentNullException) {
2266 #if FEATURE_NO_BSD_SOCKETS
2267 [ExpectedException (typeof (PlatformNotSupportedException))]
2269 public void ConnectAddressPortListen ()
2271 Socket sock = new Socket (AddressFamily.InterNetwork,
2274 IPAddress ip = IPAddress.Loopback;
2275 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
2281 sock.Connect (ip, ep.Port);
2282 Assert.Fail ("ConnectAddressPortListen #1");
2283 } catch (InvalidOperationException) {
2290 [ExpectedException (typeof(ObjectDisposedException))]
2291 public void ConnectAddressPortClosed ()
2293 Socket sock = new Socket (AddressFamily.InterNetwork,
2296 IPAddress ip = IPAddress.Loopback;
2300 sock.Connect (ip, 1250);
2304 [Category ("NotOnMac")] // MacOSX trashes the fd after the failed connect attempt to 127.0.0.4
2306 * This is not a Mono bug.
2308 * By default, only 127.0.0.1 is enabled and you must explicitly
2309 * enable additional addresses using 'sudo ifconfig lo0 alias 127.0.0.1'.
2311 * I tested this on Mac OS 10.7.4; a 'ping 127.0.0.2' does not work
2312 * until I add that alias.
2314 * However, after doing so, Mac OS treats these as separate addresses, ie. attempting
2315 * to connect to 127.0.0.4 yields a connection refused.
2317 * When using Connect(), the .NET runtime also throws an exception if connecting to
2318 * any of the IP addresses fails. This is different with BeginConnect()/EndConnect()
2319 * which succeeds when it can connect to at least one of the addresses.
2322 public void ConnectMultiple ()
2324 Socket sock = new Socket (AddressFamily.InterNetwork,
2327 Socket listen = new Socket (AddressFamily.InterNetwork,
2330 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2331 NetworkHelpers.FindFreePort ());
2332 IPAddress[] ips = new IPAddress[4];
2334 ips[0] = IPAddress.Parse ("127.0.0.4");
2335 ips[1] = IPAddress.Parse ("127.0.0.3");
2336 ips[2] = IPAddress.Parse ("127.0.0.2");
2337 ips[3] = IPAddress.Parse ("127.0.0.1");
2342 sock.Connect (ips, ep.Port);
2344 Assert.AreEqual (true, sock.Connected, "ConnectMultiple #1");
2345 Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "ConnectMultiple #2");
2346 IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
2348 Assert.AreEqual (IPAddress.Loopback, remep.Address, "ConnectMultiple #2");
2355 #if FEATURE_NO_BSD_SOCKETS
2356 [ExpectedException (typeof (PlatformNotSupportedException))]
2358 public void ConnectMultiple2 ()
2360 Socket sock = new Socket (AddressFamily.InterNetwork,
2363 Socket listen = new Socket (AddressFamily.InterNetwork,
2367 // Need at least two addresses.
2368 var ips = Dns.GetHostAddresses (string.Empty);
2370 Assert.Ignore ("This test needs at least two IP addresses.");
2372 var allIps = new IPAddress [ips.Length + 1];
2373 allIps [0] = IPAddress.Loopback;
2374 ips.CopyTo (allIps, 1);
2377 * Bind to IPAddress.Any; Connect() will fail unless it can
2378 * connect to all the addresses in allIps.
2380 IPEndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
2385 sock.Connect (allIps, ep.Port);
2387 Assert.AreEqual (true, sock.Connected, "ConnectMultiple2 #1");
2388 Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "ConnectMultiple2 #2");
2389 IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
2391 Assert.AreEqual (IPAddress.Loopback, remep.Address, "ConnectMultiple2 #3");
2398 public void ConnectMultipleNull ()
2400 Socket sock = new Socket (AddressFamily.InterNetwork,
2403 IPAddress[] ips = null;
2406 sock.Connect (ips, 1251);
2407 Assert.Fail ("ConnectMultipleNull #1");
2408 } catch (ArgumentNullException) {
2415 #if FEATURE_NO_BSD_SOCKETS
2416 [ExpectedException (typeof (PlatformNotSupportedException))]
2418 public void ConnectMultipleListen ()
2420 Socket sock = new Socket (AddressFamily.InterNetwork,
2423 IPAddress[] ips = new IPAddress[4];
2424 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2425 NetworkHelpers.FindFreePort ());
2427 ips[0] = IPAddress.Parse ("127.0.0.4");
2428 ips[1] = IPAddress.Parse ("127.0.0.3");
2429 ips[2] = IPAddress.Parse ("127.0.0.2");
2430 ips[3] = IPAddress.Parse ("127.0.0.1");
2436 sock.Connect (ips, ep.Port);
2437 Assert.Fail ("ConnectMultipleListen #1");
2438 } catch (InvalidOperationException) {
2445 [ExpectedException (typeof(ObjectDisposedException))]
2446 public void ConnectMultipleClosed ()
2448 Socket sock = new Socket (AddressFamily.InterNetwork,
2451 IPAddress[] ips = new IPAddress[4];
2453 ips[0] = IPAddress.Parse ("127.0.0.4");
2454 ips[1] = IPAddress.Parse ("127.0.0.3");
2455 ips[2] = IPAddress.Parse ("127.0.0.2");
2456 ips[3] = IPAddress.Parse ("127.0.0.1");
2460 sock.Connect (ips, 1252);
2464 public void ConnectHostPortNull ()
2466 Socket sock = new Socket (AddressFamily.InterNetwork,
2471 sock.Connect ((string)null, 0);
2472 Assert.Fail ("ConnectHostPort #1");
2473 } catch (ArgumentNullException) {
2480 #if FEATURE_NO_BSD_SOCKETS
2481 [ExpectedException (typeof (PlatformNotSupportedException))]
2483 public void ConnectHostPortListen ()
2485 Socket sock = new Socket (AddressFamily.InterNetwork,
2488 IPAddress ip = IPAddress.Loopback;
2489 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
2495 sock.Connect ("localhost", ep.Port);
2496 Assert.Fail ("ConnectHostPortListen #1");
2497 } catch (InvalidOperationException) {
2504 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
2505 public void ConnectHostPortNotIP ()
2507 Socket sock = new Socket (AddressFamily.NetBios,
2508 SocketType.Seqpacket,
2509 ProtocolType.Unspecified);
2512 sock.Connect ("localhost", 0);
2513 Assert.Fail ("ConnectHostPortNotIP #1");
2514 } catch (NotSupportedException) {
2521 #if FEATURE_NO_BSD_SOCKETS
2522 [ExpectedException (typeof (PlatformNotSupportedException))]
2524 [ExpectedException (typeof(ObjectDisposedException))]
2526 public void ConnectHostPortClosed ()
2528 Socket sock = new Socket (AddressFamily.InterNetwork,
2534 sock.Connect ("localhost", 0);
2538 [Category ("NotDotNet")] // "Needs XP or later"
2539 #if FEATURE_NO_BSD_SOCKETS
2540 [ExpectedException (typeof (PlatformNotSupportedException))]
2542 public void Disconnect ()
2544 Socket sock = new Socket (AddressFamily.InterNetwork,
2547 Socket listen = new Socket (AddressFamily.InterNetwork,
2550 IPAddress ip = IPAddress.Loopback;
2551 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
2556 sock.Connect (ip, ep.Port);
2558 Assert.AreEqual (true, sock.Connected, "Disconnect #1");
2560 sock.Shutdown (SocketShutdown.Both);
2562 sock.Disconnect (false);
2564 Assert.AreEqual (false, sock.Connected, "BeginDisconnect #3");
2571 public void DuplicateAndClose ()
2576 public void IOControl ()
2581 public void TestDefaultsDualMode ()
2583 using (var socket = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)){
2584 Assert.IsTrue (socket.DualMode, "In Mono, DualMode must be true when constructing InterNetworkV6 sockets");
2587 using (var socket = new Socket (SocketType.Stream, ProtocolType.Tcp)){
2588 Assert.AreEqual (AddressFamily.InterNetworkV6, socket.AddressFamily, "When creating sockets of type stream/tcp, the address family should be InterNetworkV6");
2589 Assert.IsTrue (socket.DualMode, "In Mono, DualMode must be true when constructing InterNetworkV6 sockets");
2591 socket.DualMode = false;
2593 Assert.IsFalse (socket.DualMode, "Setting of DualSocket should turn DualSockets off");
2599 #if FEATURE_NO_BSD_SOCKETS
2600 [ExpectedException (typeof (PlatformNotSupportedException))]
2602 public void ReceiveGeneric ()
2606 IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, NetworkHelpers.FindFreePort ());
2608 Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2609 listensock.Bind (endpoint);
2610 listensock.Listen(1);
2612 Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2613 sendsock.Connect(endpoint);
2615 Socket clientsock = listensock.Accept();
2617 byte[] sendbuf = new byte[256];
2619 for(i = 0; i < 256; i++) {
2620 sendbuf[i] = (byte)i;
2622 for (i = 4; i < 6; i++) {
2623 Assert.AreEqual (sendbuf[i], (byte)i,
2624 "#1/" + i.ToString());
2628 sendsock.Send (sendbuf, 0, 256, SocketFlags.None,
2632 byte[] recvbuf = new byte[256];
2633 List<ArraySegment<byte>> recvbuflist = new List<ArraySegment<byte>>(2);
2634 recvbuflist.Add(new ArraySegment<byte>(recvbuf, 4, 2));
2635 recvbuflist.Add(new ArraySegment<byte>(recvbuf, 20, 230));
2637 clientsock.Receive (recvbuflist);
2639 /* recvbuf should now hold the first 2 bytes
2640 * of sendbuf from pos 4, and the next 230
2641 * bytes of sendbuf from pos 20
2644 for (i = 0; i < 2; i++) {
2645 Assert.AreEqual (sendbuf[i], recvbuf[i + 4],
2646 "#2/" + i.ToString());
2648 for (i = 2; i < 232; i++) {
2649 Assert.AreEqual (sendbuf[i], recvbuf[i + 18],
2650 "#2/" + i.ToString());
2654 clientsock.Close ();
2655 listensock.Close ();
2659 #if FEATURE_NO_BSD_SOCKETS
2660 [ExpectedException (typeof (PlatformNotSupportedException))]
2662 public void SendGeneric ()
2666 IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, NetworkHelpers.FindFreePort ());
2668 Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2669 listensock.Bind (endpoint);
2670 listensock.Listen(1);
2672 Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2673 sendsock.Connect(endpoint);
2675 Socket clientsock = listensock.Accept();
2677 byte[] sendbuf = new byte[256];
2678 List<ArraySegment<byte>> sendbuflist = new List<ArraySegment<byte>>(2);
2680 sendbuflist.Add(new ArraySegment<byte>(sendbuf, 4, 2));
2681 sendbuflist.Add(new ArraySegment<byte>(sendbuf, 20, 230));
2683 for(i = 0; i < 256; i++) {
2684 sendbuf[i] = (byte)i;
2686 for (i = 4; i < 6; i++) {
2687 Assert.AreEqual (sendbuf[i], (byte)i,
2688 "#1/" + i.ToString());
2692 sendsock.Send (sendbuflist, SocketFlags.None, out err);
2695 byte[] recvbuf = new byte[256];
2697 clientsock.Receive (recvbuf);
2699 /* The first 2 bytes of recvbuf should now
2700 * hold 2 bytes of sendbuf from pos 4, and the
2701 * next 230 bytes of recvbuf should be sendbuf
2705 for (i = 0; i < 2; i++) {
2706 Assert.AreEqual (recvbuf[i], sendbuf[i + 4],
2707 "#2/" + i.ToString());
2709 for (i = 2; i < 232; i++) {
2710 Assert.AreEqual (recvbuf[i], sendbuf[i + 18],
2711 "#2/" + i.ToString());
2715 clientsock.Close ();
2716 listensock.Close ();
2720 public void ConcurrentExceedSocketLimit ()
2722 var tasks = new Task[4];
2723 for (int i = 0; i < 4; i++) {
2724 tasks[i] = Task.Factory.StartNew (() => SendGenericExceedBuffer ());
2726 Task.WaitAll (tasks);
2730 public void SendGenericExceedBuffer ()
2732 // Create a buffer larger than the default max.
2733 const int BUFFER_SIZE = 65 * 1024;
2736 IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, NetworkHelpers.FindFreePort ());
2738 Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2739 listensock.Bind (endpoint);
2740 listensock.Listen (1);
2742 Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2743 sendsock.Connect (endpoint);
2745 Socket clientsock = listensock.Accept ();
2747 byte[] sendbuf = new byte[BUFFER_SIZE];
2749 for (i = 0; i < BUFFER_SIZE; i++) {
2750 sendbuf[i] = (byte)i;
2753 Task sendTask = Task.Factory.StartNew(() => {
2754 int sent = sendsock.Send (sendbuf);
2756 Assert.AreEqual (BUFFER_SIZE, sent, "#1");
2759 byte[] recvbuf = new byte[BUFFER_SIZE];
2761 Task recvTask = Task.Factory.StartNew(() => {
2762 int totalReceived = 0;
2763 byte[] buffer = new byte[256];
2764 while (totalReceived < sendbuf.Length) {
2765 int recvd = clientsock.Receive (buffer, 0, buffer.Length, SocketFlags.None);
2766 Array.Copy (buffer, 0, recvbuf, totalReceived, recvd);
2767 totalReceived += recvd;
2770 Assert.AreEqual (BUFFER_SIZE, totalReceived, "#2");
2773 Assert.IsTrue (Task.WaitAll (new []{sendTask, recvTask}, 20 * 1000), "#2a");
2775 for (i = 0; i < BUFFER_SIZE; i++) {
2776 Assert.AreEqual (recvbuf[i], sendbuf[i],
2777 "#3/" + i.ToString());
2781 clientsock.Close ();
2782 listensock.Close ();
2786 public void ListenNotBound ()
2788 Socket sock = new Socket (AddressFamily.InterNetwork,
2794 Assert.Fail ("ListenNotBound #1");
2795 } catch (SocketException ex) {
2796 Assert.AreEqual (10022, ex.ErrorCode, "ListenNotBound #2");
2802 static Socket CWRSocket;
2803 static bool CWRReceiving = true;
2804 static ManualResetEvent CWRReady = new ManualResetEvent (false);
2806 private static void CWRReceiveThread ()
2808 byte[] buf = new byte[256];
2811 CWRSocket.Receive (buf);
2812 } catch (SocketException) {
2813 CWRReceiving = false;
2820 #if FEATURE_NO_BSD_SOCKETS
2821 [ExpectedException (typeof (PlatformNotSupportedException))]
2823 public void CloseWhileReceiving ()
2825 CWRSocket = new Socket (AddressFamily.InterNetwork,
2828 CWRSocket.Bind (new IPEndPoint (IPAddress.Loopback,
2829 NetworkHelpers.FindFreePort ()));
2831 Thread recv_thread = new Thread (new ThreadStart (CWRReceiveThread));
2833 recv_thread.Start ();
2834 Thread.Sleep (250); /* Wait for the thread to be already receiving */
2837 if (CWRReady.WaitOne (1000, false) == false) {
2838 Assert.Fail ("CloseWhileReceiving wait timed out");
2841 Assert.IsFalse (CWRReceiving);
2844 static bool RRCLastRead = false;
2845 static ManualResetEvent RRCReady = new ManualResetEvent (false);
2847 private static void RRCClientThread (int port)
2849 byte[] bytes = new byte[8];
2852 Socket sock = new Socket (AddressFamily.InterNetwork,
2855 sock.Connect (new IPEndPoint (IPAddress.Loopback,
2858 NetworkStream stream = new NetworkStream (sock);
2860 readbyte = stream.ReadByte ();
2861 Assert.AreEqual (0, readbyte, "ReceiveRemoteClosed #1");
2863 stream.Read (bytes, 0, 0);
2865 readbyte = stream.ReadByte ();
2866 Assert.AreEqual (0, readbyte, "ReceiveRemoteClosed #2");
2868 stream.Read (bytes, 0, 0);
2870 readbyte = stream.ReadByte ();
2871 Assert.AreEqual (-1, readbyte, "ReceiveRemoteClosed #3");
2879 [Test] // Receive (Byte [])
2880 public void Receive1_Buffer_Null ()
2882 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2886 s.Receive ((byte []) null);
2888 } catch (ArgumentNullException ex) {
2889 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2890 Assert.IsNull (ex.InnerException, "#3");
2891 Assert.IsNotNull (ex.Message, "#4");
2892 Assert.AreEqual ("buffer", ex.ParamName, "#5");
2898 [Test] // Receive (Byte [])
2899 public void Receive1_Socket_Closed ()
2901 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2906 s.Receive ((byte []) null);
2908 } catch (ObjectDisposedException ex) {
2909 // Cannot access a disposed object
2910 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2911 Assert.IsNull (ex.InnerException, "#3");
2912 Assert.IsNotNull (ex.Message, "#4");
2913 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2917 [Test] // Receive (Byte [], SocketFlags)
2918 public void Receive2_Buffer_Null ()
2920 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2924 s.Receive ((byte []) null, (SocketFlags) 666);
2926 } catch (ArgumentNullException ex) {
2927 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2928 Assert.IsNull (ex.InnerException, "#3");
2929 Assert.IsNotNull (ex.Message, "#4");
2930 Assert.AreEqual ("buffer", ex.ParamName, "#5");
2936 [Test] // Receive (Byte [], SocketFlags)
2937 public void Receive2_Socket_Closed ()
2939 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2944 s.Receive ((byte []) null, (SocketFlags) 666);
2946 } catch (ObjectDisposedException ex) {
2947 // Cannot access a disposed object
2948 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2949 Assert.IsNull (ex.InnerException, "#3");
2950 Assert.IsNotNull (ex.Message, "#4");
2951 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2955 [Test] // Receive (Byte [], Int32, SocketFlags)
2956 public void Receive3_Buffer_Null ()
2958 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2962 s.Receive ((byte []) null, 0, (SocketFlags) 666);
2964 } catch (ArgumentNullException ex) {
2965 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2966 Assert.IsNull (ex.InnerException, "#3");
2967 Assert.IsNotNull (ex.Message, "#4");
2968 Assert.AreEqual ("buffer", ex.ParamName, "#5");
2974 [Test] // Receive (Byte [], Int32, SocketFlags)
2975 public void Receive3_Socket_Closed ()
2977 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2982 s.Receive ((byte []) null, 0, (SocketFlags) 666);
2984 } catch (ObjectDisposedException ex) {
2985 // Cannot access a disposed object
2986 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2987 Assert.IsNull (ex.InnerException, "#3");
2988 Assert.IsNotNull (ex.Message, "#4");
2989 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2993 [Test] // Receive (Byte [], Int32, Int32, SocketFlags)
2994 public void Receive4_Buffer_Null ()
2996 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3000 s.Receive ((byte []) null, 0, 0, (SocketFlags) 666);
3002 } catch (ArgumentNullException ex) {
3003 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3004 Assert.IsNull (ex.InnerException, "#3");
3005 Assert.IsNotNull (ex.Message, "#4");
3006 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3012 [Test] // Receive (Byte [], Int32, Int32, SocketFlags)
3013 public void Receive4_Socket_Closed ()
3015 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3020 s.Receive ((byte []) null, 0, 0, (SocketFlags) 666);
3022 } catch (ObjectDisposedException ex) {
3023 // Cannot access a disposed object
3024 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3025 Assert.IsNull (ex.InnerException, "#3");
3026 Assert.IsNotNull (ex.Message, "#4");
3027 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3031 [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError)
3032 public void Receive5_Buffer_Null ()
3034 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3039 s.Receive ((byte []) null, 0, 0, SocketFlags.None, out error);
3041 } catch (ArgumentNullException ex) {
3042 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3043 Assert.IsNull (ex.InnerException, "#3");
3044 Assert.IsNotNull (ex.Message, "#4");
3045 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3051 [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError)
3052 public void Receive5_Socket_Closed ()
3054 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3060 s.Receive ((byte []) null, 0, 0, SocketFlags.None, out error);
3062 } catch (ObjectDisposedException ex) {
3063 // Cannot access a disposed object
3064 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3065 Assert.IsNull (ex.InnerException, "#3");
3066 Assert.IsNotNull (ex.Message, "#4");
3067 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3071 [Test] // Receive (IList<ArraySegment<Byte>>)
3072 public void Receive6_Buffers_Null ()
3074 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3078 s.Receive ((IList<ArraySegment<byte>>) null);
3080 } catch (ArgumentNullException ex) {
3081 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3082 Assert.IsNull (ex.InnerException, "#3");
3083 Assert.IsNotNull (ex.Message, "#4");
3084 Assert.AreEqual ("buffers", ex.ParamName, "#5");
3090 [Test] // Receive (IList<ArraySegment<Byte>>)
3091 public void Receive6_Socket_Closed ()
3093 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3098 s.Receive ((IList<ArraySegment<byte>>) null);
3100 } catch (ObjectDisposedException ex) {
3101 // Cannot access a disposed object
3102 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3103 Assert.IsNull (ex.InnerException, "#3");
3104 Assert.IsNotNull (ex.Message, "#4");
3105 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3109 [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags)
3110 public void Receive7_Buffers_Null ()
3112 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3116 s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666);
3118 } catch (ArgumentNullException ex) {
3119 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3120 Assert.IsNull (ex.InnerException, "#3");
3121 Assert.IsNotNull (ex.Message, "#4");
3122 Assert.AreEqual ("buffers", ex.ParamName, "#5");
3128 [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags)
3129 public void Receive7_Socket_Closed ()
3131 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3136 s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666);
3138 } catch (ObjectDisposedException ex) {
3139 // Cannot access a disposed object
3140 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3141 Assert.IsNull (ex.InnerException, "#3");
3142 Assert.IsNotNull (ex.Message, "#4");
3143 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3147 [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags, out SocketError)
3148 public void Receive8_Buffers_Null ()
3150 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3155 s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666,
3158 } catch (ArgumentNullException ex) {
3159 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3160 Assert.IsNull (ex.InnerException, "#3");
3161 Assert.IsNotNull (ex.Message, "#4");
3162 Assert.AreEqual ("buffers", ex.ParamName, "#5");
3168 [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags, out SocketError)
3169 public void Receive8_Socket_Closed ()
3171 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3177 s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666,
3180 } catch (ObjectDisposedException ex) {
3181 // Cannot access a disposed object
3182 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3183 Assert.IsNull (ex.InnerException, "#3");
3184 Assert.IsNotNull (ex.Message, "#4");
3185 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3191 [Test] // ReceiveFrom (Byte [], ref EndPoint)
3192 #if FEATURE_NO_BSD_SOCKETS
3193 [ExpectedException (typeof (PlatformNotSupportedException))]
3195 public void ReceiveFrom1_Buffer_Null ()
3197 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3200 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3202 s.ReceiveFrom ((Byte []) null, ref remoteEP);
3204 } catch (ArgumentNullException ex) {
3205 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3206 Assert.IsNull (ex.InnerException, "#3");
3207 Assert.IsNotNull (ex.Message, "#4");
3208 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3214 [Test] // ReceiveFrom (Byte [], ref EndPoint)
3215 public void ReceiveFrom1_RemoteEP_Null ()
3217 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3220 byte [] buffer = new byte [0];
3221 EndPoint remoteEP = null;
3223 s.ReceiveFrom (buffer, ref remoteEP);
3225 } catch (ArgumentNullException ex) {
3226 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3227 Assert.IsNull (ex.InnerException, "#3");
3228 Assert.IsNotNull (ex.Message, "#4");
3229 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
3235 [Test] // ReceiveFrom (Byte [], ref EndPoint)
3236 #if FEATURE_NO_BSD_SOCKETS
3237 [ExpectedException (typeof (PlatformNotSupportedException))]
3239 public void ReceiveFrom1_Socket_Closed ()
3241 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3245 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3247 s.ReceiveFrom ((Byte []) null, ref remoteEP);
3249 } catch (ObjectDisposedException ex) {
3250 // Cannot access a disposed object
3251 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3252 Assert.IsNull (ex.InnerException, "#3");
3253 Assert.IsNotNull (ex.Message, "#4");
3254 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3258 [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
3259 #if FEATURE_NO_BSD_SOCKETS
3260 [ExpectedException (typeof (PlatformNotSupportedException))]
3262 public void ReceiveFrom2_Buffer_Null ()
3264 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3267 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3269 s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP);
3271 } catch (ArgumentNullException ex) {
3272 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3273 Assert.IsNull (ex.InnerException, "#3");
3274 Assert.IsNotNull (ex.Message, "#4");
3275 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3281 [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
3282 public void ReceiveFrom2_RemoteEP_Null ()
3284 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3287 byte [] buffer = new byte [5];
3288 EndPoint remoteEP = null;
3290 s.ReceiveFrom (buffer, (SocketFlags) 666, ref remoteEP);
3292 } catch (ArgumentNullException ex) {
3293 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3294 Assert.IsNull (ex.InnerException, "#3");
3295 Assert.IsNotNull (ex.Message, "#4");
3296 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
3302 [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
3303 #if FEATURE_NO_BSD_SOCKETS
3304 [ExpectedException (typeof (PlatformNotSupportedException))]
3306 public void ReceiveFrom2_Socket_Closed ()
3308 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3312 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3314 s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP);
3316 } catch (ObjectDisposedException ex) {
3317 // Cannot access a disposed object
3318 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3319 Assert.IsNull (ex.InnerException, "#3");
3320 Assert.IsNotNull (ex.Message, "#4");
3321 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3325 [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
3326 #if FEATURE_NO_BSD_SOCKETS
3327 [ExpectedException (typeof (PlatformNotSupportedException))]
3329 public void ReceiveFrom3_Buffer_Null ()
3331 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3334 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3336 s.ReceiveFrom ((Byte []) null, 0, (SocketFlags) 666,
3339 } catch (ArgumentNullException ex) {
3340 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3341 Assert.IsNull (ex.InnerException, "#3");
3342 Assert.IsNotNull (ex.Message, "#4");
3343 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3349 [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
3350 public void ReceiveFrom3_RemoteEP_Null ()
3352 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3355 byte [] buffer = new byte [5];
3356 EndPoint remoteEP = null;
3358 s.ReceiveFrom (buffer, buffer.Length, (SocketFlags) 666, ref remoteEP);
3360 } catch (ArgumentNullException ex) {
3361 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3362 Assert.IsNull (ex.InnerException, "#3");
3363 Assert.IsNotNull (ex.Message, "#4");
3364 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
3370 [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
3371 #if FEATURE_NO_BSD_SOCKETS
3372 [ExpectedException (typeof (PlatformNotSupportedException))]
3374 public void ReceiveFrom3_Size_OutOfRange ()
3377 byte [] buffer = new byte [5];
3378 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3381 s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3384 s.ReceiveFrom (buffer, -1, (SocketFlags) 666, ref remoteEP);
3385 Assert.Fail ("#A1");
3386 } catch (ArgumentOutOfRangeException ex) {
3387 // Specified argument was out of the range of valid values
3388 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
3389 Assert.IsNull (ex.InnerException, "#A3");
3390 Assert.IsNotNull (ex.Message, "#A4");
3391 Assert.AreEqual ("size", ex.ParamName, "#A5");
3396 // size > buffer length
3397 s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3400 s.ReceiveFrom (buffer, (buffer.Length + 1), (SocketFlags) 666,
3402 Assert.Fail ("#B1");
3403 } catch (ArgumentOutOfRangeException ex) {
3404 // Specified argument was out of the range of valid values
3405 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
3406 Assert.IsNull (ex.InnerException, "#B3");
3407 Assert.IsNotNull (ex.Message, "#B4");
3408 Assert.AreEqual ("size", ex.ParamName, "#B5");
3414 [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
3415 #if FEATURE_NO_BSD_SOCKETS
3416 [ExpectedException (typeof (PlatformNotSupportedException))]
3418 public void ReceiveFrom3_Socket_Closed ()
3420 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3424 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3426 s.ReceiveFrom ((Byte []) null, -1, (SocketFlags) 666,
3429 } catch (ObjectDisposedException ex) {
3430 // Cannot access a disposed object
3431 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3432 Assert.IsNull (ex.InnerException, "#3");
3433 Assert.IsNotNull (ex.Message, "#4");
3434 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3438 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
3439 #if FEATURE_NO_BSD_SOCKETS
3440 [ExpectedException (typeof (PlatformNotSupportedException))]
3442 public void ReceiveFrom4_Buffer_Null ()
3444 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3446 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3449 s.ReceiveFrom ((Byte []) null, -1, -1, (SocketFlags) 666,
3452 } catch (ArgumentNullException ex) {
3453 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3454 Assert.IsNull (ex.InnerException, "#3");
3455 Assert.IsNotNull (ex.Message, "#4");
3456 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3460 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
3461 #if FEATURE_NO_BSD_SOCKETS
3462 [ExpectedException (typeof (PlatformNotSupportedException))]
3464 public void ReceiveFrom4_Offset_OutOfRange ()
3467 byte [] buffer = new byte [5];
3468 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3471 s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3474 s.ReceiveFrom (buffer, -1, 0, (SocketFlags) 666,
3476 Assert.Fail ("#A1");
3477 } catch (ArgumentOutOfRangeException ex) {
3478 // Specified argument was out of the range of valid values
3479 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
3480 Assert.IsNull (ex.InnerException, "#A3");
3481 Assert.IsNotNull (ex.Message, "#A4");
3482 Assert.AreEqual ("offset", ex.ParamName, "#A5");
3487 // offset > buffer length
3488 s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3491 s.ReceiveFrom (buffer, (buffer.Length + 1), 0, (SocketFlags) 666,
3493 Assert.Fail ("#B1");
3494 } catch (ArgumentOutOfRangeException ex) {
3495 // Specified argument was out of the range of valid values
3496 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
3497 Assert.IsNull (ex.InnerException, "#B3");
3498 Assert.IsNotNull (ex.Message, "#B4");
3499 Assert.AreEqual ("offset", ex.ParamName, "#B5");
3505 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref IPEndPoint)
3506 public void ReceiveFrom4_RemoteEP_Null ()
3508 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3510 byte [] buffer = new byte [5];
3511 EndPoint remoteEP = null;
3514 s.ReceiveFrom (buffer, 0, buffer.Length, (SocketFlags) 666, ref remoteEP);
3516 } catch (ArgumentNullException ex) {
3517 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3518 Assert.IsNull (ex.InnerException, "#3");
3519 Assert.IsNotNull (ex.Message, "#4");
3520 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
3526 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
3527 #if FEATURE_NO_BSD_SOCKETS
3528 [ExpectedException (typeof (PlatformNotSupportedException))]
3530 public void ReceiveFrom4_Size_OutOfRange ()
3533 byte [] buffer = new byte [5];
3534 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3537 s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3540 s.ReceiveFrom (buffer, 0, -1, (SocketFlags) 666,
3542 Assert.Fail ("#A1");
3543 } catch (ArgumentOutOfRangeException ex) {
3544 // Specified argument was out of the range of valid values
3545 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
3546 Assert.IsNull (ex.InnerException, "#A3");
3547 Assert.IsNotNull (ex.Message, "#A4");
3548 Assert.AreEqual ("size", ex.ParamName, "#A5");
3553 // size > buffer length
3554 s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3557 s.ReceiveFrom (buffer, 0, (buffer.Length + 1), (SocketFlags) 666,
3559 Assert.Fail ("#B1");
3560 } catch (ArgumentOutOfRangeException ex) {
3561 // Specified argument was out of the range of valid values
3562 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
3563 Assert.IsNull (ex.InnerException, "#B3");
3564 Assert.IsNotNull (ex.Message, "#B4");
3565 Assert.AreEqual ("size", ex.ParamName, "#B5");
3570 // offset + size > buffer length
3571 s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3574 s.ReceiveFrom (buffer, 2, 4, (SocketFlags) 666, ref remoteEP);
3575 Assert.Fail ("#C1");
3576 } catch (ArgumentOutOfRangeException ex) {
3577 // Specified argument was out of the range of valid values
3578 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
3579 Assert.IsNull (ex.InnerException, "#C3");
3580 Assert.IsNotNull (ex.Message, "#C4");
3581 Assert.AreEqual ("size", ex.ParamName, "#C5");
3587 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref EndPoint)
3588 #if FEATURE_NO_BSD_SOCKETS
3589 [ExpectedException (typeof (PlatformNotSupportedException))]
3591 public void ReceiveFrom4_Socket_Closed ()
3593 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3597 byte [] buffer = new byte [5];
3598 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3600 s.ReceiveFrom (buffer, -1, -1, (SocketFlags) 666,
3603 } catch (ObjectDisposedException ex) {
3604 // Cannot access a disposed object
3605 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3606 Assert.IsNull (ex.InnerException, "#3");
3607 Assert.IsNotNull (ex.Message, "#4");
3608 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3613 #if FEATURE_NO_BSD_SOCKETS
3614 [ExpectedException (typeof (PlatformNotSupportedException))]
3616 public void ReceiveRemoteClosed ()
3618 var port = NetworkHelpers.FindFreePort ();
3619 Socket sock = new Socket (AddressFamily.InterNetwork,
3622 sock.Bind (new IPEndPoint (IPAddress.Loopback, port));
3626 Thread client_thread = new Thread (() => RRCClientThread (port));
3627 client_thread.Start ();
3629 Socket client = sock.Accept ();
3630 NetworkStream stream = new NetworkStream (client);
3631 stream.WriteByte (0x00);
3632 stream.WriteByte (0x00);
3636 RRCReady.WaitOne (1000, false);
3637 Assert.IsTrue (RRCLastRead);
3641 // Test case for bug #471580
3643 #if FEATURE_NO_BSD_SOCKETS
3644 [ExpectedException (typeof (PlatformNotSupportedException))]
3646 public void UdpDoubleBind ()
3648 using (Socket s = new Socket (AddressFamily.InterNetwork,
3649 SocketType.Dgram, ProtocolType.Udp))
3650 using (Socket ss = new Socket (AddressFamily.InterNetwork,
3651 SocketType.Dgram, ProtocolType.Udp)) {
3652 var supportsReuseAddress = true;
3654 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
3655 } catch (SocketException e) {
3656 // Exception is thrown when ReuseAddress is not supported
3657 Assert.AreEqual ((int) SocketError.OperationNotSupported, e.NativeErrorCode,
3658 "Expected SocketError.OperationNotSupported");
3659 supportsReuseAddress = false;
3662 var ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
3665 if (supportsReuseAddress)
3666 ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
3669 ss.Bind (new IPEndPoint (IPAddress.Any, ep.Port));
3670 if (!supportsReuseAddress)
3671 Assert.Fail ("Reusing address is not supported, exception was expected on second bind.");
3672 } catch (SocketException e) {
3677 // Test case for bug #31557
3679 #if FEATURE_NO_BSD_SOCKETS
3680 [ExpectedException (typeof (PlatformNotSupportedException))]
3682 public void TcpDoubleBind ()
3684 using (Socket s = new Socket (AddressFamily.InterNetwork,
3685 SocketType.Stream, ProtocolType.Tcp))
3686 using (Socket ss = new Socket (AddressFamily.InterNetwork,
3687 SocketType.Stream, ProtocolType.Tcp)) {
3688 var supportsReuseAddress = true;
3690 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
3691 } catch (SocketException e) {
3692 // Exception is thrown when ReuseAddress is not supported
3693 Assert.AreEqual ((int) SocketError.OperationNotSupported, e.NativeErrorCode,
3694 "Expected SocketError.OperationNotSupported");
3695 supportsReuseAddress = false;
3698 var ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
3702 if (supportsReuseAddress)
3703 ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
3706 ss.Bind (new IPEndPoint (IPAddress.Any, ep.Port));
3708 if (!supportsReuseAddress)
3709 Assert.Fail ("Reusing address is not supported, exception was expected on second bind.");
3710 } catch (SocketException e) {
3716 [Category ("NotOnMac")]
3717 public void ConnectedProperty ()
3719 TcpListener listener = new TcpListener (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3722 Socket client = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3723 client.Connect (IPAddress.Loopback, ((IPEndPoint)listener.LocalEndpoint).Port);
3724 Socket server = listener.AcceptSocket ();
3727 server.EndSend(server.BeginSend (new byte[10], 0, 10, SocketFlags.None, null, null));
3730 server.EndReceive (server.BeginReceive (new byte[10], 0, 10, SocketFlags.None, null, null));
3733 Assert.IsTrue (!client.Connected);
3734 Assert.IsTrue (!server.Connected);
3742 [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName)
3743 public void GetSocketOption1_Socket_Closed ()
3745 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3748 s.GetSocketOption (0, 0);
3750 } catch (ObjectDisposedException ex) {
3751 // Cannot access a disposed object
3752 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3753 Assert.IsNull (ex.InnerException, "#3");
3754 Assert.IsNotNull (ex.Message, "#4");
3755 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3759 [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3760 public void GetSocketOption2_OptionValue_Null ()
3762 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3764 s.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
3767 } catch (SocketException ex) {
3768 // The system detected an invalid pointer address in attempting
3769 // to use a pointer argument in a call
3770 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3771 Assert.AreEqual (10014, ex.ErrorCode, "#3");
3772 Assert.IsNull (ex.InnerException, "#4");
3773 Assert.IsNotNull (ex.Message, "#5");
3774 Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
3775 Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
3779 [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3780 public void GetSocketOption2_Socket_Closed ()
3782 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3785 s.GetSocketOption (0, 0, (byte []) null);
3787 } catch (ObjectDisposedException ex) {
3788 // Cannot access a disposed object
3789 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3790 Assert.IsNull (ex.InnerException, "#3");
3791 Assert.IsNotNull (ex.Message, "#4");
3792 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3796 [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
3797 public void GetSocketOption3_Socket_Closed ()
3799 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3802 s.GetSocketOption (0, 0, 0);
3804 } catch (ObjectDisposedException ex) {
3805 // Cannot access a disposed object
3806 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3807 Assert.IsNull (ex.InnerException, "#3");
3808 Assert.IsNotNull (ex.Message, "#4");
3809 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3813 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3814 public void SetSocketOption1_DontLinger ()
3816 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3817 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
3818 new byte [] { 0x00 });
3819 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
3820 new byte [] { 0x01 });
3824 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3825 public void SetSocketOption1_DontLinger_Null ()
3827 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3829 s.SetSocketOption (SocketOptionLevel.Socket,
3830 SocketOptionName.DontLinger, (byte []) null);
3832 } catch (SocketException ex) {
3833 // The system detected an invalid pointer address in attempting
3834 // to use a pointer argument in a call
3835 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3836 Assert.AreEqual (10014, ex.ErrorCode, "#3");
3837 Assert.IsNull (ex.InnerException, "#4");
3838 Assert.IsNotNull (ex.Message, "#5");
3839 Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
3840 Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
3845 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3846 public void SetSocketOption1_Linger_Null ()
3848 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3850 s.SetSocketOption (SocketOptionLevel.Socket,
3851 SocketOptionName.DontLinger, (byte []) null);
3853 } catch (SocketException ex) {
3854 // The system detected an invalid pointer address in attempting
3855 // to use a pointer argument in a call
3856 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3857 Assert.AreEqual (10014, ex.ErrorCode, "#3");
3858 Assert.IsNull (ex.InnerException, "#4");
3859 Assert.IsNotNull (ex.Message, "#5");
3860 Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
3861 Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
3866 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3867 public void SetSocketOption1_Socket_Close ()
3869 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3872 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
3873 new byte [] { 0x00 });
3875 } catch (ObjectDisposedException ex) {
3876 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3877 Assert.IsNull (ex.InnerException, "#3");
3878 Assert.IsNotNull (ex.Message, "#4");
3879 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3883 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
3884 public void SetSocketOption2_DontLinger ()
3886 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3887 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 0);
3888 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 5);
3892 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
3893 [Category ("NotWorking")]
3894 public void SetSocketOption2_Linger ()
3896 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3897 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, 0);
3898 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, 5);
3902 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
3903 public void SetSocketOption2_Socket_Closed ()
3905 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3908 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 0);
3910 } catch (ObjectDisposedException ex) {
3911 // Cannot access a disposed object
3912 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3913 Assert.IsNull (ex.InnerException, "#3");
3914 Assert.IsNotNull (ex.Message, "#4");
3915 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3919 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3920 #if FEATURE_NO_BSD_SOCKETS
3921 [ExpectedException (typeof (PlatformNotSupportedException))]
3923 public void SetSocketOption3_AddMembershipIPv4_IPv6MulticastOption ()
3925 IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
3927 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
3928 s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
3930 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
3931 new IPv6MulticastOption (mcast_addr));
3933 } catch (ArgumentException ex) {
3934 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
3935 Assert.IsNull (ex.InnerException, "#3");
3936 Assert.IsNotNull (ex.Message, "#4");
3937 // The specified value is not a valid 'MulticastOption'
3938 Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
3939 Assert.AreEqual ("optionValue", ex.ParamName, "#6");
3944 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3945 #if FEATURE_NO_BSD_SOCKETS
3946 [ExpectedException (typeof (PlatformNotSupportedException))]
3948 public void SetSocketOption3_AddMembershipIPv4_MulticastOption ()
3950 IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
3952 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
3953 s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
3954 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
3955 new MulticastOption (mcast_addr));
3959 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3960 [Category ("NotWorking")]
3961 public void SetSocketOption3_AddMembershipIPv4_Socket_NotBound ()
3963 IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
3965 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
3967 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
3968 new MulticastOption (mcast_addr));
3970 } catch (SocketException ex) {
3971 // An invalid argument was supplied
3972 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3973 Assert.AreEqual (10022, ex.ErrorCode, "#3");
3974 Assert.IsNull (ex.InnerException, "#4");
3975 Assert.IsNotNull (ex.Message, "#5");
3976 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
3977 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
3983 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3984 #if FEATURE_NO_BSD_SOCKETS
3985 [ExpectedException (typeof (PlatformNotSupportedException))]
3987 public void SetSocketOption3_AddMembershipIPv6_IPv6MulticastOption ()
3989 if (!Socket.OSSupportsIPv6)
3990 Assert.Ignore ("IPv6 not enabled.");
3992 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
3994 using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
3995 s.Bind (new IPEndPoint (IPAddress.IPv6Any, NetworkHelpers.FindFreePort ()));
3996 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
3997 new IPv6MulticastOption (mcast_addr));
4001 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4002 #if FEATURE_NO_BSD_SOCKETS
4003 [ExpectedException (typeof (PlatformNotSupportedException))]
4005 public void SetSocketOption3_AddMembershipIPv6_MulticastOption ()
4007 if (!Socket.OSSupportsIPv6)
4008 Assert.Ignore ("IPv6 not enabled.");
4010 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
4012 using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
4013 s.Bind (new IPEndPoint (IPAddress.IPv6Any, NetworkHelpers.FindFreePort ()));
4015 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
4016 new MulticastOption (mcast_addr));
4018 } catch (ArgumentException ex) {
4019 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4020 Assert.IsNull (ex.InnerException, "#3");
4021 Assert.IsNotNull (ex.Message, "#4");
4022 // The specified value is not a valid 'IPv6MulticastOption'
4023 Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
4024 Assert.AreEqual ("optionValue", ex.ParamName, "#6");
4029 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4030 [Category ("NotWorking")]
4031 public void SetSocketOption3_AddMembershipIPv6_Socket_NotBound ()
4033 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
4035 Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
4037 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
4038 new IPv6MulticastOption (mcast_addr));
4040 } catch (SocketException ex) {
4041 // An invalid argument was supplied
4042 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
4043 Assert.AreEqual (10022, ex.ErrorCode, "#3");
4044 Assert.IsNull (ex.InnerException, "#4");
4045 Assert.IsNotNull (ex.Message, "#5");
4046 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
4047 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
4053 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4054 public void SetSocketOption3_DontLinger_Boolean ()
4056 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4058 s.SetSocketOption (SocketOptionLevel.Socket,
4059 SocketOptionName.DontLinger, (object) false);
4061 } catch (ArgumentException ex) {
4062 // The specified value is not valid
4063 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4064 Assert.IsNull (ex.InnerException, "#3");
4065 Assert.IsNotNull (ex.Message, "#4");
4066 Assert.AreEqual ("optionValue", ex.ParamName, "#5");
4071 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4072 public void SetSocketOption3_DontLinger_Int32 ()
4074 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4076 s.SetSocketOption (SocketOptionLevel.Socket,
4077 SocketOptionName.DontLinger, (object) 0);
4079 } catch (ArgumentException ex) {
4080 // The specified value is not valid
4081 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4082 Assert.IsNull (ex.InnerException, "#3");
4083 Assert.IsNotNull (ex.Message, "#4");
4084 Assert.AreEqual ("optionValue", ex.ParamName, "#5");
4089 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4090 public void SetSocketOption3_DontLinger_LingerOption ()
4092 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4094 s.SetSocketOption (SocketOptionLevel.Socket,
4095 SocketOptionName.DontLinger, new LingerOption (true, 1000));
4097 } catch (ArgumentException ex) {
4098 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4099 Assert.IsNull (ex.InnerException, "#3");
4100 // The specified value is not valid
4101 Assert.IsNotNull (ex.Message, "#4");
4102 Assert.AreEqual ("optionValue", ex.ParamName, "#5");
4107 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4108 public void SetSocketOption3_Linger_Boolean ()
4110 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4112 s.SetSocketOption (SocketOptionLevel.Socket,
4113 SocketOptionName.Linger, (object) false);
4115 } catch (ArgumentException ex) {
4116 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4117 Assert.IsNull (ex.InnerException, "#3");
4118 // The specified value is not valid
4119 Assert.IsNotNull (ex.Message, "#4");
4120 Assert.AreEqual ("optionValue", ex.ParamName, "#5");
4125 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4126 public void SetSocketOption3_Linger_Int32 ()
4128 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4130 s.SetSocketOption (SocketOptionLevel.Socket,
4131 SocketOptionName.Linger, (object) 0);
4133 } catch (ArgumentException ex) {
4134 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4135 Assert.IsNull (ex.InnerException, "#3");
4136 // The specified value is not valid
4137 Assert.IsNotNull (ex.Message, "#4");
4138 Assert.AreEqual ("optionValue", ex.ParamName, "#5");
4143 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4144 public void SetSocketOption3_Linger_LingerOption ()
4146 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4147 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
4148 new LingerOption (false, 0));
4149 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
4150 new LingerOption (true, 0));
4151 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
4152 new LingerOption (false, 1000));
4153 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
4154 new LingerOption (true, 1000));
4158 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4159 #if FEATURE_NO_BSD_SOCKETS
4160 [ExpectedException (typeof (PlatformNotSupportedException))]
4162 public void SetSocketOption3_DropMembershipIPv4_IPv6MulticastOption ()
4164 IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
4166 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
4167 s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
4168 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
4169 new MulticastOption (mcast_addr));
4171 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
4172 new IPv6MulticastOption (mcast_addr));
4174 } catch (ArgumentException ex) {
4175 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4176 Assert.IsNull (ex.InnerException, "#3");
4177 Assert.IsNotNull (ex.Message, "#4");
4178 // The specified value is not a valid 'MulticastOption'
4179 Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
4180 Assert.AreEqual ("optionValue", ex.ParamName, "#6");
4185 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4186 #if FEATURE_NO_BSD_SOCKETS
4187 [ExpectedException (typeof (PlatformNotSupportedException))]
4189 public void SetSocketOption3_DropMembershipIPv4_MulticastOption ()
4191 IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
4193 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
4194 MulticastOption option = new MulticastOption (mcast_addr);
4196 s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
4197 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
4199 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
4204 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4205 [Category ("NotWorking")]
4206 public void SetSocketOption3_DropMembershipIPv4_Socket_NotBound ()
4208 IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
4210 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
4212 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
4213 new MulticastOption (mcast_addr));
4215 } catch (SocketException ex) {
4216 // An invalid argument was supplied
4217 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
4218 Assert.AreEqual (10022, ex.ErrorCode, "#3");
4219 Assert.IsNull (ex.InnerException, "#4");
4220 Assert.IsNotNull (ex.Message, "#5");
4221 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
4222 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
4228 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4229 #if FEATURE_NO_BSD_SOCKETS
4230 [ExpectedException (typeof (PlatformNotSupportedException))]
4232 public void SetSocketOption3_DropMembershipIPv6_IPv6MulticastOption ()
4234 if (!Socket.OSSupportsIPv6)
4235 Assert.Ignore ("IPv6 not enabled.");
4237 using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
4238 IPv6MulticastOption option = new IPv6MulticastOption (
4239 IPAddress.Parse ("ff02::1"));
4241 s.Bind (new IPEndPoint (IPAddress.IPv6Any, 1902));
4242 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
4244 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
4249 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4250 #if FEATURE_NO_BSD_SOCKETS
4251 [ExpectedException (typeof (PlatformNotSupportedException))]
4253 public void SetSocketOption3_DropMembershipIPv6_MulticastOption ()
4255 if (!Socket.OSSupportsIPv6)
4256 Assert.Ignore ("IPv6 not enabled.");
4258 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
4260 using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
4261 s.Bind (new IPEndPoint (IPAddress.IPv6Any, NetworkHelpers.FindFreePort ()));
4262 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
4263 new IPv6MulticastOption (mcast_addr));
4265 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
4266 new MulticastOption (mcast_addr));
4268 } catch (ArgumentException ex) {
4269 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4270 Assert.IsNull (ex.InnerException, "#3");
4271 Assert.IsNotNull (ex.Message, "#4");
4272 // The specified value is not a valid 'IPv6MulticastOption'
4273 Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
4274 Assert.AreEqual ("optionValue", ex.ParamName, "#6");
4279 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4280 [Category ("NotWorking")]
4281 public void SetSocketOption3_DropMembershipIPv6_Socket_NotBound ()
4283 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
4285 Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
4287 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
4288 new IPv6MulticastOption (mcast_addr));
4290 } catch (SocketException ex) {
4291 // An invalid argument was supplied
4292 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
4293 Assert.AreEqual (10022, ex.ErrorCode, "#3");
4294 Assert.IsNull (ex.InnerException, "#4");
4295 Assert.IsNotNull (ex.Message, "#5");
4296 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
4297 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
4303 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4304 public void SetSocketOption3_OptionValue_Null ()
4306 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4308 s.SetSocketOption (SocketOptionLevel.Socket,
4309 SocketOptionName.Linger, (object) null);
4311 } catch (ArgumentNullException ex) {
4312 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
4313 Assert.IsNull (ex.InnerException, "#3");
4314 Assert.IsNotNull (ex.Message, "#4");
4315 Assert.AreEqual ("optionValue", ex.ParamName, "#5");
4320 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4321 public void SetSocketOption3_Socket_Closed ()
4323 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4326 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
4327 new LingerOption (false, 0));
4329 } catch (ObjectDisposedException ex) {
4330 // Cannot access a disposed object
4331 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
4332 Assert.IsNull (ex.InnerException, "#3");
4333 Assert.IsNotNull (ex.Message, "#4");
4334 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
4339 [Category ("RequiresBSDSockets")] // on watchOS device this happens: System.Net.Sockets.SocketException : The requested address is not valid in this context. This situation is too complex to detect and throw a PlatformNotSupportedException, so just ignore it.
4340 public void SetSocketOption_MulticastInterfaceIndex_Any ()
4342 IPAddress ip = IPAddress.Parse ("239.255.255.250");
4344 using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
4346 s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
4347 s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
4352 [Category ("RequiresBSDSockets")] // on watchOS device this happens: System.Net.Sockets.SocketException : The requested address is not valid in this context. This situation is too complex to detect and throw a PlatformNotSupportedException, so just ignore it.
4353 public void SetSocketOption_MulticastInterfaceIndex_Loopback ()
4355 IPAddress ip = IPAddress.Parse ("239.255.255.250");
4357 using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
4359 s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
4360 s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
4365 public void SetSocketOption_MulticastInterfaceIndex_Invalid ()
4367 IPAddress ip = IPAddress.Parse ("239.255.255.250");
4369 using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
4373 s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
4380 s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
4389 #if FEATURE_NO_BSD_SOCKETS
4390 [ExpectedException (typeof (PlatformNotSupportedException))]
4392 public void Shutdown_NoConnect ()
4394 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4395 s.Bind (new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ()));
4398 s.Shutdown (SocketShutdown.Both);
4400 } catch (SocketException exc) {
4401 Assert.AreEqual (10057, exc.ErrorCode, "#2");
4408 [ExpectedException (typeof (NullReferenceException))]
4409 public void ReceiveAsync_Null ()
4411 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4412 s.ReceiveAsync (null);
4417 [ExpectedException (typeof (NullReferenceException))]
4418 public void ReceiveAsync_Default ()
4420 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4421 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
4422 s.ReceiveAsync (saea);
4428 [ExpectedException (typeof (NullReferenceException))]
4429 public void ReceiveAsync_NullBuffer ()
4431 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4432 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
4433 saea.SetBuffer (null, 0, 0);
4434 s.ReceiveAsync (null);
4439 [ExpectedException (typeof (ObjectDisposedException))]
4440 public void ReceiveAsync_ClosedSocket ()
4442 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4444 s.ReceiveAsync (null);
4448 [ExpectedException (typeof (NullReferenceException))]
4449 public void SendAsync_Null ()
4451 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4457 [ExpectedException (typeof (NullReferenceException))]
4458 public void SendAsync_Default ()
4460 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4461 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
4468 [ExpectedException (typeof (NullReferenceException))]
4469 public void SendAsync_NullBuffer ()
4471 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4472 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
4473 saea.SetBuffer (null, 0, 0);
4479 [ExpectedException (typeof (ObjectDisposedException))]
4480 public void SendAsync_ClosedSocket ()
4482 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4488 #if FEATURE_NO_BSD_SOCKETS
4489 [ExpectedException (typeof (PlatformNotSupportedException))]
4491 public void SendAsyncFile ()
4493 Socket serverSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4495 serverSocket.Bind (new IPEndPoint (IPAddress.Loopback, 0));
4496 serverSocket.Listen (1);
4498 var mReceived = new ManualResetEvent (false);
4500 serverSocket.BeginAccept (AsyncCall => {
4501 byte[] bytes = new byte [1024];
4503 Socket listener = (Socket)AsyncCall.AsyncState;
4504 Socket client = listener.EndAccept (AsyncCall);
4505 client.Receive (bytes, bytes.Length, 0);
4510 Socket clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4511 clientSocket.Connect (serverSocket.LocalEndPoint);
4512 clientSocket.NoDelay = true;
4514 // Initialize buffer used to create testing file
4515 var buffer = new byte [1024];
4516 for (int i = 0; i < 1024; ++i)
4517 buffer [i] = (byte) (i % 256);
4519 string temp = Path.GetTempFileName ();
4521 // Testing file creation
4522 using (StreamWriter sw = new StreamWriter (temp)) {
4526 var mSent = new ManualResetEvent (false);
4528 // Async Send File to server
4529 clientSocket.BeginSendFile(temp, (ar) => {
4530 Socket client = (Socket) ar.AsyncState;
4531 client.EndSendFile (ar);
4535 if (!mSent.WaitOne (1500))
4536 throw new TimeoutException ();
4537 if (!mReceived.WaitOne (1500))
4538 throw new TimeoutException ();
4540 if (File.Exists (temp))
4543 clientSocket.Close ();
4544 serverSocket.Close ();
4549 #if FEATURE_NO_BSD_SOCKETS
4550 [ExpectedException (typeof (PlatformNotSupportedException))]
4552 public void ConnectToIPV4EndPointUsingDualModelSocket () {
4554 * IPv6 DualMode sockets are defaults in Mono. Explicitly specify that
4555 * anyways in this test to make it more interoparable with .NET where
4556 * IPv6 and DualMode needs to be specified.
4558 using (var server = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)) {
4560 var host = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
4562 server.DualMode = true;
4565 * Nothing to Accept the connect - we need a backlog to make sure we don't get
4570 var ep = server.LocalEndPoint as IPEndPoint;
4571 var client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
4572 client.DualMode = true;
4573 client.Connect (ep);
4574 client.Disconnect (false);
4577 client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
4578 client.DualMode = true;
4579 client.Connect (IPAddress.Loopback, ep.Port);
4580 client.Disconnect (false);
4583 client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
4584 client.DualMode = true;
4585 client.Connect (new [] { IPAddress.Loopback }, ep.Port);
4586 client.Disconnect (false);
4592 #if FEATURE_NO_BSD_SOCKETS
4593 [ExpectedException (typeof (PlatformNotSupportedException))]
4595 public void BeginConnectToIPV4EndPointUsingDualModelSocket () {
4597 * IPv6 DualMode sockets are defaults in Mono. Explicitly specify that
4598 * anyways in this test to make it more interoparable with .NET where
4599 * IPv6 and DualMode needs to be specified.
4601 using (var server = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
4603 var host = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
4605 server.DualMode = true;
4609 var ep = server.LocalEndPoint as IPEndPoint;
4611 BCCalledBack.Reset ();
4612 var client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
4613 client.DualMode = true;
4614 var ar1 = client.BeginConnect (ep, BCCallback, client);
4615 Assert.IsTrue (BCCalledBack.WaitOne (10000), "#1");
4616 client.Disconnect (false);
4619 BCCalledBack.Reset ();
4620 client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
4621 client.DualMode = true;
4622 var ar2 = client.BeginConnect (IPAddress.Loopback, ep.Port, BCCallback, client);
4623 Assert.IsTrue (BCCalledBack.WaitOne (10000), "#2");
4624 client.Disconnect (false);
4627 BCCalledBack.Reset ();
4628 client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
4629 client.DualMode = true;
4630 var ar3 = client.BeginConnect (new [] {IPAddress.Loopback}, ep.Port, BCCallback, client);
4631 Assert.IsTrue (BCCalledBack.WaitOne (10000), "#2");
4632 client.Disconnect (false);
4638 #if FEATURE_NO_BSD_SOCKETS
4639 [ExpectedException (typeof (PlatformNotSupportedException))]
4641 public void UdpMulticasTimeToLive ()
4643 /* see https://bugzilla.xamarin.com/show_bug.cgi?id=36941 */
4645 using (Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
4646 IPEndPoint end_point = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
4647 socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
4648 socket.Bind (end_point);
4649 socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 19);
4653 [Test] // Covers 41616
4654 public void ConnectAsyncUnhandledEx ()
4656 var mre = new ManualResetEvent (false);
4658 var endPoint = new IPEndPoint(0,0);
4659 var socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Unspecified);
4661 var socketArgs = new SocketAsyncEventArgs();
4662 socketArgs.RemoteEndPoint = endPoint;
4663 socketArgs.Completed += (sender, e) => mre.Set ();
4665 socket.ConnectAsync (socketArgs);
4667 Assert.IsTrue (mre.WaitOne (1000), "ConnectedAsync timeout");