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 public const string BogusAddress = "192.168.244.244";
36 #if FEATURE_NO_BSD_SOCKETS
37 [ExpectedException (typeof (PlatformNotSupportedException))]
39 public void ConnectIPAddressAny ()
41 IPEndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
43 /* UDP sockets use Any to disconnect
45 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
50 } catch (SocketException ex) {
51 Assert.AreEqual (10049, ex.ErrorCode, "#2");
56 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
61 } catch (SocketException ex) {
62 Assert.AreEqual (10049, ex.ErrorCode, "#4");
67 [Ignore ("Bug #75158")] // Looks like MS fails after the .ctor, when you try to use the socket
68 public void IncompatibleAddress ()
70 IPEndPoint epIPv6 = new IPEndPoint (IPAddress.IPv6Any,
71 NetworkHelpers.FindFreePort ());
74 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)) {
79 } catch (SocketException ex) {
80 // address incompatible with protocol
81 int expectedError = 10047;
82 Assert.AreEqual (expectedError, ex.ErrorCode,
88 [Category ("NotWorking")]
89 #if FEATURE_NO_BSD_SOCKETS
90 [ExpectedException (typeof (PlatformNotSupportedException))]
92 public void BogusEndConnect ()
94 IPAddress ipOne = IPAddress.Parse (BogusAddress);
95 IPEndPoint ipEP = new IPEndPoint (ipOne, NetworkHelpers.FindFreePort ());
96 Socket sock = new Socket (ipEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
97 IAsyncResult ar = sock.BeginConnect (ipEP, null, null);
100 // should raise an exception because connect was bogus
101 sock.EndConnect (ar);
103 } catch (SocketException ex) {
104 // Actual error code depends on network configuration.
105 var error = (SocketError) ex.ErrorCode;
106 Assert.That (error == SocketError.TimedOut ||
107 error == SocketError.ConnectionRefused ||
108 error == SocketError.NetworkUnreachable ||
109 error == SocketError.HostUnreachable, "#2");
114 [ExpectedException (typeof (ArgumentNullException))]
115 public void SelectEmpty ()
117 ArrayList list = new ArrayList ();
118 Socket.Select (list, list, list, 1000);
121 private bool BlockingConnect (bool block, int port)
123 IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, port);
124 Socket server = new Socket(AddressFamily.InterNetwork,
128 server.Blocking=block;
132 Socket conn = new Socket (AddressFamily.InterNetwork,
137 Socket client = null;
138 var sw = Stopwatch.StartNew ();
139 while (sw.ElapsedMilliseconds < 100)
142 client = server.Accept();
145 catch (SocketException ex) {
146 if (ex.SocketErrorCode == SocketError.WouldBlock)
151 Assert.IsNotNull (client, "Couldn't accept a client connection within 100ms.");
152 bool client_block = client.Blocking;
158 return(client_block);
162 #if FEATURE_NO_BSD_SOCKETS
163 [ExpectedException (typeof (PlatformNotSupportedException))]
165 public void AcceptBlockingStatus()
168 var port = NetworkHelpers.FindFreePort ();
170 block = BlockingConnect(true, port);
171 Assert.AreEqual (block, true, "BlockingStatus01");
173 block = BlockingConnect(false, port);
174 Assert.AreEqual (block, false, "BlockingStatus02");
177 static bool CFAConnected = false;
178 static ManualResetEvent CFACalledBack;
180 private static void CFACallback (IAsyncResult asyncResult)
182 Socket sock = (Socket)asyncResult.AsyncState;
183 CFAConnected = sock.Connected;
185 if (sock.Connected) {
186 sock.EndConnect (asyncResult);
189 CFACalledBack.Set ();
192 [Test] // Connect (IPEndPoint)
193 public void Connect1_RemoteEP_Null ()
195 Socket s = new Socket (AddressFamily.InterNetwork,
196 SocketType.Stream, ProtocolType.Tcp);
198 s.Connect ((IPEndPoint) null);
200 } catch (ArgumentNullException ex) {
201 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
202 Assert.IsNull (ex.InnerException, "#3");
203 Assert.IsNotNull (ex.Message, "#4");
204 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
209 #if FEATURE_NO_BSD_SOCKETS
210 [ExpectedException (typeof (PlatformNotSupportedException))]
212 public void ConnectFailAsync ()
214 Socket sock = new Socket (AddressFamily.InterNetwork,
217 sock.Blocking = false;
218 CFACalledBack = new ManualResetEvent (false);
219 CFACalledBack.Reset ();
221 /* Need a port that is not being used for
224 sock.BeginConnect (new IPEndPoint (IPAddress.Loopback,
225 NetworkHelpers.FindFreePort ()),
226 new AsyncCallback (CFACallback),
228 CFACalledBack.WaitOne ();
230 Assert.AreEqual (CFAConnected, false, "ConnectFail");
234 #if FEATURE_NO_BSD_SOCKETS
235 [ExpectedException (typeof (PlatformNotSupportedException))]
237 public void SetSocketOptionBoolean ()
239 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
240 Socket sock = new Socket (ep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
242 sock.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
248 #if FEATURE_NO_BSD_SOCKETS
249 [ExpectedException (typeof (PlatformNotSupportedException))]
251 public void TestSelect1 ()
253 Socket srv = CreateServer (NetworkHelpers.FindFreePort ());
254 ClientSocket clnt = new ClientSocket (srv.LocalEndPoint);
255 Thread th = new Thread (new ThreadStart (clnt.ConnectSleepClose));
261 ArrayList list = new ArrayList ();
262 ArrayList empty = new ArrayList ();
264 Socket.Select (list, empty, empty, 100);
265 Assert.AreEqual (0, empty.Count, "#01");
266 Assert.AreEqual (1, list.Count, "#02");
267 Socket.Select (empty, list, empty, 100);
268 Assert.AreEqual (0, empty.Count, "#03");
269 Assert.AreEqual (1, list.Count, "#04");
270 Socket.Select (list, empty, empty, -1);
271 Assert.AreEqual (0, empty.Count, "#05");
272 Assert.AreEqual (1, list.Count, "#06");
273 // Need to read the 10 bytes from the client to avoid a RST
274 byte [] bytes = new byte [10];
283 static Socket CreateServer (int port)
285 Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
286 sock.Bind (new IPEndPoint (IPAddress.Loopback, port));
295 public ClientSocket (EndPoint ep)
298 sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
301 public void ConnectSleepClose ()
310 byte [] b = new byte [10];
315 byte[] buf = new byte[100];
318 [ExpectedException (typeof (ObjectDisposedException))]
319 public void Disposed2 ()
321 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
328 [ExpectedException (typeof (ObjectDisposedException))]
329 public void Disposed6 ()
331 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
338 [ExpectedException (typeof (ObjectDisposedException))]
339 public void Disposed7 ()
341 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
348 [ExpectedException (typeof (ObjectDisposedException))]
349 public void Disposed15 ()
351 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
358 [ExpectedException (typeof (ObjectDisposedException))]
359 public void Disposed16 ()
361 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
368 [ExpectedException (typeof (ObjectDisposedException))]
369 public void Disposed17 ()
371 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
378 [ExpectedException (typeof (ObjectDisposedException))]
379 public void Disposed18 ()
381 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
384 s.Send (buf, 0, 10, 0);
388 #if FEATURE_NO_BSD_SOCKETS
389 [ExpectedException (typeof (PlatformNotSupportedException))]
391 [ExpectedException (typeof (ObjectDisposedException))]
393 public void Disposed19 ()
395 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
396 EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
399 s.SendTo (buf, 0, ep);
403 #if FEATURE_NO_BSD_SOCKETS
404 [ExpectedException (typeof (PlatformNotSupportedException))]
406 [ExpectedException (typeof (ObjectDisposedException))]
408 public void Disposed20 ()
410 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
411 EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
414 s.SendTo (buf, 10, 0, ep);
418 #if FEATURE_NO_BSD_SOCKETS
419 [ExpectedException (typeof (PlatformNotSupportedException))]
421 [ExpectedException (typeof (ObjectDisposedException))]
423 public void Disposed21 ()
425 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
426 EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
429 s.SendTo (buf, 0, 10, 0, ep);
433 #if FEATURE_NO_BSD_SOCKETS
434 [ExpectedException (typeof (PlatformNotSupportedException))]
436 [ExpectedException (typeof (ObjectDisposedException))]
438 public void Disposed22 ()
440 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
441 EndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
448 [ExpectedException (typeof (ObjectDisposedException))]
449 public void Disposed23 ()
451 Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
458 #if FEATURE_NO_BSD_SOCKETS
459 [ExpectedException (typeof (PlatformNotSupportedException))]
461 public void GetHashCodeTest ()
463 Socket server = new Socket (AddressFamily.InterNetwork,
464 SocketType.Stream, ProtocolType.Tcp);
465 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
466 NetworkHelpers.FindFreePort ());
470 Socket client = new Socket (AddressFamily.InterNetwork,
471 SocketType.Stream, ProtocolType.Tcp);
472 int hashcodeA = client.GetHashCode ();
474 int hashcodeB = client.GetHashCode ();
475 Assert.AreEqual (hashcodeA, hashcodeB, "#1");
477 int hashcodeC = client.GetHashCode ();
478 Assert.AreEqual (hashcodeB, hashcodeC, "#2");
482 static ManualResetEvent SocketError_event = new ManualResetEvent (false);
484 private static void SocketError_callback (IAsyncResult ar)
486 Socket sock = (Socket)ar.AsyncState;
489 sock.EndConnect (ar);
492 SocketError_event.Set ();
496 [Category ("RequiresBSDSockets")] // This verifies particular error codes, which we don't care about when nothing's working anyway.
497 public void SocketErrorTest ()
499 Socket sock = new Socket (AddressFamily.InterNetwork,
502 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
504 SocketError_event.Reset ();
506 sock.Blocking = false;
507 sock.BeginConnect (ep, new AsyncCallback(SocketError_callback),
510 if (SocketError_event.WaitOne (2000, false) == false) {
511 Assert.Fail ("SocketError wait timed out");
514 Assert.AreEqual (false, sock.Connected, "SocketError #1");
518 error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
519 Assert.AreEqual (10061, error, "SocketError #2");
521 error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
522 Assert.AreEqual (10061, error, "SocketError #3");
529 public void SocketInformationCtor ()
534 public void DontFragmentDefaultTcp ()
536 Socket sock = new Socket (AddressFamily.InterNetwork,
540 Assert.AreEqual (false, sock.DontFragment, "DontFragmentDefaultTcp");
546 [Category ("NotWorking")] // DontFragment doesn't work
547 public void DontFragmentChangeTcp ()
549 Socket sock = new Socket (AddressFamily.InterNetwork,
553 sock.DontFragment = true;
555 Assert.AreEqual (true, sock.DontFragment, "DontFragmentChangeTcp");
561 public void DontFragmentDefaultUdp ()
563 Socket sock = new Socket (AddressFamily.InterNetwork,
567 Assert.AreEqual (false, sock.DontFragment, "DontFragmentDefaultUdp");
573 [Category ("NotWorking")] // DontFragment doesn't work
574 public void DontFragmentChangeUdp ()
576 Socket sock = new Socket (AddressFamily.InterNetwork,
580 sock.DontFragment = true;
582 Assert.AreEqual (true, sock.DontFragment, "DontFragmentChangeUdp");
588 [ExpectedException (typeof(ObjectDisposedException))]
589 public void DontFragmentClosed ()
591 Socket sock = new Socket (AddressFamily.InterNetwork,
597 bool val = sock.DontFragment;
601 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
602 public void DontFragment ()
604 Socket sock = new Socket (AddressFamily.NetBios,
605 SocketType.Seqpacket,
606 ProtocolType.Unspecified);
609 sock.DontFragment = true;
610 Assert.Fail ("DontFragment #1");
611 } catch (NotSupportedException) {
618 public void EnableBroadcastDefaultTcp ()
620 Socket sock = new Socket (AddressFamily.InterNetwork,
625 bool value = sock.EnableBroadcast;
626 Assert.Fail ("EnableBroadcastDefaultTcp #1");
627 } catch (SocketException ex) {
628 Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastDefaultTcp #2");
635 public void EnableBroadcastDefaultUdp ()
637 Socket sock = new Socket (AddressFamily.InterNetwork,
641 Assert.AreEqual (false, sock.EnableBroadcast, "EnableBroadcastDefaultUdp");
647 public void EnableBroadcastChangeTcp ()
649 Socket sock = new Socket (AddressFamily.InterNetwork,
654 sock.EnableBroadcast = true;
655 Assert.Fail ("EnableBroadcastChangeTcp #1");
656 } catch (SocketException ex) {
657 Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastChangeTcp #2");
664 public void EnableBroadcastChangeUdp ()
666 Socket sock = new Socket (AddressFamily.InterNetwork,
670 sock.EnableBroadcast = true;
672 Assert.AreEqual (true, sock.EnableBroadcast, "EnableBroadcastChangeUdp");
678 [ExpectedException (typeof(ObjectDisposedException))]
679 public void EnableBroadcastClosed ()
681 Socket sock = new Socket (AddressFamily.InterNetwork,
687 bool val = sock.EnableBroadcast;
690 /* Can't test the default for ExclusiveAddressUse as
691 * it's different on different versions and service
695 [Category ("NotWorking")] // Not supported on Linux
696 public void ExclusiveAddressUseUnbound ()
698 Socket sock = new Socket (AddressFamily.InterNetwork,
702 sock.ExclusiveAddressUse = true;
704 Assert.AreEqual (true, sock.ExclusiveAddressUse, "ExclusiveAddressUseUnbound");
710 [ExpectedException (typeof(InvalidOperationException))]
711 [Category ("NotWorking")] // Not supported on Linux
712 public void ExclusiveAddressUseBound ()
714 Socket sock = new Socket (AddressFamily.InterNetwork,
718 sock.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
719 sock.ExclusiveAddressUse = true;
724 [ExpectedException (typeof(ObjectDisposedException))]
725 public void ExclusiveAddressUseClosed ()
727 Socket sock = new Socket (AddressFamily.InterNetwork,
733 bool val = sock.ExclusiveAddressUse;
737 #if FEATURE_NO_BSD_SOCKETS
738 [ExpectedException (typeof (PlatformNotSupportedException))]
740 public void IsBoundTcp ()
742 Socket sock = new Socket (AddressFamily.InterNetwork,
745 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
747 Assert.AreEqual (false, sock.IsBound, "IsBoundTcp #1");
750 Assert.AreEqual (true, sock.IsBound, "IsBoundTcp #2");
754 Socket sock2 = new Socket (AddressFamily.InterNetwork,
758 Assert.AreEqual (false, sock2.IsBound, "IsBoundTcp #3");
761 Assert.AreEqual (true, sock2.IsBound, "IsBoundTcp #4");
764 Assert.AreEqual (true, sock2.IsBound, "IsBoundTcp #5");
767 Assert.AreEqual (true, sock.IsBound, "IsBoundTcp #6");
771 #if FEATURE_NO_BSD_SOCKETS
772 [ExpectedException (typeof (PlatformNotSupportedException))]
774 public void IsBoundUdp ()
776 Socket sock = new Socket (AddressFamily.InterNetwork,
779 IPEndPoint ep = NetworkHelpers.LocalEphemeralEndPoint ();
781 Assert.AreEqual (false, sock.IsBound, "IsBoundUdp #1");
784 Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #2");
787 Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #3");
790 sock = new Socket (AddressFamily.InterNetwork,
794 Assert.AreEqual (false, sock.IsBound, "IsBoundUdp #4");
797 Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #5");
800 Assert.AreEqual (true, sock.IsBound, "IsBoundUdp #6");
804 /* Should not throw an exception */
805 public void IsBoundClosed ()
807 Socket sock = new Socket (AddressFamily.InterNetwork,
813 bool val = sock.IsBound;
816 /* Nothing much to test for LingerState */
819 public void MulticastLoopbackDefaultTcp ()
821 Socket sock = new Socket (AddressFamily.InterNetwork,
826 bool value = sock.MulticastLoopback;
827 Assert.Fail ("MulticastLoopbackDefaultTcp #1");
828 } catch (SocketException ex) {
829 Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackDefaultTcp #2");
836 public void MulticastLoopbackChangeTcp ()
838 Socket sock = new Socket (AddressFamily.InterNetwork,
843 sock.MulticastLoopback = false;
844 Assert.Fail ("MulticastLoopbackChangeTcp #1");
845 } catch (SocketException ex) {
846 Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackChangeTcp #2");
853 public void MulticastLoopbackDefaultUdp ()
855 Socket sock = new Socket (AddressFamily.InterNetwork,
859 Assert.AreEqual (true, sock.MulticastLoopback, "MulticastLoopbackDefaultUdp");
865 public void MulticastLoopbackChangeUdp ()
867 Socket sock = new Socket (AddressFamily.InterNetwork,
871 sock.MulticastLoopback = false;
873 Assert.AreEqual (false, sock.MulticastLoopback, "MulticastLoopbackChangeUdp");
879 [ExpectedException (typeof(ObjectDisposedException))]
880 public void MulticastLoopbackClosed ()
882 Socket sock = new Socket (AddressFamily.InterNetwork,
888 bool val = sock.MulticastLoopback;
891 /* OSSupportsIPv6 depends on the environment */
894 [Category("NotWorking")] // We have different defaults for perf reasons
895 public void ReceiveBufferSizeDefault ()
897 Socket sock = new Socket (AddressFamily.InterNetwork,
901 Assert.AreEqual (8192, sock.ReceiveBufferSize, "ReceiveBufferSizeDefault");
907 [Category("NotWorking")] // We have different defaults for perf reasons
908 public void ReceiveBufferSizeDefaultUdp ()
910 Socket sock = new Socket (AddressFamily.InterNetwork,
914 Assert.AreEqual (8192, sock.ReceiveBufferSize, "ReceiveBufferSizeDefaultUdp");
920 public void ReceiveBufferSizeChange ()
922 Socket sock = new Socket (AddressFamily.InterNetwork,
926 sock.ReceiveBufferSize = 16384;
928 Assert.AreEqual (16384, sock.ReceiveBufferSize, "ReceiveBufferSizeChange");
934 [Category("NotWorking")] // We cannot totally remove buffers (minimum is set to 256
935 public void BuffersCheck_None ()
937 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
938 int original = s.ReceiveBufferSize;
939 s.ReceiveBufferSize = 0;
940 Assert.AreEqual (0, s.ReceiveBufferSize, "ReceiveBufferSize " + original.ToString ());
942 original = s.SendBufferSize;
943 s.SendBufferSize = 0;
944 Assert.AreEqual (0, s.SendBufferSize, "SendBufferSize " + original.ToString ());
949 [ExpectedException (typeof(ObjectDisposedException))]
950 public void ReceiveBufferSizeClosed ()
952 Socket sock = new Socket (AddressFamily.InterNetwork,
958 int val = sock.ReceiveBufferSize;
962 [Category("NotWorking")] // We have different defaults for perf reasons
963 public void SendBufferSizeDefault ()
965 Socket sock = new Socket (AddressFamily.InterNetwork,
969 Assert.AreEqual (8192, sock.SendBufferSize, "SendBufferSizeDefault");
975 [Category("NotWorking")] // We have different defaults for perf reasons
976 public void SendBufferSizeDefaultUdp ()
978 Socket sock = new Socket (AddressFamily.InterNetwork,
982 Assert.AreEqual (8192, sock.SendBufferSize, "SendBufferSizeDefaultUdp");
988 public void SendBufferSizeChange ()
990 Socket sock = new Socket (AddressFamily.InterNetwork,
994 sock.SendBufferSize = 16384;
996 Assert.AreEqual (16384, sock.SendBufferSize, "SendBufferSizeChange");
1002 [ExpectedException (typeof(ObjectDisposedException))]
1003 public void SendBufferSizeClosed ()
1005 Socket sock = new Socket (AddressFamily.InterNetwork,
1011 int val = sock.SendBufferSize;
1014 /* No test for TTL default as it's platform dependent */
1016 public void TtlChange ()
1018 Socket sock = new Socket (AddressFamily.InterNetwork,
1024 Assert.AreEqual (255, sock.Ttl, "TtlChange");
1030 public void TtlChangeOverflow ()
1032 Socket sock = new Socket (AddressFamily.InterNetwork,
1038 Assert.Fail ("TtlChangeOverflow #1");
1039 } catch (ArgumentOutOfRangeException ex) {
1040 Assert.AreEqual ("value", ex.ParamName,
1041 "TtlChangeOverflow #2");
1047 /* Apparently you can set TTL=0 on the ms runtime!!
1050 Assert.Fail ("TtlChangeOverflow #4");
1051 } catch (SocketException ex) {
1052 Assert.AreEqual (10022, ex.ErrorCode,
1053 "TtlChangeOverflow #5");
1060 [ExpectedException (typeof(ObjectDisposedException))]
1061 public void TtlClosed ()
1063 Socket sock = new Socket (AddressFamily.InterNetwork,
1073 public void UseOnlyOverlappedIODefault ()
1075 Socket sock = new Socket (AddressFamily.InterNetwork,
1079 Assert.AreEqual (false, sock.UseOnlyOverlappedIO, "UseOnlyOverlappedIODefault");
1085 // We need this because the Linux kernel in certain configurations
1086 // will end up rounding up the values passed on to the kernel
1087 // for socket send/receive timeouts.
1089 int Approximate (int target, int value)
1093 if (value > target-10 && value < target+10)
1099 public void UseOnlyOverlappedIOChange ()
1101 Socket sock = new Socket (AddressFamily.InterNetwork,
1105 sock.UseOnlyOverlappedIO = true;
1107 Assert.AreEqual (true, sock.UseOnlyOverlappedIO, "UseOnlyOverlappedIOChange");
1113 /* Should not throw an exception */
1114 public void UseOnlyOverlappedIOClosed ()
1116 Socket sock = new Socket (AddressFamily.InterNetwork,
1122 bool val = sock.UseOnlyOverlappedIO;
1126 public void SendTimeoutDefault ()
1128 Socket sock = new Socket (AddressFamily.InterNetwork,
1132 Assert.AreEqual (0, sock.SendTimeout, "SendTimeoutDefault");
1138 public void SendTimeoutChange ()
1140 Socket sock = new Socket (AddressFamily.InterNetwork,
1144 /* Should be rounded up to 500, according to
1145 * the MSDN docs, but the MS runtime doesn't
1147 sock.SendTimeout = 50;
1148 Assert.AreEqual (50, Approximate (50, sock.SendTimeout), "SendTimeoutChange #1");
1150 sock.SendTimeout = 2000;
1151 Assert.AreEqual (2000, Approximate (2000, sock.SendTimeout), "SendTimeoutChange #2");
1153 sock.SendTimeout = 0;
1154 Assert.AreEqual (0, Approximate (0, sock.SendTimeout), "SendTimeoutChange #3");
1156 /* Should be the same as setting 0 */
1157 sock.SendTimeout = -1;
1158 Assert.AreEqual (0, sock.SendTimeout, "SendTimeoutChange #4");
1160 sock.SendTimeout = 65536;
1161 Assert.AreEqual (65536, Approximate (65536, sock.SendTimeout), "SendTimeoutChange #5");
1164 sock.SendTimeout = -2;
1165 Assert.Fail ("SendTimeoutChange #8");
1166 } catch (ArgumentOutOfRangeException) {
1173 [ExpectedException (typeof(ObjectDisposedException))]
1174 public void SendTimeoutClosed ()
1176 Socket sock = new Socket (AddressFamily.InterNetwork,
1182 int val = sock.SendTimeout;
1186 public void ReceiveTimeoutDefault ()
1188 Socket sock = new Socket (AddressFamily.InterNetwork,
1192 Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutDefault");
1198 public void ReceiveTimeoutChange ()
1200 Socket sock = new Socket (AddressFamily.InterNetwork,
1204 sock.ReceiveTimeout = 50;
1205 Assert.AreEqual (50, Approximate (50, sock.ReceiveTimeout), "ReceiveTimeoutChange #1");
1207 sock.ReceiveTimeout = 2000;
1208 Assert.AreEqual (2000, Approximate (2000, sock.ReceiveTimeout), "ReceiveTimeoutChange #2");
1210 sock.ReceiveTimeout = 0;
1211 Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutChange #3");
1213 /* Should be the same as setting 0 */
1214 sock.ReceiveTimeout = -1;
1215 Assert.AreEqual (0, sock.ReceiveTimeout, "ReceiveTimeoutChange #4");
1217 sock.ReceiveTimeout = 65536;
1218 Assert.AreEqual (65536, Approximate (65536, sock.ReceiveTimeout), "ReceiveTimeoutChange #5");
1221 sock.ReceiveTimeout = -2;
1222 Assert.Fail ("ReceiveTimeoutChange #8");
1223 } catch (ArgumentOutOfRangeException) {
1230 [ExpectedException (typeof(ObjectDisposedException))]
1231 public void ReceiveTimeoutClosed ()
1233 Socket sock = new Socket (AddressFamily.InterNetwork,
1239 int val = sock.ReceiveTimeout;
1243 public void NoDelayDefaultTcp ()
1245 Socket sock = new Socket (AddressFamily.InterNetwork,
1249 Assert.AreEqual (false, sock.NoDelay, "NoDelayDefaultTcp");
1255 public void NoDelayChangeTcp ()
1257 Socket sock = new Socket (AddressFamily.InterNetwork,
1261 sock.NoDelay = true;
1263 Assert.AreEqual (true, sock.NoDelay, "NoDelayChangeTcp");
1269 public void NoDelayDefaultUdp ()
1271 Socket sock = new Socket (AddressFamily.InterNetwork,
1276 bool val = sock.NoDelay;
1277 Assert.Fail ("NoDelayDefaultUdp #1");
1278 } catch (SocketException ex) {
1279 Assert.AreEqual (10042, ex.ErrorCode,
1280 "NoDelayDefaultUdp #2");
1287 public void NoDelayChangeUdp ()
1289 Socket sock = new Socket (AddressFamily.InterNetwork,
1294 sock.NoDelay = true;
1295 Assert.Fail ("NoDelayChangeUdp #1");
1296 } catch (SocketException ex) {
1297 Assert.AreEqual (10042, ex.ErrorCode,
1298 "NoDelayChangeUdp #2");
1305 [ExpectedException (typeof(ObjectDisposedException))]
1306 public void NoDelayClosed ()
1308 Socket sock = new Socket (AddressFamily.InterNetwork,
1314 bool val = sock.NoDelay;
1317 static bool BAAccepted = false;
1318 static Socket BASocket = null;
1319 static ManualResetEvent BACalledBack = new ManualResetEvent (false);
1321 private static void BACallback (IAsyncResult asyncResult)
1323 Socket sock = (Socket)asyncResult.AsyncState;
1325 BASocket = sock.EndAccept (asyncResult);
1328 BACalledBack.Set ();
1332 [ExpectedException (typeof(InvalidOperationException))]
1333 public void BeginAcceptNotBound ()
1335 Socket sock = new Socket (AddressFamily.InterNetwork,
1339 sock.BeginAccept (BACallback, sock);
1345 #if FEATURE_NO_BSD_SOCKETS
1346 [ExpectedException (typeof (PlatformNotSupportedException))]
1348 [ExpectedException (typeof(InvalidOperationException))]
1350 public void BeginAcceptNotListening ()
1352 Socket sock = new Socket (AddressFamily.InterNetwork,
1356 sock.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
1358 sock.BeginAccept (BACallback, sock);
1364 #if FEATURE_NO_BSD_SOCKETS
1365 [ExpectedException (typeof (PlatformNotSupportedException))]
1367 public void BeginAccept ()
1369 Socket sock = new Socket (AddressFamily.InterNetwork,
1372 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1373 NetworkHelpers.FindFreePort ());
1378 BACalledBack.Reset ();
1380 sock.BeginAccept (BACallback, sock);
1382 Socket conn = new Socket (AddressFamily.InterNetwork,
1388 if (BACalledBack.WaitOne (2000, false) == false) {
1389 Assert.Fail ("BeginAccept wait timed out");
1392 Assert.AreEqual (true, BAAccepted, "BeginAccept #1");
1393 Assert.AreEqual (true, BASocket.Connected, "BeginAccept #2");
1394 Assert.AreEqual (false, sock.Connected, "BeginAccept #3");
1395 Assert.AreEqual (true, conn.Connected, "BeginAccept #4");
1403 [ExpectedException (typeof(ObjectDisposedException))]
1404 public void BeginAcceptClosed ()
1406 Socket sock = new Socket (AddressFamily.InterNetwork,
1412 sock.BeginAccept (BACallback, sock);
1415 static bool BADAccepted = false;
1416 static Socket BADSocket = null;
1417 static byte[] BADBytes;
1418 static int BADByteCount;
1419 static ManualResetEvent BADCalledBack = new ManualResetEvent (false);
1421 private static void BADCallback (IAsyncResult asyncResult)
1423 Socket sock = (Socket)asyncResult.AsyncState;
1425 BADSocket = sock.EndAccept (out BADBytes,
1430 BADCalledBack.Set ();
1434 #if FEATURE_NO_BSD_SOCKETS
1435 [ExpectedException (typeof (PlatformNotSupportedException))]
1437 public void BeginAcceptData ()
1439 Socket sock = new Socket (AddressFamily.InterNetwork,
1442 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1443 NetworkHelpers.FindFreePort ());
1448 BADCalledBack.Reset ();
1450 sock.BeginAccept (256, BADCallback, sock);
1452 Socket conn = new Socket (AddressFamily.InterNetwork,
1455 byte[] send_bytes = new byte[] {10, 11, 12, 13};
1458 conn.Send (send_bytes);
1460 if (BADCalledBack.WaitOne (2000, false) == false) {
1461 Assert.Fail ("BeginAcceptData wait timed out");
1464 Assert.AreEqual (true, BADAccepted, "BeginAcceptData #1");
1465 Assert.AreEqual (true, BADSocket.Connected, "BeginAcceptData #2");
1466 Assert.AreEqual (false, sock.Connected, "BeginAcceptData #3");
1467 Assert.AreEqual (true, conn.Connected, "BeginAcceptData #4");
1468 Assert.AreEqual (send_bytes.Length, BADByteCount, "BeginAcceptData #5");
1470 /* The MS runtime gives the returned data in a
1471 * much bigger array. TODO: investigate
1472 * whether it the size correlates to the first
1473 * parameter in BeginAccept()
1475 Assert.IsFalse (BADBytes.Length == send_bytes.Length,
1476 "BeginAcceptData #6");
1478 for(int i = 0; i < send_bytes.Length; i++) {
1479 Assert.AreEqual (send_bytes[i], BADBytes[i], "BeginAcceptData #" + (i+7).ToString ());
1488 [ExpectedException (typeof(ObjectDisposedException))]
1489 public void BeginAcceptDataClosed ()
1491 Socket sock = new Socket (AddressFamily.InterNetwork,
1497 sock.BeginAccept (256, BADCallback, sock);
1501 #if FEATURE_NO_BSD_SOCKETS
1502 [ExpectedException (typeof (PlatformNotSupportedException))]
1504 public void BeginAcceptSocketUdp ()
1506 Socket sock = new Socket (AddressFamily.InterNetwork,
1509 Socket acc = new Socket (AddressFamily.InterNetwork,
1513 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1514 NetworkHelpers.FindFreePort ());
1520 sock.BeginAccept (acc, 256, BADCallback, sock);
1521 Assert.Fail ("BeginAcceptSocketUdp #1");
1522 } catch (SocketException ex) {
1523 Assert.AreEqual (10022, ex.ErrorCode, "BeginAcceptSocketUdp #2");
1531 #if FEATURE_NO_BSD_SOCKETS
1532 [ExpectedException (typeof (PlatformNotSupportedException))]
1534 public void BeginAcceptSocketBound ()
1536 Socket sock = new Socket (AddressFamily.InterNetwork,
1539 Socket acc = new Socket (AddressFamily.InterNetwork,
1543 IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
1544 NetworkHelpers.FindFreePort ());
1546 IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
1547 NetworkHelpers.FindFreePort ());
1555 sock.BeginAccept (acc, 256, BADCallback, sock);
1556 Assert.Fail ("BeginAcceptSocketBound #1");
1557 } catch (InvalidOperationException) {
1565 #if FEATURE_NO_BSD_SOCKETS
1566 [ExpectedException (typeof (PlatformNotSupportedException))]
1568 public void BeginAcceptSocket ()
1570 Socket sock = new Socket (AddressFamily.InterNetwork,
1573 Socket acc = new Socket (AddressFamily.InterNetwork,
1577 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1578 NetworkHelpers.FindFreePort ());
1583 BADCalledBack.Reset ();
1585 sock.BeginAccept (acc, 256, BADCallback, sock);
1587 Socket conn = new Socket (AddressFamily.InterNetwork,
1590 byte[] send_bytes = new byte[] {10, 11, 12, 13};
1593 conn.Send (send_bytes);
1595 if (BADCalledBack.WaitOne (2000, false) == false) {
1596 Assert.Fail ("BeginAcceptSocket wait timed out");
1599 Assert.AreEqual (true, BADAccepted, "BeginAcceptSocket #1");
1600 Assert.AreEqual (true, BADSocket.Connected, "BeginAcceptSocket #2");
1601 Assert.AreEqual (false, sock.Connected, "BeginAcceptSocket #3");
1602 Assert.AreEqual (true, conn.Connected, "BeginAcceptSocket #4");
1603 Assert.AreEqual (send_bytes.Length, BADByteCount, "BeginAcceptSocket #5");
1604 Assert.AreEqual (AddressFamily.InterNetwork, acc.AddressFamily, "BeginAcceptSocket #6");
1605 Assert.AreEqual (SocketType.Stream, acc.SocketType, "BeginAcceptSocket #7");
1606 Assert.AreEqual (ProtocolType.Tcp, acc.ProtocolType, "BeginAcceptSocket #8");
1607 Assert.AreEqual (conn.LocalEndPoint, acc.RemoteEndPoint, "BeginAcceptSocket #9");
1609 /* The MS runtime gives the returned data in a
1610 * much bigger array. TODO: investigate
1611 * whether it the size correlates to the first
1612 * parameter in BeginAccept()
1614 Assert.IsFalse (BADBytes.Length == send_bytes.Length,
1615 "BeginAcceptSocket #10");
1617 for(int i = 0; i < send_bytes.Length; i++) {
1618 Assert.AreEqual (send_bytes[i], BADBytes[i], "BeginAcceptSocket #" + (i+11).ToString ());
1628 public void BeginAcceptSocketClosed ()
1630 Socket sock = new Socket (AddressFamily.InterNetwork,
1633 Socket acc = new Socket (AddressFamily.InterNetwork,
1640 sock.BeginAccept (acc, 256, BADCallback, null);
1641 Assert.Fail ("BeginAcceptSocketClosed #1");
1642 } catch (ObjectDisposedException) {
1649 #if FEATURE_NO_BSD_SOCKETS
1650 [ExpectedException (typeof (PlatformNotSupportedException))]
1652 public void BeginAcceptSocketAccClosed ()
1654 Socket sock = new Socket (AddressFamily.InterNetwork,
1657 Socket acc = new Socket (AddressFamily.InterNetwork,
1660 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1661 NetworkHelpers.FindFreePort ());
1668 BADCalledBack.Reset ();
1671 sock.BeginAccept (acc, 256, BADCallback, null);
1672 Assert.Fail ("BeginAcceptSocketAccClosed #1");
1673 } catch (ObjectDisposedException) {
1679 static bool BCConnected = false;
1680 static ManualResetEvent BCCalledBack = new ManualResetEvent (false);
1682 private static void BCCallback (IAsyncResult asyncResult)
1684 Socket sock = (Socket)asyncResult.AsyncState;
1687 sock.EndConnect (asyncResult);
1688 } catch (Exception e) {
1689 Console.WriteLine ("BCCallback exception:");
1690 Console.WriteLine (e);
1697 BCCalledBack.Set ();
1701 #if FEATURE_NO_BSD_SOCKETS
1702 [ExpectedException (typeof (PlatformNotSupportedException))]
1704 public void BeginConnectAddressPort ()
1706 Socket sock = new Socket (AddressFamily.InterNetwork,
1709 Socket listen = new Socket (AddressFamily.InterNetwork,
1712 IPAddress ip = IPAddress.Loopback;
1713 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
1718 BCCalledBack.Reset ();
1720 BCConnected = false;
1722 sock.BeginConnect (ip, ep.Port, BCCallback, sock);
1724 if (BCCalledBack.WaitOne (2000, false) == false) {
1725 Assert.Fail ("BeginConnectAddressPort wait timed out");
1728 Assert.AreEqual (true, BCConnected, "BeginConnectAddressPort #1");
1735 public void BeginConnectAddressPortNull ()
1737 var port = NetworkHelpers.FindFreePort ();
1738 Socket sock = new Socket (AddressFamily.InterNetwork,
1741 IPAddress ip = null;
1744 sock.BeginConnect (ip, port, BCCallback,
1746 Assert.Fail ("BeginConnectAddressPortNull #1");
1747 } catch (ArgumentNullException) {
1754 #if FEATURE_NO_BSD_SOCKETS
1755 [ExpectedException (typeof (PlatformNotSupportedException))]
1757 public void BeginConnectAddressPortListen ()
1759 Socket sock = new Socket (AddressFamily.InterNetwork,
1762 IPAddress ip = IPAddress.Loopback;
1763 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
1769 sock.BeginConnect (ip, ep.Port, BCCallback, sock);
1770 Assert.Fail ("BeginConnectAddressPortListen #1");
1771 } catch (InvalidOperationException) {
1778 [ExpectedException (typeof(ObjectDisposedException))]
1779 public void BeginConnectAddressPortClosed ()
1781 var port = NetworkHelpers.FindFreePort ();
1782 Socket sock = new Socket (AddressFamily.InterNetwork,
1785 IPAddress ip = IPAddress.Loopback;
1789 sock.BeginConnect (ip, port, BCCallback, sock);
1793 [Category ("NotOnMac")]
1795 * This is not a Mono bug.
1797 * By default, only 127.0.0.1 is enabled and you must explicitly
1798 * enable additional addresses using 'sudo ifconfig lo0 alias 127.0.0.1'.
1800 * I tested this on Mac OS 10.7.4; a 'ping 127.0.0.2' does not work
1801 * until I add that alias.
1804 public void BeginConnectMultiple ()
1806 Socket sock = new Socket (AddressFamily.InterNetwork,
1809 Socket listen = new Socket (AddressFamily.InterNetwork,
1812 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1813 NetworkHelpers.FindFreePort ());
1814 IPAddress[] ips = new IPAddress[4];
1816 ips[0] = IPAddress.Parse ("127.0.0.4");
1817 ips[1] = IPAddress.Parse ("127.0.0.3");
1818 ips[2] = IPAddress.Parse ("127.0.0.2");
1819 ips[3] = IPAddress.Parse ("127.0.0.1");
1824 BCCalledBack.Reset ();
1826 BCConnected = false;
1828 sock.BeginConnect (ips, ep.Port, BCCallback, sock);
1830 /* Longer wait here, because the ms runtime
1831 * takes a lot longer to not connect
1834 if (BCCalledBack.WaitOne (30000, false) == false) {
1835 Assert.Fail ("BeginConnectMultiple wait failed");
1839 Assert.IsTrue (BCCalledBack.WaitOne (30000), "#0");
1841 Assert.AreEqual (true, BCConnected, "BeginConnectMultiple #1");
1842 Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "BeginConnectMultiple #2");
1843 IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
1845 Assert.AreEqual (IPAddress.Loopback, remep.Address, "BeginConnectMultiple #2");
1852 #if FEATURE_NO_BSD_SOCKETS
1853 [ExpectedException (typeof (PlatformNotSupportedException))]
1855 public void BeginConnectMultiple2 ()
1857 Socket sock = new Socket (AddressFamily.InterNetwork,
1860 Socket listen = new Socket (AddressFamily.InterNetwork,
1864 // Need at least two addresses.
1865 var ips = Dns.GetHostAddresses (string.Empty);
1867 Assert.Ignore ("This test needs at least two IP addresses.");
1869 var allIps = new IPAddress [ips.Length + 1];
1870 allIps [0] = IPAddress.Loopback;
1871 ips.CopyTo (allIps, 1);
1874 * Only bind to the loopback interface, so all the non-loopback
1875 * IP addresses will fail. BeginConnect()/EndConnect() should
1876 * succeed it it can connect to at least one of the requested
1879 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
1884 BCCalledBack.Reset ();
1886 BCConnected = false;
1888 sock.BeginConnect (allIps, ep.Port, BCCallback, sock);
1890 /* Longer wait here, because the ms runtime
1891 * takes a lot longer to not connect
1893 if (BCCalledBack.WaitOne (10000, false) == false) {
1894 Assert.Fail ("BeginConnectMultiple2 wait failed");
1897 Assert.AreEqual (true, BCConnected, "BeginConnectMultiple2 #1");
1898 Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "BeginConnectMultiple2 #2");
1899 IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
1901 Assert.AreEqual (IPAddress.Loopback, remep.Address, "BeginConnectMultiple2 #2");
1909 public void BeginConnectMultipleNull ()
1911 var port = NetworkHelpers.FindFreePort ();
1912 Socket sock = new Socket (AddressFamily.InterNetwork,
1915 IPAddress[] ips = null;
1918 sock.BeginConnect (ips, port, BCCallback,
1920 Assert.Fail ("BeginConnectMultipleNull #1");
1921 } catch (ArgumentNullException) {
1928 #if FEATURE_NO_BSD_SOCKETS
1929 [ExpectedException (typeof (PlatformNotSupportedException))]
1931 public void BeginConnectMultipleListen ()
1933 Socket sock = new Socket (AddressFamily.InterNetwork,
1936 IPAddress[] ips = new IPAddress[4];
1937 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
1938 NetworkHelpers.FindFreePort ());
1940 ips[0] = IPAddress.Parse ("127.0.0.4");
1941 ips[1] = IPAddress.Parse ("127.0.0.3");
1942 ips[2] = IPAddress.Parse ("127.0.0.2");
1943 ips[3] = IPAddress.Parse ("127.0.0.1");
1949 sock.BeginConnect (ips, ep.Port, BCCallback,
1951 Assert.Fail ("BeginConnectMultipleListen #1");
1952 } catch (InvalidOperationException) {
1959 [ExpectedException (typeof(ObjectDisposedException))]
1960 public void BeginConnectMultipleClosed ()
1962 var port = NetworkHelpers.FindFreePort ();
1963 Socket sock = new Socket (AddressFamily.InterNetwork,
1966 IPAddress[] ips = new IPAddress[4];
1968 ips[0] = IPAddress.Parse ("127.0.0.4");
1969 ips[1] = IPAddress.Parse ("127.0.0.3");
1970 ips[2] = IPAddress.Parse ("127.0.0.2");
1971 ips[3] = IPAddress.Parse ("127.0.0.1");
1975 sock.BeginConnect (ips, port, BCCallback, sock);
1979 public void BeginConnectHostPortNull ()
1981 Socket sock = new Socket (AddressFamily.InterNetwork,
1986 sock.BeginConnect ((string)null, 0,
1988 Assert.Fail ("BeginConnectHostPort #1");
1989 } catch (ArgumentNullException) {
1996 #if FEATURE_NO_BSD_SOCKETS
1997 [ExpectedException (typeof (PlatformNotSupportedException))]
1999 public void BeginConnectHostPortListen ()
2001 Socket sock = new Socket (AddressFamily.InterNetwork,
2004 IPAddress ip = IPAddress.Loopback;
2005 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
2011 sock.BeginConnect ("localhost", ep.Port,
2013 Assert.Fail ("BeginConnectHostPortListen #1");
2014 } catch (InvalidOperationException) {
2021 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
2022 public void BeginConnectHostPortNotIP ()
2024 Socket sock = new Socket (AddressFamily.NetBios,
2025 SocketType.Seqpacket,
2026 ProtocolType.Unspecified);
2029 sock.BeginConnect ("localhost", 0, BCCallback,
2031 Assert.Fail ("BeginConnectHostPortNotIP #1");
2032 } catch (NotSupportedException) {
2039 [ExpectedException (typeof(ObjectDisposedException))]
2040 public void BeginConnectHostPortClosed ()
2042 Socket sock = new Socket (AddressFamily.InterNetwork,
2048 sock.BeginConnect ("localhost", 0, BCCallback, sock);
2051 static bool BDDisconnected = false;
2052 static ManualResetEvent BDCalledBack = new ManualResetEvent (false);
2054 private static void BDCallback (IAsyncResult asyncResult)
2056 Socket sock = (Socket)asyncResult.AsyncState;
2058 sock.EndDisconnect (asyncResult);
2059 BDDisconnected = true;
2061 BDCalledBack.Set ();
2065 [Category ("NotDotNet")] // "Needs XP or later"
2066 #if FEATURE_NO_BSD_SOCKETS
2067 [ExpectedException (typeof (PlatformNotSupportedException))]
2069 public void BeginDisconnect ()
2071 Socket sock = new Socket (AddressFamily.InterNetwork,
2074 Socket listen = new Socket (AddressFamily.InterNetwork,
2077 IPAddress ip = IPAddress.Loopback;
2078 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
2083 sock.Connect (ip, ep.Port);
2085 Assert.AreEqual (true, sock.Connected, "BeginDisconnect #1");
2087 sock.Shutdown (SocketShutdown.Both);
2089 BDCalledBack.Reset ();
2090 BDDisconnected = false;
2092 sock.BeginDisconnect (false, BDCallback, sock);
2094 if (BDCalledBack.WaitOne (2000, false) == false) {
2095 Assert.Fail ("BeginDisconnect wait timed out");
2098 Assert.AreEqual (true, BDDisconnected, "BeginDisconnect #2");
2099 Assert.AreEqual (false, sock.Connected, "BeginDisconnect #3");
2106 public void BeginReceiveSocketError ()
2111 public void BeginReceiveGeneric ()
2116 public void BeginReceiveGenericSocketError ()
2120 private static void BSCallback (IAsyncResult asyncResult)
2122 Socket sock = (Socket)asyncResult.AsyncState;
2124 sock.EndSend (asyncResult);
2128 public void BeginSendNotConnected ()
2130 Socket sock = new Socket (AddressFamily.InterNetwork,
2134 byte[] send_bytes = new byte[] {10, 11, 12, 13};
2137 sock.BeginSend (send_bytes, 0,
2139 SocketFlags.None, BSCallback,
2141 Assert.Fail ("BeginSendNotConnected #1");
2142 } catch (SocketException ex) {
2143 Assert.AreEqual (10057, ex.ErrorCode, "BeginSendNotConnected #2");
2150 public void BeginSendSocketError ()
2155 public void BeginSendGeneric ()
2160 public void BeginSendGenericSocketError ()
2165 #if FEATURE_NO_BSD_SOCKETS
2166 [ExpectedException (typeof (PlatformNotSupportedException))]
2168 public void BindTwice ()
2170 Socket sock = new Socket (AddressFamily.InterNetwork,
2173 IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
2174 NetworkHelpers.FindFreePort ());
2175 IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
2176 NetworkHelpers.FindFreePort ());
2182 Assert.Fail ("BindTwice #1");
2183 } catch (SocketException ex) {
2184 Assert.AreEqual (10022, ex.ErrorCode, "BindTwice #2");
2191 #if FEATURE_NO_BSD_SOCKETS
2192 [ExpectedException (typeof (PlatformNotSupportedException))]
2194 public void Close ()
2196 Socket sock = new Socket (AddressFamily.InterNetwork,
2199 Socket listen = new Socket (AddressFamily.InterNetwork,
2202 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2203 NetworkHelpers.FindFreePort ());
2210 Assert.AreEqual (true, sock.Connected, "Close #1");
2214 Thread.Sleep (3000);
2216 Assert.AreEqual (false, sock.Connected, "Close #2");
2222 #if FEATURE_NO_BSD_SOCKETS
2223 [ExpectedException (typeof (PlatformNotSupportedException))]
2225 public void ConnectAddressPort ()
2227 Socket sock = new Socket (AddressFamily.InterNetwork,
2230 Socket listen = new Socket (AddressFamily.InterNetwork,
2233 IPAddress ip = IPAddress.Loopback;
2234 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
2239 sock.Connect (ip, ep.Port);
2241 Assert.AreEqual (true, sock.Connected, "ConnectAddressPort #1");
2248 public void ConnectAddressPortNull ()
2250 var port = NetworkHelpers.FindFreePort ();
2251 Socket sock = new Socket (AddressFamily.InterNetwork,
2254 IPAddress ip = null;
2257 sock.Connect (ip, port);
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 var port = NetworkHelpers.FindFreePort ();
2294 Socket sock = new Socket (AddressFamily.InterNetwork,
2297 IPAddress ip = IPAddress.Loopback;
2301 sock.Connect (ip, port);
2305 [Category ("NotOnMac")] // MacOSX trashes the fd after the failed connect attempt to 127.0.0.4
2307 * This is not a Mono bug.
2309 * By default, only 127.0.0.1 is enabled and you must explicitly
2310 * enable additional addresses using 'sudo ifconfig lo0 alias 127.0.0.1'.
2312 * I tested this on Mac OS 10.7.4; a 'ping 127.0.0.2' does not work
2313 * until I add that alias.
2315 * However, after doing so, Mac OS treats these as separate addresses, ie. attempting
2316 * to connect to 127.0.0.4 yields a connection refused.
2318 * When using Connect(), the .NET runtime also throws an exception if connecting to
2319 * any of the IP addresses fails. This is different with BeginConnect()/EndConnect()
2320 * which succeeds when it can connect to at least one of the addresses.
2323 public void ConnectMultiple ()
2325 Socket sock = new Socket (AddressFamily.InterNetwork,
2328 Socket listen = new Socket (AddressFamily.InterNetwork,
2331 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2332 NetworkHelpers.FindFreePort ());
2333 IPAddress[] ips = new IPAddress[4];
2335 ips[0] = IPAddress.Parse ("127.0.0.4");
2336 ips[1] = IPAddress.Parse ("127.0.0.3");
2337 ips[2] = IPAddress.Parse ("127.0.0.2");
2338 ips[3] = IPAddress.Parse ("127.0.0.1");
2343 sock.Connect (ips, ep.Port);
2345 Assert.AreEqual (true, sock.Connected, "ConnectMultiple #1");
2346 Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "ConnectMultiple #2");
2347 IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
2349 Assert.AreEqual (IPAddress.Loopback, remep.Address, "ConnectMultiple #2");
2356 #if FEATURE_NO_BSD_SOCKETS
2357 [ExpectedException (typeof (PlatformNotSupportedException))]
2359 public void ConnectMultiple2 ()
2361 Socket sock = new Socket (AddressFamily.InterNetwork,
2364 Socket listen = new Socket (AddressFamily.InterNetwork,
2368 // Need at least two addresses.
2369 var ips = Dns.GetHostAddresses (string.Empty);
2371 Assert.Ignore ("This test needs at least two IP addresses.");
2373 var allIps = new IPAddress [ips.Length + 1];
2374 allIps [0] = IPAddress.Loopback;
2375 ips.CopyTo (allIps, 1);
2378 * Bind to IPAddress.Any; Connect() will fail unless it can
2379 * connect to all the addresses in allIps.
2381 IPEndPoint ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
2386 sock.Connect (allIps, ep.Port);
2388 Assert.AreEqual (true, sock.Connected, "ConnectMultiple2 #1");
2389 Assert.AreEqual (AddressFamily.InterNetwork, sock.RemoteEndPoint.AddressFamily, "ConnectMultiple2 #2");
2390 IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
2392 Assert.AreEqual (IPAddress.Loopback, remep.Address, "ConnectMultiple2 #3");
2399 public void ConnectMultipleNull ()
2401 var port = NetworkHelpers.FindFreePort ();
2402 Socket sock = new Socket (AddressFamily.InterNetwork,
2405 IPAddress[] ips = null;
2408 sock.Connect (ips, port);
2409 Assert.Fail ("ConnectMultipleNull #1");
2410 } catch (ArgumentNullException) {
2417 #if FEATURE_NO_BSD_SOCKETS
2418 [ExpectedException (typeof (PlatformNotSupportedException))]
2420 public void ConnectMultipleListen ()
2422 Socket sock = new Socket (AddressFamily.InterNetwork,
2425 IPAddress[] ips = new IPAddress[4];
2426 IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
2427 NetworkHelpers.FindFreePort ());
2429 ips[0] = IPAddress.Parse ("127.0.0.4");
2430 ips[1] = IPAddress.Parse ("127.0.0.3");
2431 ips[2] = IPAddress.Parse ("127.0.0.2");
2432 ips[3] = IPAddress.Parse ("127.0.0.1");
2438 sock.Connect (ips, ep.Port);
2439 Assert.Fail ("ConnectMultipleListen #1");
2440 } catch (InvalidOperationException) {
2447 [ExpectedException (typeof(ObjectDisposedException))]
2448 public void ConnectMultipleClosed ()
2450 var port = NetworkHelpers.FindFreePort ();
2451 Socket sock = new Socket (AddressFamily.InterNetwork,
2454 IPAddress[] ips = new IPAddress[4];
2456 ips[0] = IPAddress.Parse ("127.0.0.4");
2457 ips[1] = IPAddress.Parse ("127.0.0.3");
2458 ips[2] = IPAddress.Parse ("127.0.0.2");
2459 ips[3] = IPAddress.Parse ("127.0.0.1");
2463 sock.Connect (ips, port);
2467 public void ConnectHostPortNull ()
2469 Socket sock = new Socket (AddressFamily.InterNetwork,
2474 sock.Connect ((string)null, 0);
2475 Assert.Fail ("ConnectHostPort #1");
2476 } catch (ArgumentNullException) {
2483 #if FEATURE_NO_BSD_SOCKETS
2484 [ExpectedException (typeof (PlatformNotSupportedException))]
2486 public void ConnectHostPortListen ()
2488 Socket sock = new Socket (AddressFamily.InterNetwork,
2491 IPAddress ip = IPAddress.Loopback;
2492 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
2498 sock.Connect ("localhost", ep.Port);
2499 Assert.Fail ("ConnectHostPortListen #1");
2500 } catch (InvalidOperationException) {
2507 [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
2508 public void ConnectHostPortNotIP ()
2510 Socket sock = new Socket (AddressFamily.NetBios,
2511 SocketType.Seqpacket,
2512 ProtocolType.Unspecified);
2515 sock.Connect ("localhost", 0);
2516 Assert.Fail ("ConnectHostPortNotIP #1");
2517 } catch (NotSupportedException) {
2524 #if FEATURE_NO_BSD_SOCKETS
2525 [ExpectedException (typeof (PlatformNotSupportedException))]
2527 [ExpectedException (typeof(ObjectDisposedException))]
2529 public void ConnectHostPortClosed ()
2531 Socket sock = new Socket (AddressFamily.InterNetwork,
2537 sock.Connect ("localhost", 0);
2541 [Category ("NotDotNet")] // "Needs XP or later"
2542 #if FEATURE_NO_BSD_SOCKETS
2543 [ExpectedException (typeof (PlatformNotSupportedException))]
2545 public void Disconnect ()
2547 Socket sock = new Socket (AddressFamily.InterNetwork,
2550 Socket listen = new Socket (AddressFamily.InterNetwork,
2553 IPAddress ip = IPAddress.Loopback;
2554 IPEndPoint ep = new IPEndPoint (ip, NetworkHelpers.FindFreePort ());
2559 sock.Connect (ip, ep.Port);
2561 Assert.AreEqual (true, sock.Connected, "Disconnect #1");
2563 sock.Shutdown (SocketShutdown.Both);
2565 sock.Disconnect (false);
2567 Assert.AreEqual (false, sock.Connected, "BeginDisconnect #3");
2574 public void DuplicateAndClose ()
2579 public void IOControl ()
2584 public void TestDefaultsDualMode ()
2586 using (var socket = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)){
2587 Assert.IsTrue (socket.DualMode, "In Mono, DualMode must be true when constructing InterNetworkV6 sockets");
2590 using (var socket = new Socket (SocketType.Stream, ProtocolType.Tcp)){
2591 Assert.AreEqual (AddressFamily.InterNetworkV6, socket.AddressFamily, "When creating sockets of type stream/tcp, the address family should be InterNetworkV6");
2592 Assert.IsTrue (socket.DualMode, "In Mono, DualMode must be true when constructing InterNetworkV6 sockets");
2594 socket.DualMode = false;
2596 Assert.IsFalse (socket.DualMode, "Setting of DualSocket should turn DualSockets off");
2602 #if FEATURE_NO_BSD_SOCKETS
2603 [ExpectedException (typeof (PlatformNotSupportedException))]
2605 public void ReceiveGeneric ()
2609 IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, NetworkHelpers.FindFreePort ());
2611 Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2612 listensock.Bind (endpoint);
2613 listensock.Listen(1);
2615 Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2616 sendsock.Connect(endpoint);
2618 Socket clientsock = listensock.Accept();
2620 byte[] sendbuf = new byte[256];
2622 for(i = 0; i < 256; i++) {
2623 sendbuf[i] = (byte)i;
2625 for (i = 4; i < 6; i++) {
2626 Assert.AreEqual (sendbuf[i], (byte)i,
2627 "#1/" + i.ToString());
2631 sendsock.Send (sendbuf, 0, 256, SocketFlags.None,
2635 byte[] recvbuf = new byte[256];
2636 List<ArraySegment<byte>> recvbuflist = new List<ArraySegment<byte>>(2);
2637 recvbuflist.Add(new ArraySegment<byte>(recvbuf, 4, 2));
2638 recvbuflist.Add(new ArraySegment<byte>(recvbuf, 20, 230));
2640 clientsock.Receive (recvbuflist);
2642 /* recvbuf should now hold the first 2 bytes
2643 * of sendbuf from pos 4, and the next 230
2644 * bytes of sendbuf from pos 20
2647 for (i = 0; i < 2; i++) {
2648 Assert.AreEqual (sendbuf[i], recvbuf[i + 4],
2649 "#2/" + i.ToString());
2651 for (i = 2; i < 232; i++) {
2652 Assert.AreEqual (sendbuf[i], recvbuf[i + 18],
2653 "#2/" + i.ToString());
2657 clientsock.Close ();
2658 listensock.Close ();
2662 #if FEATURE_NO_BSD_SOCKETS
2663 [ExpectedException (typeof (PlatformNotSupportedException))]
2665 public void SendGeneric ()
2669 IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, NetworkHelpers.FindFreePort ());
2671 Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2672 listensock.Bind (endpoint);
2673 listensock.Listen(1);
2675 Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2676 sendsock.Connect(endpoint);
2678 Socket clientsock = listensock.Accept();
2680 byte[] sendbuf = new byte[256];
2681 List<ArraySegment<byte>> sendbuflist = new List<ArraySegment<byte>>(2);
2683 sendbuflist.Add(new ArraySegment<byte>(sendbuf, 4, 2));
2684 sendbuflist.Add(new ArraySegment<byte>(sendbuf, 20, 230));
2686 for(i = 0; i < 256; i++) {
2687 sendbuf[i] = (byte)i;
2689 for (i = 4; i < 6; i++) {
2690 Assert.AreEqual (sendbuf[i], (byte)i,
2691 "#1/" + i.ToString());
2695 sendsock.Send (sendbuflist, SocketFlags.None, out err);
2698 byte[] recvbuf = new byte[256];
2700 clientsock.Receive (recvbuf);
2702 /* The first 2 bytes of recvbuf should now
2703 * hold 2 bytes of sendbuf from pos 4, and the
2704 * next 230 bytes of recvbuf should be sendbuf
2708 for (i = 0; i < 2; i++) {
2709 Assert.AreEqual (recvbuf[i], sendbuf[i + 4],
2710 "#2/" + i.ToString());
2712 for (i = 2; i < 232; i++) {
2713 Assert.AreEqual (recvbuf[i], sendbuf[i + 18],
2714 "#2/" + i.ToString());
2718 clientsock.Close ();
2719 listensock.Close ();
2723 #if FEATURE_NO_BSD_SOCKETS
2724 [ExpectedException (typeof (AggregateException))] // Something catches the PlatformNotSupportedException and re-throws an AggregateException
2726 public void ConcurrentExceedSocketLimit ()
2728 var tasks = new Task[4];
2729 for (int i = 0; i < 4; i++) {
2730 tasks[i] = Task.Factory.StartNew (() => SendGenericExceedBuffer ());
2732 Task.WaitAll (tasks);
2736 #if FEATURE_NO_BSD_SOCKETS
2737 [ExpectedException (typeof (PlatformNotSupportedException))]
2739 public void SendGenericExceedBuffer ()
2741 // Create a buffer larger than the default max.
2742 const int BUFFER_SIZE = 65 * 1024;
2745 IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, NetworkHelpers.FindFreePort ());
2747 Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2748 listensock.Bind (endpoint);
2749 listensock.Listen (1);
2751 Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
2752 sendsock.Connect (endpoint);
2754 Socket clientsock = listensock.Accept ();
2756 byte[] sendbuf = new byte[BUFFER_SIZE];
2758 for (i = 0; i < BUFFER_SIZE; i++) {
2759 sendbuf[i] = (byte)i;
2762 Task sendTask = Task.Factory.StartNew(() => {
2763 int sent = sendsock.Send (sendbuf);
2765 Assert.AreEqual (BUFFER_SIZE, sent, "#1");
2768 byte[] recvbuf = new byte[BUFFER_SIZE];
2770 Task recvTask = Task.Factory.StartNew(() => {
2771 int totalReceived = 0;
2772 byte[] buffer = new byte[256];
2773 while (totalReceived < sendbuf.Length) {
2774 int recvd = clientsock.Receive (buffer, 0, buffer.Length, SocketFlags.None);
2775 Array.Copy (buffer, 0, recvbuf, totalReceived, recvd);
2776 totalReceived += recvd;
2779 Assert.AreEqual (BUFFER_SIZE, totalReceived, "#2");
2782 Assert.IsTrue (Task.WaitAll (new []{sendTask, recvTask}, 20 * 1000), "#2a");
2784 for (i = 0; i < BUFFER_SIZE; i++) {
2785 Assert.AreEqual (recvbuf[i], sendbuf[i],
2786 "#3/" + i.ToString());
2790 clientsock.Close ();
2791 listensock.Close ();
2795 public void ListenNotBound ()
2797 Socket sock = new Socket (AddressFamily.InterNetwork,
2803 Assert.Fail ("ListenNotBound #1");
2804 } catch (SocketException ex) {
2805 Assert.AreEqual (10022, ex.ErrorCode, "ListenNotBound #2");
2811 static Socket CWRSocket;
2812 static bool CWRReceiving = true;
2813 static ManualResetEvent CWRReady = new ManualResetEvent (false);
2815 private static void CWRReceiveThread ()
2817 byte[] buf = new byte[256];
2820 CWRSocket.Receive (buf);
2821 } catch (SocketException) {
2822 CWRReceiving = false;
2829 #if FEATURE_NO_BSD_SOCKETS
2830 [ExpectedException (typeof (PlatformNotSupportedException))]
2832 public void CloseWhileReceiving ()
2834 CWRSocket = new Socket (AddressFamily.InterNetwork,
2837 CWRSocket.Bind (new IPEndPoint (IPAddress.Loopback,
2838 NetworkHelpers.FindFreePort ()));
2840 Thread recv_thread = new Thread (new ThreadStart (CWRReceiveThread));
2842 recv_thread.Start ();
2843 Thread.Sleep (250); /* Wait for the thread to be already receiving */
2846 if (CWRReady.WaitOne (1000, false) == false) {
2847 Assert.Fail ("CloseWhileReceiving wait timed out");
2850 Assert.IsFalse (CWRReceiving);
2853 static bool RRCLastRead = false;
2854 static ManualResetEvent RRCReady = new ManualResetEvent (false);
2856 private static void RRCClientThread (int port)
2858 byte[] bytes = new byte[8];
2861 Socket sock = new Socket (AddressFamily.InterNetwork,
2864 sock.Connect (new IPEndPoint (IPAddress.Loopback,
2867 NetworkStream stream = new NetworkStream (sock);
2869 readbyte = stream.ReadByte ();
2870 Assert.AreEqual (0, readbyte, "ReceiveRemoteClosed #1");
2872 stream.Read (bytes, 0, 0);
2874 readbyte = stream.ReadByte ();
2875 Assert.AreEqual (0, readbyte, "ReceiveRemoteClosed #2");
2877 stream.Read (bytes, 0, 0);
2879 readbyte = stream.ReadByte ();
2880 Assert.AreEqual (-1, readbyte, "ReceiveRemoteClosed #3");
2888 [Test] // Receive (Byte [])
2889 public void Receive1_Buffer_Null ()
2891 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2895 s.Receive ((byte []) null);
2897 } catch (ArgumentNullException ex) {
2898 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2899 Assert.IsNull (ex.InnerException, "#3");
2900 Assert.IsNotNull (ex.Message, "#4");
2901 Assert.AreEqual ("buffer", ex.ParamName, "#5");
2907 [Test] // Receive (Byte [])
2908 public void Receive1_Socket_Closed ()
2910 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2915 s.Receive ((byte []) null);
2917 } catch (ObjectDisposedException ex) {
2918 // Cannot access a disposed object
2919 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2920 Assert.IsNull (ex.InnerException, "#3");
2921 Assert.IsNotNull (ex.Message, "#4");
2922 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2926 [Test] // Receive (Byte [], SocketFlags)
2927 public void Receive2_Buffer_Null ()
2929 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2933 s.Receive ((byte []) null, (SocketFlags) 666);
2935 } catch (ArgumentNullException ex) {
2936 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2937 Assert.IsNull (ex.InnerException, "#3");
2938 Assert.IsNotNull (ex.Message, "#4");
2939 Assert.AreEqual ("buffer", ex.ParamName, "#5");
2945 [Test] // Receive (Byte [], SocketFlags)
2946 public void Receive2_Socket_Closed ()
2948 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2953 s.Receive ((byte []) null, (SocketFlags) 666);
2955 } catch (ObjectDisposedException ex) {
2956 // Cannot access a disposed object
2957 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2958 Assert.IsNull (ex.InnerException, "#3");
2959 Assert.IsNotNull (ex.Message, "#4");
2960 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
2964 [Test] // Receive (Byte [], Int32, SocketFlags)
2965 public void Receive3_Buffer_Null ()
2967 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2971 s.Receive ((byte []) null, 0, (SocketFlags) 666);
2973 } catch (ArgumentNullException ex) {
2974 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
2975 Assert.IsNull (ex.InnerException, "#3");
2976 Assert.IsNotNull (ex.Message, "#4");
2977 Assert.AreEqual ("buffer", ex.ParamName, "#5");
2983 [Test] // Receive (Byte [], Int32, SocketFlags)
2984 public void Receive3_Socket_Closed ()
2986 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
2991 s.Receive ((byte []) null, 0, (SocketFlags) 666);
2993 } catch (ObjectDisposedException ex) {
2994 // Cannot access a disposed object
2995 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
2996 Assert.IsNull (ex.InnerException, "#3");
2997 Assert.IsNotNull (ex.Message, "#4");
2998 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3002 [Test] // Receive (Byte [], Int32, Int32, SocketFlags)
3003 public void Receive4_Buffer_Null ()
3005 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3009 s.Receive ((byte []) null, 0, 0, (SocketFlags) 666);
3011 } catch (ArgumentNullException ex) {
3012 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3013 Assert.IsNull (ex.InnerException, "#3");
3014 Assert.IsNotNull (ex.Message, "#4");
3015 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3021 [Test] // Receive (Byte [], Int32, Int32, SocketFlags)
3022 public void Receive4_Socket_Closed ()
3024 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3029 s.Receive ((byte []) null, 0, 0, (SocketFlags) 666);
3031 } catch (ObjectDisposedException ex) {
3032 // Cannot access a disposed object
3033 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3034 Assert.IsNull (ex.InnerException, "#3");
3035 Assert.IsNotNull (ex.Message, "#4");
3036 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3040 [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError)
3041 public void Receive5_Buffer_Null ()
3043 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3048 s.Receive ((byte []) null, 0, 0, SocketFlags.None, out error);
3050 } catch (ArgumentNullException ex) {
3051 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3052 Assert.IsNull (ex.InnerException, "#3");
3053 Assert.IsNotNull (ex.Message, "#4");
3054 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3060 [Test] // Receive (Byte [], Int32, Int32, SocketFlags, out SocketError)
3061 public void Receive5_Socket_Closed ()
3063 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3069 s.Receive ((byte []) null, 0, 0, SocketFlags.None, out error);
3071 } catch (ObjectDisposedException ex) {
3072 // Cannot access a disposed object
3073 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3074 Assert.IsNull (ex.InnerException, "#3");
3075 Assert.IsNotNull (ex.Message, "#4");
3076 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3080 [Test] // Receive (IList<ArraySegment<Byte>>)
3081 public void Receive6_Buffers_Null ()
3083 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3087 s.Receive ((IList<ArraySegment<byte>>) null);
3089 } catch (ArgumentNullException ex) {
3090 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3091 Assert.IsNull (ex.InnerException, "#3");
3092 Assert.IsNotNull (ex.Message, "#4");
3093 Assert.AreEqual ("buffers", ex.ParamName, "#5");
3099 [Test] // Receive (IList<ArraySegment<Byte>>)
3100 public void Receive6_Socket_Closed ()
3102 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3107 s.Receive ((IList<ArraySegment<byte>>) null);
3109 } catch (ObjectDisposedException ex) {
3110 // Cannot access a disposed object
3111 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3112 Assert.IsNull (ex.InnerException, "#3");
3113 Assert.IsNotNull (ex.Message, "#4");
3114 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3118 [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags)
3119 public void Receive7_Buffers_Null ()
3121 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3125 s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666);
3127 } catch (ArgumentNullException ex) {
3128 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3129 Assert.IsNull (ex.InnerException, "#3");
3130 Assert.IsNotNull (ex.Message, "#4");
3131 Assert.AreEqual ("buffers", ex.ParamName, "#5");
3137 [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags)
3138 public void Receive7_Socket_Closed ()
3140 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3145 s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666);
3147 } catch (ObjectDisposedException ex) {
3148 // Cannot access a disposed object
3149 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3150 Assert.IsNull (ex.InnerException, "#3");
3151 Assert.IsNotNull (ex.Message, "#4");
3152 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3156 [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags, out SocketError)
3157 public void Receive8_Buffers_Null ()
3159 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3164 s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666,
3167 } catch (ArgumentNullException ex) {
3168 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3169 Assert.IsNull (ex.InnerException, "#3");
3170 Assert.IsNotNull (ex.Message, "#4");
3171 Assert.AreEqual ("buffers", ex.ParamName, "#5");
3177 [Test] // Receive (IList<ArraySegment<Byte>>, SocketFlags, out SocketError)
3178 public void Receive8_Socket_Closed ()
3180 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3186 s.Receive ((IList<ArraySegment<byte>>) null, (SocketFlags) 666,
3189 } catch (ObjectDisposedException ex) {
3190 // Cannot access a disposed object
3191 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3192 Assert.IsNull (ex.InnerException, "#3");
3193 Assert.IsNotNull (ex.Message, "#4");
3194 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3200 [Test] // ReceiveFrom (Byte [], ref EndPoint)
3201 #if FEATURE_NO_BSD_SOCKETS
3202 [ExpectedException (typeof (PlatformNotSupportedException))]
3204 public void ReceiveFrom1_Buffer_Null ()
3206 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3209 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3211 s.ReceiveFrom ((Byte []) null, ref remoteEP);
3213 } catch (ArgumentNullException ex) {
3214 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3215 Assert.IsNull (ex.InnerException, "#3");
3216 Assert.IsNotNull (ex.Message, "#4");
3217 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3223 [Test] // ReceiveFrom (Byte [], ref EndPoint)
3224 public void ReceiveFrom1_RemoteEP_Null ()
3226 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3229 byte [] buffer = new byte [0];
3230 EndPoint remoteEP = null;
3232 s.ReceiveFrom (buffer, ref remoteEP);
3234 } catch (ArgumentNullException ex) {
3235 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3236 Assert.IsNull (ex.InnerException, "#3");
3237 Assert.IsNotNull (ex.Message, "#4");
3238 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
3244 [Test] // ReceiveFrom (Byte [], ref EndPoint)
3245 #if FEATURE_NO_BSD_SOCKETS
3246 [ExpectedException (typeof (PlatformNotSupportedException))]
3248 public void ReceiveFrom1_Socket_Closed ()
3250 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3254 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3256 s.ReceiveFrom ((Byte []) null, ref remoteEP);
3258 } catch (ObjectDisposedException ex) {
3259 // Cannot access a disposed object
3260 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3261 Assert.IsNull (ex.InnerException, "#3");
3262 Assert.IsNotNull (ex.Message, "#4");
3263 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3267 [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
3268 #if FEATURE_NO_BSD_SOCKETS
3269 [ExpectedException (typeof (PlatformNotSupportedException))]
3271 public void ReceiveFrom2_Buffer_Null ()
3273 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3276 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3278 s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP);
3280 } catch (ArgumentNullException ex) {
3281 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3282 Assert.IsNull (ex.InnerException, "#3");
3283 Assert.IsNotNull (ex.Message, "#4");
3284 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3290 [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
3291 public void ReceiveFrom2_RemoteEP_Null ()
3293 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3296 byte [] buffer = new byte [5];
3297 EndPoint remoteEP = null;
3299 s.ReceiveFrom (buffer, (SocketFlags) 666, ref remoteEP);
3301 } catch (ArgumentNullException ex) {
3302 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3303 Assert.IsNull (ex.InnerException, "#3");
3304 Assert.IsNotNull (ex.Message, "#4");
3305 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
3311 [Test] // ReceiveFrom (Byte [], SocketFlags, ref EndPoint)
3312 #if FEATURE_NO_BSD_SOCKETS
3313 [ExpectedException (typeof (PlatformNotSupportedException))]
3315 public void ReceiveFrom2_Socket_Closed ()
3317 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3321 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3323 s.ReceiveFrom ((Byte []) null, (SocketFlags) 666, ref remoteEP);
3325 } catch (ObjectDisposedException ex) {
3326 // Cannot access a disposed object
3327 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3328 Assert.IsNull (ex.InnerException, "#3");
3329 Assert.IsNotNull (ex.Message, "#4");
3330 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3334 [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
3335 #if FEATURE_NO_BSD_SOCKETS
3336 [ExpectedException (typeof (PlatformNotSupportedException))]
3338 public void ReceiveFrom3_Buffer_Null ()
3340 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3343 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3345 s.ReceiveFrom ((Byte []) null, 0, (SocketFlags) 666,
3348 } catch (ArgumentNullException ex) {
3349 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3350 Assert.IsNull (ex.InnerException, "#3");
3351 Assert.IsNotNull (ex.Message, "#4");
3352 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3358 [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
3359 public void ReceiveFrom3_RemoteEP_Null ()
3361 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3364 byte [] buffer = new byte [5];
3365 EndPoint remoteEP = null;
3367 s.ReceiveFrom (buffer, buffer.Length, (SocketFlags) 666, ref remoteEP);
3369 } catch (ArgumentNullException ex) {
3370 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3371 Assert.IsNull (ex.InnerException, "#3");
3372 Assert.IsNotNull (ex.Message, "#4");
3373 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
3379 [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
3380 #if FEATURE_NO_BSD_SOCKETS
3381 [ExpectedException (typeof (PlatformNotSupportedException))]
3383 public void ReceiveFrom3_Size_OutOfRange ()
3386 byte [] buffer = new byte [5];
3387 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3390 s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3393 s.ReceiveFrom (buffer, -1, (SocketFlags) 666, ref remoteEP);
3394 Assert.Fail ("#A1");
3395 } catch (ArgumentOutOfRangeException ex) {
3396 // Specified argument was out of the range of valid values
3397 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
3398 Assert.IsNull (ex.InnerException, "#A3");
3399 Assert.IsNotNull (ex.Message, "#A4");
3400 Assert.AreEqual ("size", ex.ParamName, "#A5");
3405 // size > buffer length
3406 s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3409 s.ReceiveFrom (buffer, (buffer.Length + 1), (SocketFlags) 666,
3411 Assert.Fail ("#B1");
3412 } catch (ArgumentOutOfRangeException ex) {
3413 // Specified argument was out of the range of valid values
3414 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
3415 Assert.IsNull (ex.InnerException, "#B3");
3416 Assert.IsNotNull (ex.Message, "#B4");
3417 Assert.AreEqual ("size", ex.ParamName, "#B5");
3423 [Test] // ReceiveFrom (Byte [], Int32, SocketFlags, ref EndPoint)
3424 #if FEATURE_NO_BSD_SOCKETS
3425 [ExpectedException (typeof (PlatformNotSupportedException))]
3427 public void ReceiveFrom3_Socket_Closed ()
3429 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3433 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3435 s.ReceiveFrom ((Byte []) null, -1, (SocketFlags) 666,
3438 } catch (ObjectDisposedException ex) {
3439 // Cannot access a disposed object
3440 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3441 Assert.IsNull (ex.InnerException, "#3");
3442 Assert.IsNotNull (ex.Message, "#4");
3443 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3447 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
3448 #if FEATURE_NO_BSD_SOCKETS
3449 [ExpectedException (typeof (PlatformNotSupportedException))]
3451 public void ReceiveFrom4_Buffer_Null ()
3453 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3455 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3458 s.ReceiveFrom ((Byte []) null, -1, -1, (SocketFlags) 666,
3461 } catch (ArgumentNullException ex) {
3462 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3463 Assert.IsNull (ex.InnerException, "#3");
3464 Assert.IsNotNull (ex.Message, "#4");
3465 Assert.AreEqual ("buffer", ex.ParamName, "#5");
3469 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
3470 #if FEATURE_NO_BSD_SOCKETS
3471 [ExpectedException (typeof (PlatformNotSupportedException))]
3473 public void ReceiveFrom4_Offset_OutOfRange ()
3476 byte [] buffer = new byte [5];
3477 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3480 s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3483 s.ReceiveFrom (buffer, -1, 0, (SocketFlags) 666,
3485 Assert.Fail ("#A1");
3486 } catch (ArgumentOutOfRangeException ex) {
3487 // Specified argument was out of the range of valid values
3488 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
3489 Assert.IsNull (ex.InnerException, "#A3");
3490 Assert.IsNotNull (ex.Message, "#A4");
3491 Assert.AreEqual ("offset", ex.ParamName, "#A5");
3496 // offset > buffer length
3497 s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3500 s.ReceiveFrom (buffer, (buffer.Length + 1), 0, (SocketFlags) 666,
3502 Assert.Fail ("#B1");
3503 } catch (ArgumentOutOfRangeException ex) {
3504 // Specified argument was out of the range of valid values
3505 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
3506 Assert.IsNull (ex.InnerException, "#B3");
3507 Assert.IsNotNull (ex.Message, "#B4");
3508 Assert.AreEqual ("offset", ex.ParamName, "#B5");
3514 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref IPEndPoint)
3515 public void ReceiveFrom4_RemoteEP_Null ()
3517 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3519 byte [] buffer = new byte [5];
3520 EndPoint remoteEP = null;
3523 s.ReceiveFrom (buffer, 0, buffer.Length, (SocketFlags) 666, ref remoteEP);
3525 } catch (ArgumentNullException ex) {
3526 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
3527 Assert.IsNull (ex.InnerException, "#3");
3528 Assert.IsNotNull (ex.Message, "#4");
3529 Assert.AreEqual ("remoteEP", ex.ParamName, "#5");
3535 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, EndPoint)
3536 #if FEATURE_NO_BSD_SOCKETS
3537 [ExpectedException (typeof (PlatformNotSupportedException))]
3539 public void ReceiveFrom4_Size_OutOfRange ()
3542 byte [] buffer = new byte [5];
3543 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3546 s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3549 s.ReceiveFrom (buffer, 0, -1, (SocketFlags) 666,
3551 Assert.Fail ("#A1");
3552 } catch (ArgumentOutOfRangeException ex) {
3553 // Specified argument was out of the range of valid values
3554 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2");
3555 Assert.IsNull (ex.InnerException, "#A3");
3556 Assert.IsNotNull (ex.Message, "#A4");
3557 Assert.AreEqual ("size", ex.ParamName, "#A5");
3562 // size > buffer length
3563 s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3566 s.ReceiveFrom (buffer, 0, (buffer.Length + 1), (SocketFlags) 666,
3568 Assert.Fail ("#B1");
3569 } catch (ArgumentOutOfRangeException ex) {
3570 // Specified argument was out of the range of valid values
3571 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
3572 Assert.IsNull (ex.InnerException, "#B3");
3573 Assert.IsNotNull (ex.Message, "#B4");
3574 Assert.AreEqual ("size", ex.ParamName, "#B5");
3579 // offset + size > buffer length
3580 s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3583 s.ReceiveFrom (buffer, 2, 4, (SocketFlags) 666, ref remoteEP);
3584 Assert.Fail ("#C1");
3585 } catch (ArgumentOutOfRangeException ex) {
3586 // Specified argument was out of the range of valid values
3587 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
3588 Assert.IsNull (ex.InnerException, "#C3");
3589 Assert.IsNotNull (ex.Message, "#C4");
3590 Assert.AreEqual ("size", ex.ParamName, "#C5");
3596 [Test] // ReceiveFrom (Byte [], Int32, Int32, SocketFlags, ref EndPoint)
3597 #if FEATURE_NO_BSD_SOCKETS
3598 [ExpectedException (typeof (PlatformNotSupportedException))]
3600 public void ReceiveFrom4_Socket_Closed ()
3602 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream,
3606 byte [] buffer = new byte [5];
3607 EndPoint remoteEP = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
3609 s.ReceiveFrom (buffer, -1, -1, (SocketFlags) 666,
3612 } catch (ObjectDisposedException ex) {
3613 // Cannot access a disposed object
3614 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3615 Assert.IsNull (ex.InnerException, "#3");
3616 Assert.IsNotNull (ex.Message, "#4");
3617 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3622 #if FEATURE_NO_BSD_SOCKETS
3623 [ExpectedException (typeof (PlatformNotSupportedException))]
3625 public void ReceiveRemoteClosed ()
3627 var port = NetworkHelpers.FindFreePort ();
3628 Socket sock = new Socket (AddressFamily.InterNetwork,
3631 sock.Bind (new IPEndPoint (IPAddress.Loopback, port));
3635 Thread client_thread = new Thread (() => RRCClientThread (port));
3636 client_thread.Start ();
3638 Socket client = sock.Accept ();
3639 NetworkStream stream = new NetworkStream (client);
3640 stream.WriteByte (0x00);
3641 stream.WriteByte (0x00);
3645 RRCReady.WaitOne (1000, false);
3646 Assert.IsTrue (RRCLastRead);
3650 // Test case for bug #471580
3652 #if FEATURE_NO_BSD_SOCKETS
3653 [ExpectedException (typeof (PlatformNotSupportedException))]
3655 public void UdpDoubleBind ()
3657 using (Socket s = new Socket (AddressFamily.InterNetwork,
3658 SocketType.Dgram, ProtocolType.Udp))
3659 using (Socket ss = new Socket (AddressFamily.InterNetwork,
3660 SocketType.Dgram, ProtocolType.Udp)) {
3661 var supportsReuseAddress = true;
3663 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
3664 } catch (SocketException e) {
3665 // Exception is thrown when ReuseAddress is not supported
3666 Assert.AreEqual ((int) SocketError.OperationNotSupported, e.NativeErrorCode,
3667 "Expected SocketError.OperationNotSupported");
3668 supportsReuseAddress = false;
3671 var ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
3674 if (supportsReuseAddress)
3675 ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
3678 ss.Bind (new IPEndPoint (IPAddress.Any, ep.Port));
3679 if (!supportsReuseAddress)
3680 Assert.Fail ("Reusing address is not supported, exception was expected on second bind.");
3681 } catch (SocketException e) {
3686 // Test case for bug #31557
3688 #if FEATURE_NO_BSD_SOCKETS
3689 [ExpectedException (typeof (PlatformNotSupportedException))]
3691 public void TcpDoubleBind ()
3693 using (Socket s = new Socket (AddressFamily.InterNetwork,
3694 SocketType.Stream, ProtocolType.Tcp))
3695 using (Socket ss = new Socket (AddressFamily.InterNetwork,
3696 SocketType.Stream, ProtocolType.Tcp)) {
3697 var supportsReuseAddress = true;
3699 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
3700 } catch (SocketException e) {
3701 // Exception is thrown when ReuseAddress is not supported
3702 Assert.AreEqual ((int) SocketError.OperationNotSupported, e.NativeErrorCode,
3703 "Expected SocketError.OperationNotSupported");
3704 supportsReuseAddress = false;
3707 var ep = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
3711 if (supportsReuseAddress)
3712 ss.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
3715 ss.Bind (new IPEndPoint (IPAddress.Any, ep.Port));
3717 if (!supportsReuseAddress)
3718 Assert.Fail ("Reusing address is not supported, exception was expected on second bind.");
3719 } catch (SocketException e) {
3724 // Test case for https://bugzilla.novell.com/show_bug.cgi?id=443346
3725 // See also https://bugzilla.xamarin.com/show_bug.cgi?id=52157
3727 public void ConnectedProperty ()
3729 var port = NetworkHelpers.FindFreePort ();
3730 var listener = new TcpListener (IPAddress.Loopback, port);
3733 var client = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3734 client.Connect (IPAddress.Loopback, port);
3735 var server = listener.AcceptSocket ();
3737 const int blobSize = 2048;
3739 // Small send/recv buffers so that our send operation will fill them
3740 server.ReceiveBufferSize = server.SendBufferSize = 256;
3741 client.ReceiveBufferSize = client.SendBufferSize = 256;
3743 // Setting this linger state causes shutdown to be a hard close (RST if send data is pending)
3744 server.LingerState = client.LingerState = new LingerOption (true, 0);
3746 // KeepAlive being set introduces another opportunity for a RST according to some documentation
3747 server.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
3748 client.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
3752 // Fill the send buffer
3753 client.Send (new byte[blobSize]);
3754 // Now shut down the socket. Because of the linger state this will hard close and send RST.
3755 client.Shutdown (SocketShutdown.Both);
3758 Assert.IsFalse (client.Connected);
3759 Assert.IsTrue (server.Connected);
3765 // On Windows this BeginSend succeeds... it really shouldn't and a sync send fails.
3766 // On Linux this fails with a ConnectionReset.
3767 var ar = server.BeginSend (new byte[blobSize], 0, blobSize, SocketFlags.None, null, null);
3768 // On Mac we get an erroneous WSAESHUTDOWN (10058) here
3769 server.EndSend (ar);
3771 catch (SocketException se)
3776 (se.SocketErrorCode != SocketError.ConnectionReset) &&
3777 (se.SocketErrorCode != SocketError.Shutdown)
3786 // On Windows this second send operation will fail with a ConnectionReset.
3787 var ar = server.BeginSend (new byte[blobSize], 0, blobSize, SocketFlags.None, null, null);
3788 server.EndSend (ar);
3790 catch (SocketException se)
3792 if (se.SocketErrorCode != SocketError.ConnectionReset)
3797 Assert.IsFalse (client.Connected);
3798 Assert.IsFalse (server.Connected);
3808 [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName)
3809 public void GetSocketOption1_Socket_Closed ()
3811 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3814 s.GetSocketOption (0, 0);
3816 } catch (ObjectDisposedException ex) {
3817 // Cannot access a disposed object
3818 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3819 Assert.IsNull (ex.InnerException, "#3");
3820 Assert.IsNotNull (ex.Message, "#4");
3821 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3825 [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3826 public void GetSocketOption2_OptionValue_Null ()
3828 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3830 s.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
3833 } catch (SocketException ex) {
3834 // The system detected an invalid pointer address in attempting
3835 // to use a pointer argument in a call
3836 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3837 Assert.AreEqual (10014, ex.ErrorCode, "#3");
3838 Assert.IsNull (ex.InnerException, "#4");
3839 Assert.IsNotNull (ex.Message, "#5");
3840 Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
3841 Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
3845 [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3846 public void GetSocketOption2_Socket_Closed ()
3848 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3851 s.GetSocketOption (0, 0, (byte []) null);
3853 } catch (ObjectDisposedException ex) {
3854 // Cannot access a disposed object
3855 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3856 Assert.IsNull (ex.InnerException, "#3");
3857 Assert.IsNotNull (ex.Message, "#4");
3858 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3862 [Test] // GetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
3863 public void GetSocketOption3_Socket_Closed ()
3865 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3868 s.GetSocketOption (0, 0, 0);
3870 } catch (ObjectDisposedException ex) {
3871 // Cannot access a disposed object
3872 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3873 Assert.IsNull (ex.InnerException, "#3");
3874 Assert.IsNotNull (ex.Message, "#4");
3875 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3879 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3880 public void SetSocketOption1_DontLinger ()
3882 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3883 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
3884 new byte [] { 0x00 });
3885 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
3886 new byte [] { 0x01 });
3890 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3891 public void SetSocketOption1_DontLinger_Null ()
3893 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3895 s.SetSocketOption (SocketOptionLevel.Socket,
3896 SocketOptionName.DontLinger, (byte []) null);
3898 } catch (SocketException ex) {
3899 // The system detected an invalid pointer address in attempting
3900 // to use a pointer argument in a call
3901 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3902 Assert.AreEqual (10014, ex.ErrorCode, "#3");
3903 Assert.IsNull (ex.InnerException, "#4");
3904 Assert.IsNotNull (ex.Message, "#5");
3905 Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
3906 Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
3911 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3912 public void SetSocketOption1_Linger_Null ()
3914 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3916 s.SetSocketOption (SocketOptionLevel.Socket,
3917 SocketOptionName.DontLinger, (byte []) null);
3919 } catch (SocketException ex) {
3920 // The system detected an invalid pointer address in attempting
3921 // to use a pointer argument in a call
3922 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
3923 Assert.AreEqual (10014, ex.ErrorCode, "#3");
3924 Assert.IsNull (ex.InnerException, "#4");
3925 Assert.IsNotNull (ex.Message, "#5");
3926 Assert.AreEqual (10014, ex.NativeErrorCode, "#6");
3927 Assert.AreEqual (SocketError.Fault, ex.SocketErrorCode, "#7");
3932 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Byte [])
3933 public void SetSocketOption1_Socket_Close ()
3935 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3938 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger,
3939 new byte [] { 0x00 });
3941 } catch (ObjectDisposedException ex) {
3942 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3943 Assert.IsNull (ex.InnerException, "#3");
3944 Assert.IsNotNull (ex.Message, "#4");
3945 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3949 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
3950 public void SetSocketOption2_DontLinger ()
3952 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3953 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 0);
3954 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 5);
3958 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
3959 [Category ("NotWorking")]
3960 public void SetSocketOption2_Linger ()
3962 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
3963 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, 0);
3964 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger, 5);
3968 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Int32)
3969 public void SetSocketOption2_Socket_Closed ()
3971 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
3974 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.DontLinger, 0);
3976 } catch (ObjectDisposedException ex) {
3977 // Cannot access a disposed object
3978 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
3979 Assert.IsNull (ex.InnerException, "#3");
3980 Assert.IsNotNull (ex.Message, "#4");
3981 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
3985 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
3986 #if FEATURE_NO_BSD_SOCKETS
3987 [ExpectedException (typeof (PlatformNotSupportedException))]
3989 public void SetSocketOption3_AddMembershipIPv4_IPv6MulticastOption ()
3991 IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
3993 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
3994 s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
3996 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
3997 new IPv6MulticastOption (mcast_addr));
3999 } catch (ArgumentException ex) {
4000 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4001 Assert.IsNull (ex.InnerException, "#3");
4002 Assert.IsNotNull (ex.Message, "#4");
4003 // The specified value is not a valid 'MulticastOption'
4004 Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
4005 Assert.AreEqual ("optionValue", ex.ParamName, "#6");
4010 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4011 #if FEATURE_NO_BSD_SOCKETS
4012 [ExpectedException (typeof (PlatformNotSupportedException))]
4014 public void SetSocketOption3_AddMembershipIPv4_MulticastOption ()
4016 IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
4018 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
4019 s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
4020 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
4021 new MulticastOption (mcast_addr));
4025 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4026 [Category ("NotWorking")]
4027 public void SetSocketOption3_AddMembershipIPv4_Socket_NotBound ()
4029 IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
4031 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
4033 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
4034 new MulticastOption (mcast_addr));
4036 } catch (SocketException ex) {
4037 // An invalid argument was supplied
4038 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
4039 Assert.AreEqual (10022, ex.ErrorCode, "#3");
4040 Assert.IsNull (ex.InnerException, "#4");
4041 Assert.IsNotNull (ex.Message, "#5");
4042 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
4043 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
4049 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4050 #if FEATURE_NO_BSD_SOCKETS
4051 [ExpectedException (typeof (PlatformNotSupportedException))]
4053 public void SetSocketOption3_AddMembershipIPv6_IPv6MulticastOption ()
4055 if (!Socket.OSSupportsIPv6)
4056 Assert.Ignore ("IPv6 not enabled.");
4058 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
4060 using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
4061 s.Bind (new IPEndPoint (IPAddress.IPv6Any, NetworkHelpers.FindFreePort ()));
4062 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
4063 new IPv6MulticastOption (mcast_addr));
4067 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4068 #if FEATURE_NO_BSD_SOCKETS
4069 [ExpectedException (typeof (PlatformNotSupportedException))]
4071 public void SetSocketOption3_AddMembershipIPv6_MulticastOption ()
4073 if (!Socket.OSSupportsIPv6)
4074 Assert.Ignore ("IPv6 not enabled.");
4076 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
4078 using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
4079 s.Bind (new IPEndPoint (IPAddress.IPv6Any, NetworkHelpers.FindFreePort ()));
4081 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
4082 new MulticastOption (mcast_addr));
4084 } catch (ArgumentException ex) {
4085 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4086 Assert.IsNull (ex.InnerException, "#3");
4087 Assert.IsNotNull (ex.Message, "#4");
4088 // The specified value is not a valid 'IPv6MulticastOption'
4089 Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
4090 Assert.AreEqual ("optionValue", ex.ParamName, "#6");
4095 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4096 [Category ("NotWorking")]
4097 public void SetSocketOption3_AddMembershipIPv6_Socket_NotBound ()
4099 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
4101 Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
4103 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
4104 new IPv6MulticastOption (mcast_addr));
4106 } catch (SocketException ex) {
4107 // An invalid argument was supplied
4108 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
4109 Assert.AreEqual (10022, ex.ErrorCode, "#3");
4110 Assert.IsNull (ex.InnerException, "#4");
4111 Assert.IsNotNull (ex.Message, "#5");
4112 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
4113 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
4119 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4120 public void SetSocketOption3_DontLinger_Boolean ()
4122 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4124 s.SetSocketOption (SocketOptionLevel.Socket,
4125 SocketOptionName.DontLinger, (object) false);
4127 } catch (ArgumentException ex) {
4128 // The specified value is not valid
4129 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4130 Assert.IsNull (ex.InnerException, "#3");
4131 Assert.IsNotNull (ex.Message, "#4");
4132 Assert.AreEqual ("optionValue", ex.ParamName, "#5");
4137 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4138 public void SetSocketOption3_DontLinger_Int32 ()
4140 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4142 s.SetSocketOption (SocketOptionLevel.Socket,
4143 SocketOptionName.DontLinger, (object) 0);
4145 } catch (ArgumentException ex) {
4146 // The specified value is not valid
4147 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4148 Assert.IsNull (ex.InnerException, "#3");
4149 Assert.IsNotNull (ex.Message, "#4");
4150 Assert.AreEqual ("optionValue", ex.ParamName, "#5");
4155 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4156 public void SetSocketOption3_DontLinger_LingerOption ()
4158 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4160 s.SetSocketOption (SocketOptionLevel.Socket,
4161 SocketOptionName.DontLinger, new LingerOption (true, 1000));
4163 } catch (ArgumentException ex) {
4164 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4165 Assert.IsNull (ex.InnerException, "#3");
4166 // The specified value is not valid
4167 Assert.IsNotNull (ex.Message, "#4");
4168 Assert.AreEqual ("optionValue", ex.ParamName, "#5");
4173 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4174 public void SetSocketOption3_Linger_Boolean ()
4176 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4178 s.SetSocketOption (SocketOptionLevel.Socket,
4179 SocketOptionName.Linger, (object) false);
4181 } catch (ArgumentException ex) {
4182 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4183 Assert.IsNull (ex.InnerException, "#3");
4184 // The specified value is not valid
4185 Assert.IsNotNull (ex.Message, "#4");
4186 Assert.AreEqual ("optionValue", ex.ParamName, "#5");
4191 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4192 public void SetSocketOption3_Linger_Int32 ()
4194 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4196 s.SetSocketOption (SocketOptionLevel.Socket,
4197 SocketOptionName.Linger, (object) 0);
4199 } catch (ArgumentException ex) {
4200 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4201 Assert.IsNull (ex.InnerException, "#3");
4202 // The specified value is not valid
4203 Assert.IsNotNull (ex.Message, "#4");
4204 Assert.AreEqual ("optionValue", ex.ParamName, "#5");
4209 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4210 public void SetSocketOption3_Linger_LingerOption ()
4212 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4213 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
4214 new LingerOption (false, 0));
4215 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
4216 new LingerOption (true, 0));
4217 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
4218 new LingerOption (false, 1000));
4219 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
4220 new LingerOption (true, 1000));
4224 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4225 #if FEATURE_NO_BSD_SOCKETS
4226 [ExpectedException (typeof (PlatformNotSupportedException))]
4228 public void SetSocketOption3_DropMembershipIPv4_IPv6MulticastOption ()
4230 IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
4232 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
4233 s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
4234 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
4235 new MulticastOption (mcast_addr));
4237 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
4238 new IPv6MulticastOption (mcast_addr));
4240 } catch (ArgumentException ex) {
4241 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4242 Assert.IsNull (ex.InnerException, "#3");
4243 Assert.IsNotNull (ex.Message, "#4");
4244 // The specified value is not a valid 'MulticastOption'
4245 Assert.IsTrue (ex.Message.IndexOf ("'MulticastOption'") != -1, "#5:" + ex.Message);
4246 Assert.AreEqual ("optionValue", ex.ParamName, "#6");
4251 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4252 #if FEATURE_NO_BSD_SOCKETS
4253 [ExpectedException (typeof (PlatformNotSupportedException))]
4255 public void SetSocketOption3_DropMembershipIPv4_MulticastOption ()
4257 IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
4259 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
4260 MulticastOption option = new MulticastOption (mcast_addr);
4262 s.Bind (new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ()));
4263 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership,
4265 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
4270 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4271 [Category ("NotWorking")]
4272 public void SetSocketOption3_DropMembershipIPv4_Socket_NotBound ()
4274 IPAddress mcast_addr = IPAddress.Parse ("239.255.255.250");
4276 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
4278 s.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.DropMembership,
4279 new MulticastOption (mcast_addr));
4281 } catch (SocketException ex) {
4282 // An invalid argument was supplied
4283 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
4284 Assert.AreEqual (10022, ex.ErrorCode, "#3");
4285 Assert.IsNull (ex.InnerException, "#4");
4286 Assert.IsNotNull (ex.Message, "#5");
4287 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
4288 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
4294 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4295 #if FEATURE_NO_BSD_SOCKETS
4296 [ExpectedException (typeof (PlatformNotSupportedException))]
4298 public void SetSocketOption3_DropMembershipIPv6_IPv6MulticastOption ()
4300 if (!Socket.OSSupportsIPv6)
4301 Assert.Ignore ("IPv6 not enabled.");
4303 using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
4304 IPv6MulticastOption option = new IPv6MulticastOption (
4305 IPAddress.Parse ("ff02::1"));
4307 s.Bind (new IPEndPoint (IPAddress.IPv6Any, 0));
4308 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
4310 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
4315 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4316 #if FEATURE_NO_BSD_SOCKETS
4317 [ExpectedException (typeof (PlatformNotSupportedException))]
4319 public void SetSocketOption3_DropMembershipIPv6_MulticastOption ()
4321 if (!Socket.OSSupportsIPv6)
4322 Assert.Ignore ("IPv6 not enabled.");
4324 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
4326 using (Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp)) {
4327 s.Bind (new IPEndPoint (IPAddress.IPv6Any, NetworkHelpers.FindFreePort ()));
4328 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
4329 new IPv6MulticastOption (mcast_addr));
4331 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
4332 new MulticastOption (mcast_addr));
4334 } catch (ArgumentException ex) {
4335 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
4336 Assert.IsNull (ex.InnerException, "#3");
4337 Assert.IsNotNull (ex.Message, "#4");
4338 // The specified value is not a valid 'IPv6MulticastOption'
4339 Assert.IsTrue (ex.Message.IndexOf ("'IPv6MulticastOption'") != -1, "#5:" + ex.Message);
4340 Assert.AreEqual ("optionValue", ex.ParamName, "#6");
4345 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4346 [Category ("NotWorking")]
4347 public void SetSocketOption3_DropMembershipIPv6_Socket_NotBound ()
4349 IPAddress mcast_addr = IPAddress.Parse ("ff02::1");
4351 Socket s = new Socket (AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
4353 s.SetSocketOption (SocketOptionLevel.IPv6, SocketOptionName.DropMembership,
4354 new IPv6MulticastOption (mcast_addr));
4356 } catch (SocketException ex) {
4357 // An invalid argument was supplied
4358 Assert.AreEqual (typeof (SocketException), ex.GetType (), "#2");
4359 Assert.AreEqual (10022, ex.ErrorCode, "#3");
4360 Assert.IsNull (ex.InnerException, "#4");
4361 Assert.IsNotNull (ex.Message, "#5");
4362 Assert.AreEqual (10022, ex.NativeErrorCode, "#6");
4363 Assert.AreEqual (SocketError.InvalidArgument, ex.SocketErrorCode, "#7");
4369 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4370 public void SetSocketOption3_OptionValue_Null ()
4372 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4374 s.SetSocketOption (SocketOptionLevel.Socket,
4375 SocketOptionName.Linger, (object) null);
4377 } catch (ArgumentNullException ex) {
4378 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
4379 Assert.IsNull (ex.InnerException, "#3");
4380 Assert.IsNotNull (ex.Message, "#4");
4381 Assert.AreEqual ("optionValue", ex.ParamName, "#5");
4386 [Test] // SetSocketOption (SocketOptionLevel, SocketOptionName, Object)
4387 public void SetSocketOption3_Socket_Closed ()
4389 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4392 s.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Linger,
4393 new LingerOption (false, 0));
4395 } catch (ObjectDisposedException ex) {
4396 // Cannot access a disposed object
4397 Assert.AreEqual (typeof (ObjectDisposedException), ex.GetType (), "#2");
4398 Assert.IsNull (ex.InnerException, "#3");
4399 Assert.IsNotNull (ex.Message, "#4");
4400 Assert.AreEqual (typeof (Socket).FullName, ex.ObjectName, "#5");
4405 [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.
4406 public void SetSocketOption_MulticastInterfaceIndex_Any ()
4408 IPAddress ip = IPAddress.Parse ("239.255.255.250");
4410 using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
4412 s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
4413 s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
4418 [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.
4419 public void SetSocketOption_MulticastInterfaceIndex_Loopback ()
4421 IPAddress ip = IPAddress.Parse ("239.255.255.250");
4423 using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
4425 s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
4426 s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
4431 public void SetSocketOption_MulticastInterfaceIndex_Invalid ()
4433 IPAddress ip = IPAddress.Parse ("239.255.255.250");
4435 using (Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
4439 s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, IPAddress.HostToNetworkOrder(index));
4446 s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(ip, index));
4455 #if FEATURE_NO_BSD_SOCKETS
4456 [ExpectedException (typeof (PlatformNotSupportedException))]
4458 public void Shutdown_NoConnect ()
4460 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4461 s.Bind (new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ()));
4464 s.Shutdown (SocketShutdown.Both);
4466 } catch (SocketException exc) {
4467 Assert.AreEqual (10057, exc.ErrorCode, "#2");
4474 [ExpectedException (typeof (NullReferenceException))]
4475 public void ReceiveAsync_Null ()
4477 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4478 s.ReceiveAsync (null);
4483 [ExpectedException (typeof (NullReferenceException))]
4484 public void ReceiveAsync_Default ()
4486 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4487 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
4488 s.ReceiveAsync (saea);
4494 [ExpectedException (typeof (NullReferenceException))]
4495 public void ReceiveAsync_NullBuffer ()
4497 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4498 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
4499 saea.SetBuffer (null, 0, 0);
4500 s.ReceiveAsync (null);
4505 [ExpectedException (typeof (ObjectDisposedException))]
4506 public void ReceiveAsync_ClosedSocket ()
4508 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4510 s.ReceiveAsync (null);
4514 [ExpectedException (typeof (NullReferenceException))]
4515 public void SendAsync_Null ()
4517 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4523 [ExpectedException (typeof (NullReferenceException))]
4524 public void SendAsync_Default ()
4526 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4527 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
4534 [ExpectedException (typeof (NullReferenceException))]
4535 public void SendAsync_NullBuffer ()
4537 using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
4538 SocketAsyncEventArgs saea = new SocketAsyncEventArgs ();
4539 saea.SetBuffer (null, 0, 0);
4545 [ExpectedException (typeof (ObjectDisposedException))]
4546 public void SendAsync_ClosedSocket ()
4548 Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4554 #if FEATURE_NO_BSD_SOCKETS
4555 [ExpectedException (typeof (PlatformNotSupportedException))]
4557 [Ignore ("https://bugzilla.xamarin.com/show_bug.cgi?id=43172")]
4558 public void SendAsyncFile ()
4560 Socket serverSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4562 serverSocket.Bind (new IPEndPoint (IPAddress.Loopback, 0));
4563 serverSocket.Listen (1);
4565 var mReceived = new ManualResetEvent (false);
4567 serverSocket.BeginAccept (AsyncCall => {
4568 byte[] bytes = new byte [1024];
4570 Socket listener = (Socket)AsyncCall.AsyncState;
4571 Socket client = listener.EndAccept (AsyncCall);
4572 client.Receive (bytes, bytes.Length, 0);
4577 Socket clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
4578 clientSocket.Connect (serverSocket.LocalEndPoint);
4579 clientSocket.NoDelay = true;
4581 // Initialize buffer used to create testing file
4582 var buffer = new byte [1024];
4583 for (int i = 0; i < 1024; ++i)
4584 buffer [i] = (byte) (i % 256);
4586 string temp = Path.GetTempFileName ();
4588 // Testing file creation
4589 using (StreamWriter sw = new StreamWriter (temp)) {
4593 var mSent = new ManualResetEvent (false);
4595 // Async Send File to server
4596 clientSocket.BeginSendFile(temp, (ar) => {
4597 Socket client = (Socket) ar.AsyncState;
4598 client.EndSendFile (ar);
4602 Assert.IsTrue (mSent.WaitOne (5000), "#1");
4603 Assert.IsTrue (mReceived.WaitOne (5000), "#2");
4605 if (File.Exists (temp))
4608 clientSocket.Close ();
4609 serverSocket.Close ();
4614 #if FEATURE_NO_BSD_SOCKETS
4615 [ExpectedException (typeof (PlatformNotSupportedException))]
4617 public void ConnectToIPV4EndPointUsingDualModelSocket () {
4619 * IPv6 DualMode sockets are defaults in Mono. Explicitly specify that
4620 * anyways in this test to make it more interoparable with .NET where
4621 * IPv6 and DualMode needs to be specified.
4623 using (var server = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp)) {
4625 var host = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
4627 server.DualMode = true;
4630 * Nothing to Accept the connect - we need a backlog to make sure we don't get
4635 var ep = server.LocalEndPoint as IPEndPoint;
4636 var client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
4637 client.DualMode = true;
4638 client.Connect (ep);
4639 client.Disconnect (false);
4642 client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
4643 client.DualMode = true;
4644 client.Connect (IPAddress.Loopback, ep.Port);
4645 client.Disconnect (false);
4648 client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
4649 client.DualMode = true;
4650 client.Connect (new [] { IPAddress.Loopback }, ep.Port);
4651 client.Disconnect (false);
4657 #if FEATURE_NO_BSD_SOCKETS
4658 [ExpectedException (typeof (PlatformNotSupportedException))]
4660 public void BeginConnectToIPV4EndPointUsingDualModelSocket () {
4662 * IPv6 DualMode sockets are defaults in Mono. Explicitly specify that
4663 * anyways in this test to make it more interoparable with .NET where
4664 * IPv6 and DualMode needs to be specified.
4666 using (var server = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp))
4668 var host = new IPEndPoint (IPAddress.Loopback, NetworkHelpers.FindFreePort ());
4670 server.DualMode = true;
4674 var ep = server.LocalEndPoint as IPEndPoint;
4676 BCCalledBack.Reset ();
4677 var client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
4678 client.DualMode = true;
4679 var ar1 = client.BeginConnect (ep, BCCallback, client);
4680 Assert.IsTrue (BCCalledBack.WaitOne (10000), "#1");
4681 Assert.AreEqual(server.AddressFamily, client.RemoteEndPoint.AddressFamily, "#2");
4682 Assert.AreEqual(server.AddressFamily, client.LocalEndPoint.AddressFamily, "#3");
4683 client.Disconnect (false);
4686 BCCalledBack.Reset ();
4687 client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
4688 client.DualMode = true;
4689 var ar2 = client.BeginConnect (IPAddress.Loopback, ep.Port, BCCallback, client);
4690 Assert.IsTrue (BCCalledBack.WaitOne (10000), "#4");
4691 Assert.AreEqual(server.AddressFamily, client.RemoteEndPoint.AddressFamily, "#5");
4692 Assert.AreEqual(server.AddressFamily, client.LocalEndPoint.AddressFamily, "#6");
4693 client.Disconnect (false);
4696 BCCalledBack.Reset ();
4697 client = new Socket (AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
4698 client.DualMode = true;
4699 var ar3 = client.BeginConnect (new [] {IPAddress.Loopback}, ep.Port, BCCallback, client);
4700 Assert.IsTrue (BCCalledBack.WaitOne (10000), "#7");
4701 Assert.AreEqual(server.AddressFamily, client.RemoteEndPoint.AddressFamily, "#8");
4702 Assert.AreEqual(server.AddressFamily, client.LocalEndPoint.AddressFamily, "#9");
4703 client.Disconnect (false);
4709 #if FEATURE_NO_BSD_SOCKETS
4710 [ExpectedException (typeof (PlatformNotSupportedException))]
4712 public void UdpMulticasTimeToLive ()
4714 /* see https://bugzilla.xamarin.com/show_bug.cgi?id=36941 */
4716 using (Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
4717 IPEndPoint end_point = new IPEndPoint (IPAddress.Any, NetworkHelpers.FindFreePort ());
4718 socket.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
4719 socket.Bind (end_point);
4720 socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 19);
4724 [Test] // Covers 41616
4725 public void ConnectAsyncUnhandledEx ()
4727 var mre = new ManualResetEvent (false);
4729 var endPoint = new IPEndPoint(0,0);
4730 var socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Unspecified);
4732 var socketArgs = new SocketAsyncEventArgs();
4733 socketArgs.RemoteEndPoint = endPoint;
4734 socketArgs.Completed += (sender, e) => mre.Set ();
4736 socket.ConnectAsync (socketArgs);
4738 Assert.IsTrue (mre.WaitOne (1000), "ConnectedAsync timeout");
4741 [Test] // Covers https://bugzilla.xamarin.com/show_bug.cgi?id=52549
4742 public void SocketMismatchProtocol ()
4745 using (Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Tcp));
4747 } catch (SocketException e) {
4749 // Assert.AreEqual(SocketError.ProtocolType, e.SocketErrorCode, "#2");
4753 using (Socket socket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Udp));
4755 } catch (SocketException e) {
4757 // Assert.AreEqual(SocketError.ProtocolType, e.SocketErrorCode, "#4");