SocketTest.cs 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830
  1. // System.Net.Sockets.SocketTest.cs
  2. //
  3. // Authors:
  4. // Brad Fitzpatrick ([email protected])
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) Copyright 2003 Brad Fitzpatrick
  8. // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
  9. //
  10. using System;
  11. using System.Collections;
  12. using System.Threading;
  13. using System.Net;
  14. using System.Net.Sockets;
  15. using NUnit.Framework;
  16. #if NET_2_0
  17. using System.Collections.Generic;
  18. #endif
  19. namespace MonoTests.System.Net.Sockets
  20. {
  21. [TestFixture]
  22. public class SocketTest
  23. {
  24. // note: also used in SocketCas tests
  25. public const string BogusAddress = "192.168.244.244";
  26. public const int BogusPort = 23483;
  27. [Test]
  28. public void ConnectIPAddressAny ()
  29. {
  30. IPEndPoint ep = new IPEndPoint (IPAddress.Any, 0);
  31. try {
  32. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)) {
  33. s.Connect (ep);
  34. s.Close ();
  35. }
  36. Assert.Fail ("#1");
  37. } catch (SocketException ex) {
  38. Assert.AreEqual (10049, ex.ErrorCode, "#2");
  39. }
  40. try {
  41. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) {
  42. s.Connect (ep);
  43. s.Close ();
  44. }
  45. Assert.Fail ("#3");
  46. } catch (SocketException ex) {
  47. Assert.AreEqual (10049, ex.ErrorCode, "#4");
  48. }
  49. }
  50. [Test]
  51. [Ignore ("Bug #75158")]
  52. public void IncompatibleAddress ()
  53. {
  54. IPEndPoint epIPv6 = new IPEndPoint (IPAddress.IPv6Any,
  55. 0);
  56. try {
  57. using (Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP)) {
  58. s.Connect (epIPv6);
  59. s.Close ();
  60. }
  61. Assert.Fail ("#1");
  62. } catch (SocketException ex) {
  63. #if !NET_2_0
  64. // invalid argument
  65. int expectedError = 10022;
  66. #else
  67. // address incompatible with protocol
  68. int expectedError = 10047;
  69. #endif
  70. Assert.AreEqual (expectedError, ex.ErrorCode,
  71. "#2");
  72. }
  73. }
  74. [Test]
  75. [Category ("InetAccess")]
  76. public void EndConnect ()
  77. {
  78. IPAddress ipOne = IPAddress.Parse (BogusAddress);
  79. IPEndPoint ipEP = new IPEndPoint (ipOne, BogusPort);
  80. Socket sock = new Socket (ipEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  81. IAsyncResult ar = sock.BeginConnect (ipEP, null, null);
  82. try {
  83. // should raise an exception because connect was bogus
  84. sock.EndConnect (ar);
  85. Assert.Fail ("#1");
  86. } catch (SocketException ex) {
  87. Assert.AreEqual (10060, ex.ErrorCode, "#2");
  88. }
  89. }
  90. [Test]
  91. [ExpectedException (typeof (ArgumentNullException))]
  92. public void SelectEmpty ()
  93. {
  94. ArrayList list = new ArrayList ();
  95. Socket.Select (list, list, list, 1000);
  96. }
  97. private bool BlockingConnect (bool block)
  98. {
  99. IPEndPoint ep = new IPEndPoint(IPAddress.Loopback, 1234);
  100. Socket server = new Socket(AddressFamily.InterNetwork,
  101. SocketType.Stream,
  102. ProtocolType.Tcp);
  103. server.Bind(ep);
  104. server.Blocking=block;
  105. server.Listen(0);
  106. Socket conn = new Socket (AddressFamily.InterNetwork,
  107. SocketType.Stream,
  108. ProtocolType.Tcp);
  109. conn.Connect (ep);
  110. Socket client = server.Accept();
  111. bool client_block = client.Blocking;
  112. client.Close();
  113. conn.Close();
  114. server.Close();
  115. return(client_block);
  116. }
  117. [Test]
  118. public void AcceptBlockingStatus()
  119. {
  120. bool block;
  121. block = BlockingConnect(true);
  122. Assertion.AssertEquals ("BlockingStatus01",
  123. block, true);
  124. block = BlockingConnect(false);
  125. Assertion.AssertEquals ("BlockingStatus02",
  126. block, false);
  127. }
  128. static bool CFAConnected = false;
  129. static ManualResetEvent CFACalledBack;
  130. private static void CFACallback (IAsyncResult asyncResult)
  131. {
  132. Socket sock = (Socket)asyncResult.AsyncState;
  133. CFAConnected = sock.Connected;
  134. if (sock.Connected) {
  135. sock.EndConnect (asyncResult);
  136. }
  137. CFACalledBack.Set ();
  138. }
  139. [Test]
  140. public void ConnectFailAsync ()
  141. {
  142. Socket sock = new Socket (AddressFamily.InterNetwork,
  143. SocketType.Stream,
  144. ProtocolType.Tcp);
  145. sock.Blocking = false;
  146. CFACalledBack = new ManualResetEvent (false);
  147. CFACalledBack.Reset ();
  148. /* Need a port that is not being used for
  149. * anything...
  150. */
  151. sock.BeginConnect (new IPEndPoint (IPAddress.Loopback,
  152. 114),
  153. new AsyncCallback (CFACallback),
  154. sock);
  155. CFACalledBack.WaitOne ();
  156. Assertion.AssertEquals ("ConnectFail", CFAConnected,
  157. false);
  158. }
  159. #if !TARGET_JVM
  160. [Test]
  161. #if !NET_2_0
  162. [ExpectedException (typeof (ArgumentException))]
  163. #endif
  164. public void SetSocketOptionBoolean ()
  165. {
  166. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback, 1);
  167. Socket sock = new Socket (ep.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  168. try {
  169. sock.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
  170. } finally {
  171. sock.Close ();
  172. }
  173. }
  174. #endif
  175. [Test]
  176. public void TestSelect1 ()
  177. {
  178. Socket srv = CreateServer ();
  179. ClientSocket clnt = new ClientSocket (srv.LocalEndPoint);
  180. Thread th = new Thread (new ThreadStart (clnt.ConnectSleepClose));
  181. Socket acc = null;
  182. try {
  183. th.Start ();
  184. acc = srv.Accept ();
  185. clnt.Write ();
  186. ArrayList list = new ArrayList ();
  187. ArrayList empty = new ArrayList ();
  188. list.Add (acc);
  189. Socket.Select (list, empty, empty, 100);
  190. Assertion.AssertEquals ("#01", 0, empty.Count);
  191. Assertion.AssertEquals ("#02", 1, list.Count);
  192. Socket.Select (empty, list, empty, 100);
  193. Assertion.AssertEquals ("#03", 0, empty.Count);
  194. Assertion.AssertEquals ("#04", 1, list.Count);
  195. Socket.Select (list, empty, empty, -1);
  196. Assertion.AssertEquals ("#05", 0, empty.Count);
  197. Assertion.AssertEquals ("#06", 1, list.Count);
  198. } finally {
  199. if (acc != null)
  200. acc.Close ();
  201. srv.Close ();
  202. }
  203. }
  204. static Socket CreateServer ()
  205. {
  206. Socket sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  207. sock.Bind (new IPEndPoint (IPAddress.Loopback, 0));
  208. sock.Listen (1);
  209. return sock;
  210. }
  211. class ClientSocket {
  212. Socket sock;
  213. EndPoint ep;
  214. public ClientSocket (EndPoint ep)
  215. {
  216. this.ep = ep;
  217. sock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  218. }
  219. public void ConnectSleepClose ()
  220. {
  221. sock.Connect (ep);
  222. Thread.Sleep (2000);
  223. sock.Close ();
  224. }
  225. public void Write ()
  226. {
  227. byte [] b = new byte [10];
  228. sock.Send (b);
  229. }
  230. }
  231. byte[] buf = new byte[100];
  232. [Test]
  233. [ExpectedException (typeof (ObjectDisposedException))]
  234. public void Disposed1 ()
  235. {
  236. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  237. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  238. s.Close();
  239. s.ReceiveFrom (buf, ref ep);
  240. }
  241. [Test]
  242. [ExpectedException (typeof (ObjectDisposedException))]
  243. public void Disposed2 ()
  244. {
  245. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  246. s.Close();
  247. s.Blocking = true;
  248. }
  249. [Test]
  250. [ExpectedException (typeof (ObjectDisposedException))]
  251. public void Disposed3 ()
  252. {
  253. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  254. s.Close();
  255. s.GetSocketOption (0, 0);
  256. }
  257. [Test]
  258. [ExpectedException (typeof (ObjectDisposedException))]
  259. public void Disposed4 ()
  260. {
  261. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  262. s.Close();
  263. s.GetSocketOption (0, 0, null);
  264. }
  265. [Test]
  266. [ExpectedException (typeof (ObjectDisposedException))]
  267. public void Disposed5 ()
  268. {
  269. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  270. s.Close();
  271. s.GetSocketOption (0, 0, 0);
  272. }
  273. [Test]
  274. [ExpectedException (typeof (ObjectDisposedException))]
  275. public void Disposed6 ()
  276. {
  277. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  278. s.Close();
  279. s.Listen (5);
  280. }
  281. [Test]
  282. [ExpectedException (typeof (ObjectDisposedException))]
  283. public void Disposed7 ()
  284. {
  285. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  286. s.Close();
  287. s.Poll (100, 0);
  288. }
  289. [Test]
  290. [ExpectedException (typeof (ObjectDisposedException))]
  291. public void Disposed8 ()
  292. {
  293. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  294. s.Close();
  295. s.Receive (buf);
  296. }
  297. [Test]
  298. [ExpectedException (typeof (ObjectDisposedException))]
  299. public void Disposed9 ()
  300. {
  301. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  302. s.Close();
  303. s.Receive (buf, 0);
  304. }
  305. [Test]
  306. [ExpectedException (typeof (ObjectDisposedException))]
  307. public void Disposed10 ()
  308. {
  309. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  310. s.Close();
  311. s.Receive (buf, 10, 0);
  312. }
  313. [Test]
  314. [ExpectedException (typeof (ObjectDisposedException))]
  315. public void Disposed11 ()
  316. {
  317. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  318. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  319. s.Close();
  320. s.Receive (buf, 0, 10, 0);
  321. }
  322. [Test]
  323. [ExpectedException (typeof (ObjectDisposedException))]
  324. public void Disposed12 ()
  325. {
  326. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  327. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  328. s.Close();
  329. s.ReceiveFrom (buf, 0, ref ep);
  330. }
  331. [Test]
  332. [ExpectedException (typeof (ObjectDisposedException))]
  333. public void Disposed13 ()
  334. {
  335. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  336. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  337. s.Close();
  338. s.ReceiveFrom (buf, 10, 0, ref ep);
  339. }
  340. [Test]
  341. [ExpectedException (typeof (ObjectDisposedException))]
  342. public void Disposed14 ()
  343. {
  344. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  345. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  346. s.Close();
  347. s.ReceiveFrom (buf, 0, 10, 0, ref ep);
  348. }
  349. [Test]
  350. [ExpectedException (typeof (ObjectDisposedException))]
  351. public void Disposed15 ()
  352. {
  353. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  354. s.Close();
  355. s.Send (buf);
  356. }
  357. [Test]
  358. [ExpectedException (typeof (ObjectDisposedException))]
  359. public void Disposed16 ()
  360. {
  361. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  362. s.Close();
  363. s.Send (buf, 0);
  364. }
  365. [Test]
  366. [ExpectedException (typeof (ObjectDisposedException))]
  367. public void Disposed17 ()
  368. {
  369. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  370. s.Close();
  371. s.Send (buf, 10, 0);
  372. }
  373. [Test]
  374. [ExpectedException (typeof (ObjectDisposedException))]
  375. public void Disposed18 ()
  376. {
  377. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  378. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  379. s.Close();
  380. s.Send (buf, 0, 10, 0);
  381. }
  382. [Test]
  383. [ExpectedException (typeof (ObjectDisposedException))]
  384. public void Disposed19 ()
  385. {
  386. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  387. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  388. s.Close();
  389. s.SendTo (buf, 0, ep);
  390. }
  391. [Test]
  392. [ExpectedException (typeof (ObjectDisposedException))]
  393. public void Disposed20 ()
  394. {
  395. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  396. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  397. s.Close();
  398. s.SendTo (buf, 10, 0, ep);
  399. }
  400. [Test]
  401. [ExpectedException (typeof (ObjectDisposedException))]
  402. public void Disposed21 ()
  403. {
  404. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  405. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  406. s.Close();
  407. s.SendTo (buf, 0, 10, 0, ep);
  408. }
  409. [Test]
  410. [ExpectedException (typeof (ObjectDisposedException))]
  411. public void Disposed22 ()
  412. {
  413. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  414. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  415. s.Close();
  416. s.SendTo (buf, ep);
  417. }
  418. [Test]
  419. [ExpectedException (typeof (ObjectDisposedException))]
  420. public void Disposed23 ()
  421. {
  422. Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
  423. EndPoint ep = new IPEndPoint (IPAddress.Any, 31337);
  424. s.Close();
  425. s.Shutdown (0);
  426. }
  427. [Test]
  428. public void GetHashCodeTest ()
  429. {
  430. Socket server = new Socket (AddressFamily.InterNetwork,
  431. SocketType.Stream, ProtocolType.Tcp);
  432. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  433. 9010);
  434. server.Bind (ep);
  435. server.Listen (1);
  436. Socket client = new Socket (AddressFamily.InterNetwork,
  437. SocketType.Stream, ProtocolType.Tcp);
  438. int hashcodeA = client.GetHashCode ();
  439. client.Connect (ep);
  440. int hashcodeB = client.GetHashCode ();
  441. Assert.AreEqual (hashcodeA, hashcodeB, "#1");
  442. client.Close ();
  443. int hashcodeC = client.GetHashCode ();
  444. #if NET_2_0
  445. Assert.AreEqual (hashcodeB, hashcodeC, "#2");
  446. #else
  447. Assert.IsFalse (hashcodeB == hashcodeC, "#2");
  448. #endif
  449. server.Close ();
  450. }
  451. static ManualResetEvent SocketError_event = new ManualResetEvent (false);
  452. private static void SocketError_callback (IAsyncResult ar)
  453. {
  454. Socket sock = (Socket)ar.AsyncState;
  455. if(sock.Connected) {
  456. sock.EndConnect (ar);
  457. }
  458. SocketError_event.Set ();
  459. }
  460. [Test]
  461. public void SocketError ()
  462. {
  463. Socket sock = new Socket (AddressFamily.InterNetwork,
  464. SocketType.Stream,
  465. ProtocolType.Tcp);
  466. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  467. BogusPort);
  468. SocketError_event.Reset ();
  469. sock.Blocking = false;
  470. sock.BeginConnect (ep, new AsyncCallback(SocketError_callback),
  471. sock);
  472. if (SocketError_event.WaitOne (2000, false) == false) {
  473. Assert.Fail ("SocketError wait timed out");
  474. }
  475. Assertion.AssertEquals ("SocketError #1", false,
  476. sock.Connected);
  477. int error;
  478. error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
  479. Assertion.AssertEquals ("SocketError #2", 10061,
  480. error);
  481. error = (int)sock.GetSocketOption (SocketOptionLevel.Socket, SocketOptionName.Error);
  482. Assertion.AssertEquals ("SocketError #3", 10061,
  483. error);
  484. sock.Close ();
  485. }
  486. #if NET_2_0
  487. [Test]
  488. public void SocketInformationCtor ()
  489. {
  490. }
  491. [Test]
  492. public void DontFragmentDefaultTcp ()
  493. {
  494. Socket sock = new Socket (AddressFamily.InterNetwork,
  495. SocketType.Stream,
  496. ProtocolType.Tcp);
  497. Assertion.AssertEquals ("DontFragmentDefaultTcp",
  498. false, sock.DontFragment);
  499. sock.Close ();
  500. }
  501. [Test]
  502. [Category ("NotOnMac")] // DontFragment doesn't work on Mac
  503. public void DontFragmentChangeTcp ()
  504. {
  505. Socket sock = new Socket (AddressFamily.InterNetwork,
  506. SocketType.Stream,
  507. ProtocolType.Tcp);
  508. sock.DontFragment = true;
  509. Assertion.AssertEquals ("DontFragmentChangeTcp",
  510. true, sock.DontFragment);
  511. sock.Close ();
  512. }
  513. [Test]
  514. public void DontFragmentDefaultUdp ()
  515. {
  516. Socket sock = new Socket (AddressFamily.InterNetwork,
  517. SocketType.Dgram,
  518. ProtocolType.Udp);
  519. Assertion.AssertEquals ("DontFragmentDefaultUdp",
  520. false, sock.DontFragment);
  521. sock.Close ();
  522. }
  523. [Test]
  524. [Category ("NotOnMac")] // DontFragment doesn't work on Mac
  525. public void DontFragmentChangeUdp ()
  526. {
  527. Socket sock = new Socket (AddressFamily.InterNetwork,
  528. SocketType.Dgram,
  529. ProtocolType.Udp);
  530. sock.DontFragment = true;
  531. Assertion.AssertEquals ("DontFragmentChangeUdp",
  532. true, sock.DontFragment);
  533. sock.Close ();
  534. }
  535. [Test]
  536. [ExpectedException (typeof(ObjectDisposedException))]
  537. public void DontFragmentClosed ()
  538. {
  539. Socket sock = new Socket (AddressFamily.InterNetwork,
  540. SocketType.Stream,
  541. ProtocolType.Tcp);
  542. sock.Close ();
  543. bool val = sock.DontFragment;
  544. }
  545. [Test]
  546. [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
  547. public void DontFragment ()
  548. {
  549. Socket sock = new Socket (AddressFamily.NetBios,
  550. SocketType.Seqpacket,
  551. ProtocolType.Unspecified);
  552. try {
  553. sock.DontFragment = true;
  554. Assert.Fail ("DontFragment #1");
  555. } catch (NotSupportedException) {
  556. } catch {
  557. Assert.Fail ("DontFragment #2");
  558. } finally {
  559. sock.Close ();
  560. }
  561. }
  562. [Test]
  563. public void EnableBroadcastDefaultTcp ()
  564. {
  565. Socket sock = new Socket (AddressFamily.InterNetwork,
  566. SocketType.Stream,
  567. ProtocolType.Tcp);
  568. try {
  569. bool value = sock.EnableBroadcast;
  570. Assert.Fail ("EnableBroadcastDefaultTcp #1");
  571. } catch (SocketException ex) {
  572. Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastDefaultTcp #2");
  573. } catch {
  574. Assert.Fail ("EnableBroadcastDefaultTcp #2");
  575. } finally {
  576. sock.Close ();
  577. }
  578. }
  579. [Test]
  580. public void EnableBroadcastDefaultUdp ()
  581. {
  582. Socket sock = new Socket (AddressFamily.InterNetwork,
  583. SocketType.Dgram,
  584. ProtocolType.Udp);
  585. Assertion.AssertEquals ("EnableBroadcastDefaultUdp",
  586. false, sock.EnableBroadcast);
  587. sock.Close ();
  588. }
  589. [Test]
  590. public void EnableBroadcastChangeTcp ()
  591. {
  592. Socket sock = new Socket (AddressFamily.InterNetwork,
  593. SocketType.Stream,
  594. ProtocolType.Tcp);
  595. try {
  596. sock.EnableBroadcast = true;
  597. Assert.Fail ("EnableBroadcastChangeTcp #1");
  598. } catch (SocketException ex) {
  599. Assert.AreEqual (10042, ex.ErrorCode, "EnableBroadcastChangeTcp #2");
  600. } catch {
  601. Assert.Fail ("EnableBroadcastChangeTcp #2");
  602. } finally {
  603. sock.Close ();
  604. }
  605. }
  606. [Test]
  607. public void EnableBroadcastChangeUdp ()
  608. {
  609. Socket sock = new Socket (AddressFamily.InterNetwork,
  610. SocketType.Dgram,
  611. ProtocolType.Udp);
  612. sock.EnableBroadcast = true;
  613. Assertion.AssertEquals ("EnableBroadcastChangeUdp",
  614. true, sock.EnableBroadcast);
  615. sock.Close ();
  616. }
  617. [Test]
  618. [ExpectedException (typeof(ObjectDisposedException))]
  619. public void EnableBroadcastClosed ()
  620. {
  621. Socket sock = new Socket (AddressFamily.InterNetwork,
  622. SocketType.Dgram,
  623. ProtocolType.Udp);
  624. sock.Close ();
  625. bool val = sock.EnableBroadcast;
  626. }
  627. /* Can't test the default for ExclusiveAddressUse as
  628. * it's different on different versions and service
  629. * packs of windows
  630. */
  631. [Test]
  632. [Category ("NotWorking")] // Not supported on Linux
  633. public void ExclusiveAddressUseUnbound ()
  634. {
  635. Socket sock = new Socket (AddressFamily.InterNetwork,
  636. SocketType.Stream,
  637. ProtocolType.Tcp);
  638. sock.ExclusiveAddressUse = true;
  639. Assertion.AssertEquals ("ExclusiveAddressUseUnbound",
  640. true,
  641. sock.ExclusiveAddressUse);
  642. sock.Close ();
  643. }
  644. [Test]
  645. [ExpectedException (typeof(InvalidOperationException))]
  646. [Category ("NotWorking")] // Not supported on Linux
  647. public void ExclusiveAddressUseBound ()
  648. {
  649. Socket sock = new Socket (AddressFamily.InterNetwork,
  650. SocketType.Stream,
  651. ProtocolType.Tcp);
  652. sock.Bind (new IPEndPoint (IPAddress.Any, 1235));
  653. sock.ExclusiveAddressUse = true;
  654. sock.Close ();
  655. }
  656. [Test]
  657. [ExpectedException (typeof(ObjectDisposedException))]
  658. public void ExclusiveAddressUseClosed ()
  659. {
  660. Socket sock = new Socket (AddressFamily.InterNetwork,
  661. SocketType.Stream,
  662. ProtocolType.Tcp);
  663. sock.Close ();
  664. bool val = sock.ExclusiveAddressUse;
  665. }
  666. [Test]
  667. public void IsBoundTcp ()
  668. {
  669. Socket sock = new Socket (AddressFamily.InterNetwork,
  670. SocketType.Stream,
  671. ProtocolType.Tcp);
  672. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  673. BogusPort);
  674. Assertion.AssertEquals ("IsBoundTcp #1", false,
  675. sock.IsBound);
  676. sock.Bind (ep);
  677. Assertion.AssertEquals ("IsBoundTcp #2", true,
  678. sock.IsBound);
  679. sock.Listen (1);
  680. Socket sock2 = new Socket (AddressFamily.InterNetwork,
  681. SocketType.Stream,
  682. ProtocolType.Tcp);
  683. Assertion.AssertEquals ("IsBoundTcp #3", false,
  684. sock2.IsBound);
  685. sock2.Connect (ep);
  686. Assertion.AssertEquals ("IsBoundTcp #4", true,
  687. sock2.IsBound);
  688. sock2.Close ();
  689. Assertion.AssertEquals ("IsBoundTcp #5", true,
  690. sock2.IsBound);
  691. sock.Close ();
  692. Assertion.AssertEquals ("IsBoundTcp #6", true,
  693. sock.IsBound);
  694. }
  695. [Test]
  696. public void IsBoundUdp ()
  697. {
  698. Socket sock = new Socket (AddressFamily.InterNetwork,
  699. SocketType.Dgram,
  700. ProtocolType.Udp);
  701. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  702. BogusPort);
  703. Assertion.AssertEquals ("IsBoundUdp #1", false,
  704. sock.IsBound);
  705. sock.Bind (ep);
  706. Assertion.AssertEquals ("IsBoundUdp #2", true,
  707. sock.IsBound);
  708. sock.Close ();
  709. Assertion.AssertEquals ("IsBoundUdp #3", true,
  710. sock.IsBound);
  711. sock = new Socket (AddressFamily.InterNetwork,
  712. SocketType.Dgram,
  713. ProtocolType.Udp);
  714. Assertion.AssertEquals ("IsBoundUdp #4", false,
  715. sock.IsBound);
  716. sock.Connect (ep);
  717. Assertion.AssertEquals ("IsBoundUdp #5", true,
  718. sock.IsBound);
  719. sock.Close ();
  720. Assertion.AssertEquals ("IsBoundUdp #6", true,
  721. sock.IsBound);
  722. }
  723. [Test]
  724. /* Should not throw an exception */
  725. public void IsBoundClosed ()
  726. {
  727. Socket sock = new Socket (AddressFamily.InterNetwork,
  728. SocketType.Stream,
  729. ProtocolType.Tcp);
  730. sock.Close ();
  731. bool val = sock.IsBound;
  732. }
  733. /* Nothing much to test for LingerState */
  734. [Test]
  735. public void MulticastLoopbackDefaultTcp ()
  736. {
  737. Socket sock = new Socket (AddressFamily.InterNetwork,
  738. SocketType.Stream,
  739. ProtocolType.Tcp);
  740. try {
  741. bool value = sock.MulticastLoopback;
  742. Assert.Fail ("MulticastLoopbackDefaultTcp #1");
  743. } catch (SocketException ex) {
  744. Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackDefaultTcp #2");
  745. } catch {
  746. Assert.Fail ("MulticastLoopbackDefaultTcp #2");
  747. } finally {
  748. sock.Close ();
  749. }
  750. }
  751. [Test]
  752. public void MulticastLoopbackChangeTcp ()
  753. {
  754. Socket sock = new Socket (AddressFamily.InterNetwork,
  755. SocketType.Stream,
  756. ProtocolType.Tcp);
  757. try {
  758. sock.MulticastLoopback = false;
  759. Assert.Fail ("MulticastLoopbackChangeTcp #1");
  760. } catch (SocketException ex) {
  761. Assert.AreEqual (10042, ex.ErrorCode, "MulticastLoopbackChangeTcp #2");
  762. } catch {
  763. Assert.Fail ("MulticastLoopbackChangeTcp #2");
  764. } finally {
  765. sock.Close ();
  766. }
  767. }
  768. [Test]
  769. public void MulticastLoopbackDefaultUdp ()
  770. {
  771. Socket sock = new Socket (AddressFamily.InterNetwork,
  772. SocketType.Dgram,
  773. ProtocolType.Udp);
  774. Assertion.AssertEquals ("MulticastLoopbackDefaultUdp",
  775. true, sock.MulticastLoopback);
  776. sock.Close ();
  777. }
  778. [Test]
  779. public void MulticastLoopbackChangeUdp ()
  780. {
  781. Socket sock = new Socket (AddressFamily.InterNetwork,
  782. SocketType.Dgram,
  783. ProtocolType.Udp);
  784. sock.MulticastLoopback = false;
  785. Assertion.AssertEquals ("MulticastLoopbackChangeUdp",
  786. false, sock.MulticastLoopback);
  787. sock.Close ();
  788. }
  789. [Test]
  790. [ExpectedException (typeof(ObjectDisposedException))]
  791. public void MulticastLoopbackClosed ()
  792. {
  793. Socket sock = new Socket (AddressFamily.InterNetwork,
  794. SocketType.Stream,
  795. ProtocolType.Tcp);
  796. sock.Close ();
  797. bool val = sock.MulticastLoopback;
  798. }
  799. /* OSSupportsIPv6 depends on the environment */
  800. [Test]
  801. [Category("NotWorking")] // We have different defaults for perf reasons
  802. public void ReceiveBufferSizeDefault ()
  803. {
  804. Socket sock = new Socket (AddressFamily.InterNetwork,
  805. SocketType.Stream,
  806. ProtocolType.Tcp);
  807. Assertion.AssertEquals ("ReceiveBufferSizeDefault",
  808. 8192, sock.ReceiveBufferSize);
  809. sock.Close ();
  810. }
  811. [Test]
  812. [Category("NotWorking")] // We have different defaults for perf reasons
  813. public void ReceiveBufferSizeDefaultUdp ()
  814. {
  815. Socket sock = new Socket (AddressFamily.InterNetwork,
  816. SocketType.Dgram,
  817. ProtocolType.Udp);
  818. Assertion.AssertEquals ("ReceiveBufferSizeDefaultUdp",
  819. 8192, sock.ReceiveBufferSize);
  820. sock.Close ();
  821. }
  822. [Test]
  823. public void ReceiveBufferSizeChange ()
  824. {
  825. Socket sock = new Socket (AddressFamily.InterNetwork,
  826. SocketType.Stream,
  827. ProtocolType.Tcp);
  828. sock.ReceiveBufferSize = 16384;
  829. Assertion.AssertEquals ("ReceiveBufferSizeChange",
  830. 16384, sock.ReceiveBufferSize);
  831. sock.Close ();
  832. }
  833. [Test]
  834. [ExpectedException (typeof(ObjectDisposedException))]
  835. public void ReceiveBufferSizeClosed ()
  836. {
  837. Socket sock = new Socket (AddressFamily.InterNetwork,
  838. SocketType.Stream,
  839. ProtocolType.Tcp);
  840. sock.Close ();
  841. int val = sock.ReceiveBufferSize;
  842. }
  843. [Test]
  844. [Category("NotWorking")] // We have different defaults for perf reasons
  845. public void SendBufferSizeDefault ()
  846. {
  847. Socket sock = new Socket (AddressFamily.InterNetwork,
  848. SocketType.Stream,
  849. ProtocolType.Tcp);
  850. Assertion.AssertEquals ("SendBufferSizeDefault",
  851. 8192, sock.SendBufferSize);
  852. sock.Close ();
  853. }
  854. [Test]
  855. [Category("NotWorking")] // We have different defaults for perf reasons
  856. public void SendBufferSizeDefaultUdp ()
  857. {
  858. Socket sock = new Socket (AddressFamily.InterNetwork,
  859. SocketType.Dgram,
  860. ProtocolType.Udp);
  861. Assertion.AssertEquals ("SendBufferSizeDefaultUdp",
  862. 8192, sock.SendBufferSize);
  863. sock.Close ();
  864. }
  865. [Test]
  866. public void SendBufferSizeChange ()
  867. {
  868. Socket sock = new Socket (AddressFamily.InterNetwork,
  869. SocketType.Stream,
  870. ProtocolType.Tcp);
  871. sock.SendBufferSize = 16384;
  872. Assertion.AssertEquals ("SendBufferSizeChange",
  873. 16384, sock.SendBufferSize);
  874. sock.Close ();
  875. }
  876. [Test]
  877. [ExpectedException (typeof(ObjectDisposedException))]
  878. public void SendBufferSizeClosed ()
  879. {
  880. Socket sock = new Socket (AddressFamily.InterNetwork,
  881. SocketType.Stream,
  882. ProtocolType.Tcp);
  883. sock.Close ();
  884. int val = sock.SendBufferSize;
  885. }
  886. /* No test for TTL default as it's platform dependent */
  887. [Test]
  888. public void TtlChange ()
  889. {
  890. Socket sock = new Socket (AddressFamily.InterNetwork,
  891. SocketType.Stream,
  892. ProtocolType.Tcp);
  893. sock.Ttl = 255;
  894. Assertion.AssertEquals ("TtlChange", 255, sock.Ttl);
  895. sock.Close ();
  896. }
  897. [Test]
  898. [Category ("NotOnMac")] // Mac doesn't throw when overflowing the ttl
  899. public void TtlChangeOverflow ()
  900. {
  901. Socket sock = new Socket (AddressFamily.InterNetwork,
  902. SocketType.Stream,
  903. ProtocolType.Tcp);
  904. try {
  905. sock.Ttl = 256;
  906. Assert.Fail ("TtlChangeOverflow #1");
  907. } catch (SocketException ex) {
  908. Assert.AreEqual (10022, ex.ErrorCode,
  909. "TtlChangeOverflow #2");
  910. } catch {
  911. Assert.Fail ("TtlChangeoverflow #3");
  912. } finally {
  913. sock.Close ();
  914. }
  915. }
  916. /* Apparently you can set TTL=0 on the ms runtime!!
  917. try {
  918. sock.Ttl = 0;
  919. Assert.Fail ("TtlChangeOverflow #4");
  920. } catch (SocketException ex) {
  921. Assert.AreEqual (10022, ex.ErrorCode,
  922. "TtlChangeOverflow #5");
  923. } catch {
  924. Assert.Fail ("TtlChangeOverflow #6");
  925. } finally {
  926. sock.Close ();
  927. }
  928. */
  929. [Test]
  930. [ExpectedException (typeof(ObjectDisposedException))]
  931. public void TtlClosed ()
  932. {
  933. Socket sock = new Socket (AddressFamily.InterNetwork,
  934. SocketType.Stream,
  935. ProtocolType.Tcp);
  936. sock.Close ();
  937. int val = sock.Ttl;
  938. }
  939. [Test]
  940. public void UseOnlyOverlappedIODefault ()
  941. {
  942. Socket sock = new Socket (AddressFamily.InterNetwork,
  943. SocketType.Stream,
  944. ProtocolType.Tcp);
  945. Assertion.AssertEquals ("UseOnlyOverlappedIODefault",
  946. false,
  947. sock.UseOnlyOverlappedIO);
  948. sock.Close ();
  949. }
  950. //
  951. // We need this because the Linux kernel in certain configurations
  952. // will end up rounding up the values passed on to the kernel
  953. // for socket send/receive timeouts.
  954. //
  955. int Approximate (int target, int value)
  956. {
  957. int epsilon = 10;
  958. if (value > target-10 && value < target+10)
  959. return target;
  960. return value;
  961. }
  962. [Test]
  963. public void UseOnlyOverlappedIOChange ()
  964. {
  965. Socket sock = new Socket (AddressFamily.InterNetwork,
  966. SocketType.Stream,
  967. ProtocolType.Tcp);
  968. sock.UseOnlyOverlappedIO = true;
  969. Assertion.AssertEquals ("UseOnlyOverlappedIOChange",
  970. true,
  971. sock.UseOnlyOverlappedIO);
  972. sock.Close ();
  973. }
  974. [Test]
  975. /* Should not throw an exception */
  976. public void UseOnlyOverlappedIOClosed ()
  977. {
  978. Socket sock = new Socket (AddressFamily.InterNetwork,
  979. SocketType.Stream,
  980. ProtocolType.Tcp);
  981. sock.Close ();
  982. bool val = sock.UseOnlyOverlappedIO;
  983. }
  984. [Test]
  985. public void SendTimeoutDefault ()
  986. {
  987. Socket sock = new Socket (AddressFamily.InterNetwork,
  988. SocketType.Stream,
  989. ProtocolType.Tcp);
  990. Assertion.AssertEquals ("SendTimeoutDefault",
  991. 0, sock.SendTimeout);
  992. sock.Close ();
  993. }
  994. [Test]
  995. public void SendTimeoutChange ()
  996. {
  997. Socket sock = new Socket (AddressFamily.InterNetwork,
  998. SocketType.Stream,
  999. ProtocolType.Tcp);
  1000. /* Should be rounded up to 500, according to
  1001. * the MSDN docs, but the MS runtime doesn't
  1002. */
  1003. sock.SendTimeout = 50;
  1004. Assertion.AssertEquals ("SendTimeoutChange #1",
  1005. 50, Approximate (50, sock.SendTimeout));
  1006. sock.SendTimeout = 2000;
  1007. Assertion.AssertEquals ("SendTimeoutChange #2",
  1008. 2000, Approximate (2000, sock.SendTimeout));
  1009. sock.SendTimeout = 0;
  1010. Assertion.AssertEquals ("SendTimeoutChange #3",
  1011. 0, Approximate (0, sock.SendTimeout));
  1012. /* Should be the same as setting 0 */
  1013. sock.SendTimeout = -1;
  1014. Assertion.AssertEquals ("SendTimeoutChange #4",
  1015. 0, sock.SendTimeout);
  1016. sock.SendTimeout = 65536;
  1017. Assertion.AssertEquals ("SendTimeoutChange #5",
  1018. 65536, Approximate (65536, sock.SendTimeout));
  1019. try {
  1020. sock.SendTimeout = -2;
  1021. Assert.Fail ("SendTimeoutChange #8");
  1022. } catch (ArgumentOutOfRangeException) {
  1023. } catch {
  1024. Assert.Fail ("SendTimeoutChange #9");
  1025. } finally {
  1026. sock.Close ();
  1027. }
  1028. }
  1029. [Test]
  1030. [ExpectedException (typeof(ObjectDisposedException))]
  1031. public void SendTimeoutClosed ()
  1032. {
  1033. Socket sock = new Socket (AddressFamily.InterNetwork,
  1034. SocketType.Stream,
  1035. ProtocolType.Tcp);
  1036. sock.Close ();
  1037. int val = sock.SendTimeout;
  1038. }
  1039. [Test]
  1040. public void ReceiveTimeoutDefault ()
  1041. {
  1042. Socket sock = new Socket (AddressFamily.InterNetwork,
  1043. SocketType.Stream,
  1044. ProtocolType.Tcp);
  1045. Assertion.AssertEquals ("ReceiveTimeoutDefault",
  1046. 0, sock.ReceiveTimeout);
  1047. sock.Close ();
  1048. }
  1049. [Test]
  1050. public void ReceiveTimeoutChange ()
  1051. {
  1052. Socket sock = new Socket (AddressFamily.InterNetwork,
  1053. SocketType.Stream,
  1054. ProtocolType.Tcp);
  1055. sock.ReceiveTimeout = 50;
  1056. Assertion.AssertEquals ("ReceiveTimeoutChange #1",
  1057. 50, Approximate (50, sock.ReceiveTimeout));
  1058. sock.ReceiveTimeout = 2000;
  1059. Assertion.AssertEquals ("ReceiveTimeoutChange #2",
  1060. 2000, Approximate (2000, sock.ReceiveTimeout));
  1061. sock.ReceiveTimeout = 0;
  1062. Assertion.AssertEquals ("ReceiveTimeoutChange #3",
  1063. 0, sock.ReceiveTimeout);
  1064. /* Should be the same as setting 0 */
  1065. sock.ReceiveTimeout = -1;
  1066. Assertion.AssertEquals ("ReceiveTimeoutChange #4",
  1067. 0, sock.ReceiveTimeout);
  1068. sock.ReceiveTimeout = 65536;
  1069. Assertion.AssertEquals ("ReceiveTimeoutChange #5",
  1070. 65536, Approximate (65536, sock.ReceiveTimeout));
  1071. try {
  1072. sock.ReceiveTimeout = -2;
  1073. Assert.Fail ("ReceiveTimeoutChange #8");
  1074. } catch (ArgumentOutOfRangeException) {
  1075. } catch {
  1076. Assert.Fail ("ReceiveTimeoutChange #9");
  1077. } finally {
  1078. sock.Close ();
  1079. }
  1080. }
  1081. [Test]
  1082. [ExpectedException (typeof(ObjectDisposedException))]
  1083. public void ReceiveTimeoutClosed ()
  1084. {
  1085. Socket sock = new Socket (AddressFamily.InterNetwork,
  1086. SocketType.Stream,
  1087. ProtocolType.Tcp);
  1088. sock.Close ();
  1089. int val = sock.ReceiveTimeout;
  1090. }
  1091. [Test]
  1092. public void NoDelayDefaultTcp ()
  1093. {
  1094. Socket sock = new Socket (AddressFamily.InterNetwork,
  1095. SocketType.Stream,
  1096. ProtocolType.Tcp);
  1097. Assertion.AssertEquals ("NoDelayDefaultTcp", false,
  1098. sock.NoDelay);
  1099. sock.Close ();
  1100. }
  1101. [Test]
  1102. public void NoDelayChangeTcp ()
  1103. {
  1104. Socket sock = new Socket (AddressFamily.InterNetwork,
  1105. SocketType.Stream,
  1106. ProtocolType.Tcp);
  1107. sock.NoDelay = true;
  1108. Assertion.AssertEquals ("NoDelayChangeTcp", true,
  1109. sock.NoDelay);
  1110. sock.Close ();
  1111. }
  1112. [Test]
  1113. public void NoDelayDefaultUdp ()
  1114. {
  1115. Socket sock = new Socket (AddressFamily.InterNetwork,
  1116. SocketType.Dgram,
  1117. ProtocolType.Udp);
  1118. try {
  1119. bool val = sock.NoDelay;
  1120. Assert.Fail ("NoDelayDefaultUdp #1");
  1121. } catch (SocketException ex) {
  1122. Assert.AreEqual (10042, ex.ErrorCode,
  1123. "NoDelayDefaultUdp #2");
  1124. } catch {
  1125. Assert.Fail ("NoDelayDefaultUdp #3");
  1126. } finally {
  1127. sock.Close ();
  1128. }
  1129. }
  1130. [Test]
  1131. public void NoDelayChangeUdp ()
  1132. {
  1133. Socket sock = new Socket (AddressFamily.InterNetwork,
  1134. SocketType.Dgram,
  1135. ProtocolType.Udp);
  1136. try {
  1137. sock.NoDelay = true;
  1138. Assert.Fail ("NoDelayChangeUdp #1");
  1139. } catch (SocketException ex) {
  1140. Assert.AreEqual (10042, ex.ErrorCode,
  1141. "NoDelayChangeUdp #2");
  1142. } catch {
  1143. Assert.Fail ("NoDelayChangeUdp #3");
  1144. } finally {
  1145. sock.Close ();
  1146. }
  1147. }
  1148. [Test]
  1149. [ExpectedException (typeof(ObjectDisposedException))]
  1150. public void NoDelayClosed ()
  1151. {
  1152. Socket sock = new Socket (AddressFamily.InterNetwork,
  1153. SocketType.Stream,
  1154. ProtocolType.Tcp);
  1155. sock.Close ();
  1156. bool val = sock.NoDelay;
  1157. }
  1158. static bool BAAccepted = false;
  1159. static Socket BASocket = null;
  1160. static ManualResetEvent BACalledBack = new ManualResetEvent (false);
  1161. private static void BACallback (IAsyncResult asyncResult)
  1162. {
  1163. Socket sock = (Socket)asyncResult.AsyncState;
  1164. BASocket = sock.EndAccept (asyncResult);
  1165. BAAccepted = true;
  1166. BACalledBack.Set ();
  1167. }
  1168. [Test]
  1169. [ExpectedException (typeof(InvalidOperationException))]
  1170. public void BeginAcceptNotBound ()
  1171. {
  1172. Socket sock = new Socket (AddressFamily.InterNetwork,
  1173. SocketType.Stream,
  1174. ProtocolType.Tcp);
  1175. sock.BeginAccept (BACallback, sock);
  1176. sock.Close ();
  1177. }
  1178. [Test]
  1179. [ExpectedException (typeof(InvalidOperationException))]
  1180. public void BeginAcceptNotListening ()
  1181. {
  1182. Socket sock = new Socket (AddressFamily.InterNetwork,
  1183. SocketType.Stream,
  1184. ProtocolType.Tcp);
  1185. sock.Bind (new IPEndPoint (IPAddress.Any, 1236));
  1186. sock.BeginAccept (BACallback, sock);
  1187. sock.Close ();
  1188. }
  1189. [Test]
  1190. public void BeginAccept ()
  1191. {
  1192. Socket sock = new Socket (AddressFamily.InterNetwork,
  1193. SocketType.Stream,
  1194. ProtocolType.Tcp);
  1195. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1196. 1237);
  1197. sock.Bind (ep);
  1198. sock.Listen (1);
  1199. BACalledBack.Reset ();
  1200. sock.BeginAccept (BACallback, sock);
  1201. Socket conn = new Socket (AddressFamily.InterNetwork,
  1202. SocketType.Stream,
  1203. ProtocolType.Tcp);
  1204. conn.Connect (ep);
  1205. if (BACalledBack.WaitOne (2000, false) == false) {
  1206. Assert.Fail ("BeginAccept wait timed out");
  1207. }
  1208. Assertion.AssertEquals ("BeginAccept #1", true,
  1209. BAAccepted);
  1210. Assertion.AssertEquals ("BeginAccept #2", true,
  1211. BASocket.Connected);
  1212. Assertion.AssertEquals ("BeginAccept #3", false,
  1213. sock.Connected);
  1214. Assertion.AssertEquals ("BeginAccept #4", true,
  1215. conn.Connected);
  1216. BASocket.Close ();
  1217. conn.Close ();
  1218. sock.Close ();
  1219. }
  1220. [Test]
  1221. [ExpectedException (typeof(ObjectDisposedException))]
  1222. public void BeginAcceptClosed ()
  1223. {
  1224. Socket sock = new Socket (AddressFamily.InterNetwork,
  1225. SocketType.Stream,
  1226. ProtocolType.Tcp);
  1227. sock.Close ();
  1228. sock.BeginAccept (BACallback, sock);
  1229. }
  1230. static bool BADAccepted = false;
  1231. static Socket BADSocket = null;
  1232. static byte[] BADBytes;
  1233. static int BADByteCount;
  1234. static ManualResetEvent BADCalledBack = new ManualResetEvent (false);
  1235. private static void BADCallback (IAsyncResult asyncResult)
  1236. {
  1237. Socket sock = (Socket)asyncResult.AsyncState;
  1238. BADSocket = sock.EndAccept (out BADBytes,
  1239. out BADByteCount,
  1240. asyncResult);
  1241. BADAccepted = true;
  1242. BADCalledBack.Set ();
  1243. }
  1244. [Test]
  1245. public void BeginAcceptData ()
  1246. {
  1247. Socket sock = new Socket (AddressFamily.InterNetwork,
  1248. SocketType.Stream,
  1249. ProtocolType.Tcp);
  1250. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1251. 1238);
  1252. sock.Bind (ep);
  1253. sock.Listen (1);
  1254. BADCalledBack.Reset ();
  1255. sock.BeginAccept (256, BADCallback, sock);
  1256. Socket conn = new Socket (AddressFamily.InterNetwork,
  1257. SocketType.Stream,
  1258. ProtocolType.Tcp);
  1259. byte[] send_bytes = new byte[] {10, 11, 12, 13};
  1260. conn.Connect (ep);
  1261. conn.Send (send_bytes);
  1262. if (BADCalledBack.WaitOne (2000, false) == false) {
  1263. Assert.Fail ("BeginAcceptData wait timed out");
  1264. }
  1265. Assertion.AssertEquals ("BeginAcceptData #1", true,
  1266. BADAccepted);
  1267. Assertion.AssertEquals ("BeginAcceptData #2", true,
  1268. BADSocket.Connected);
  1269. Assertion.AssertEquals ("BeginAcceptData #3", false,
  1270. sock.Connected);
  1271. Assertion.AssertEquals ("BeginAcceptData #4", true,
  1272. conn.Connected);
  1273. Assertion.AssertEquals ("BeginAcceptData #5",
  1274. send_bytes.Length,
  1275. BADByteCount);
  1276. /* The MS runtime gives the returned data in a
  1277. * much bigger array. TODO: investigate
  1278. * whether it the size correlates to the first
  1279. * parameter in BeginAccept()
  1280. */
  1281. Assert.IsFalse (BADBytes.Length == send_bytes.Length,
  1282. "BeginAcceptData #6");
  1283. for(int i = 0; i < send_bytes.Length; i++) {
  1284. Assertion.AssertEquals ("BeginAcceptData #" + (i+7).ToString (), send_bytes[i], BADBytes[i]);
  1285. }
  1286. BADSocket.Close ();
  1287. conn.Close ();
  1288. sock.Close ();
  1289. }
  1290. [Test]
  1291. [ExpectedException (typeof(ObjectDisposedException))]
  1292. public void BeginAcceptDataClosed ()
  1293. {
  1294. Socket sock = new Socket (AddressFamily.InterNetwork,
  1295. SocketType.Stream,
  1296. ProtocolType.Tcp);
  1297. sock.Close ();
  1298. sock.BeginAccept (256, BADCallback, sock);
  1299. }
  1300. [Test]
  1301. public void BeginAcceptSocketUdp ()
  1302. {
  1303. Socket sock = new Socket (AddressFamily.InterNetwork,
  1304. SocketType.Stream,
  1305. ProtocolType.Tcp);
  1306. Socket acc = new Socket (AddressFamily.InterNetwork,
  1307. SocketType.Dgram,
  1308. ProtocolType.Udp);
  1309. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1310. 1239);
  1311. sock.Bind (ep);
  1312. sock.Listen (1);
  1313. try {
  1314. sock.BeginAccept (acc, 256, BADCallback, sock);
  1315. Assert.Fail ("BeginAcceptSocketUdp #1");
  1316. } catch (SocketException ex) {
  1317. Assertion.AssertEquals ("BeginAcceptSocketUdp #2", 10022, ex.ErrorCode);
  1318. } catch {
  1319. Assert.Fail ("BeginAcceptSocketUdp #3");
  1320. } finally {
  1321. acc.Close ();
  1322. sock.Close ();
  1323. }
  1324. }
  1325. [Test]
  1326. public void BeginAcceptSocketBound ()
  1327. {
  1328. Socket sock = new Socket (AddressFamily.InterNetwork,
  1329. SocketType.Stream,
  1330. ProtocolType.Tcp);
  1331. Socket acc = new Socket (AddressFamily.InterNetwork,
  1332. SocketType.Stream,
  1333. ProtocolType.Tcp);
  1334. IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
  1335. 1240);
  1336. IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
  1337. 1241);
  1338. sock.Bind (ep1);
  1339. sock.Listen (1);
  1340. acc.Bind (ep2);
  1341. try {
  1342. sock.BeginAccept (acc, 256, BADCallback, sock);
  1343. Assert.Fail ("BeginAcceptSocketBound #1");
  1344. } catch (InvalidOperationException) {
  1345. } catch {
  1346. Assert.Fail ("BeginAcceptSocketBound #2");
  1347. } finally {
  1348. acc.Close ();
  1349. sock.Close ();
  1350. }
  1351. }
  1352. [Test]
  1353. public void BeginAcceptSocket ()
  1354. {
  1355. Socket sock = new Socket (AddressFamily.InterNetwork,
  1356. SocketType.Stream,
  1357. ProtocolType.Tcp);
  1358. Socket acc = new Socket (AddressFamily.InterNetwork,
  1359. SocketType.Stream,
  1360. ProtocolType.Tcp);
  1361. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1362. 1242);
  1363. sock.Bind (ep);
  1364. sock.Listen (1);
  1365. BADCalledBack.Reset ();
  1366. sock.BeginAccept (acc, 256, BADCallback, sock);
  1367. Socket conn = new Socket (AddressFamily.InterNetwork,
  1368. SocketType.Stream,
  1369. ProtocolType.Tcp);
  1370. byte[] send_bytes = new byte[] {10, 11, 12, 13};
  1371. conn.Connect (ep);
  1372. conn.Send (send_bytes);
  1373. if (BADCalledBack.WaitOne (2000, false) == false) {
  1374. Assert.Fail ("BeginAcceptSocket wait timed out");
  1375. }
  1376. Assertion.AssertEquals ("BeginAcceptSocket #1", true,
  1377. BADAccepted);
  1378. Assertion.AssertEquals ("BeginAcceptSocket #2", true,
  1379. BADSocket.Connected);
  1380. Assertion.AssertEquals ("BeginAcceptSocket #3", false,
  1381. sock.Connected);
  1382. Assertion.AssertEquals ("BeginAcceptSocket #4", true,
  1383. conn.Connected);
  1384. Assertion.AssertEquals ("BeginAcceptSocket #5",
  1385. send_bytes.Length,
  1386. BADByteCount);
  1387. Assertion.AssertEquals ("BeginAcceptSocket #6",
  1388. AddressFamily.InterNetwork,
  1389. acc.AddressFamily);
  1390. Assertion.AssertEquals ("BeginAcceptSocket #7",
  1391. SocketType.Stream,
  1392. acc.SocketType);
  1393. Assertion.AssertEquals ("BeginAcceptSocket #8",
  1394. ProtocolType.Tcp,
  1395. acc.ProtocolType);
  1396. Assertion.AssertEquals ("BeginAcceptSocket #9",
  1397. conn.LocalEndPoint,
  1398. acc.RemoteEndPoint);
  1399. /* The MS runtime gives the returned data in a
  1400. * much bigger array. TODO: investigate
  1401. * whether it the size correlates to the first
  1402. * parameter in BeginAccept()
  1403. */
  1404. Assert.IsFalse (BADBytes.Length == send_bytes.Length,
  1405. "BeginAcceptSocket #10");
  1406. for(int i = 0; i < send_bytes.Length; i++) {
  1407. Assertion.AssertEquals ("BeginAcceptSocket #" + (i+11).ToString (), send_bytes[i], BADBytes[i]);
  1408. }
  1409. BADSocket.Close ();
  1410. conn.Close ();
  1411. acc.Close ();
  1412. sock.Close ();
  1413. }
  1414. [Test]
  1415. public void BeginAcceptSocketClosed ()
  1416. {
  1417. Socket sock = new Socket (AddressFamily.InterNetwork,
  1418. SocketType.Stream,
  1419. ProtocolType.Tcp);
  1420. Socket acc = new Socket (AddressFamily.InterNetwork,
  1421. SocketType.Stream,
  1422. ProtocolType.Tcp);
  1423. sock.Close ();
  1424. try {
  1425. sock.BeginAccept (acc, 256, BADCallback, null);
  1426. Assert.Fail ("BeginAcceptSocketClosed #1");
  1427. } catch (ObjectDisposedException) {
  1428. } catch {
  1429. Assert.Fail ("BeginAcceptSocketClosed #2");
  1430. } finally {
  1431. acc.Close ();
  1432. }
  1433. }
  1434. [Test]
  1435. public void BeginAcceptSocketAccClosed ()
  1436. {
  1437. Socket sock = new Socket (AddressFamily.InterNetwork,
  1438. SocketType.Stream,
  1439. ProtocolType.Tcp);
  1440. Socket acc = new Socket (AddressFamily.InterNetwork,
  1441. SocketType.Stream,
  1442. ProtocolType.Tcp);
  1443. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1444. 1243);
  1445. sock.Bind (ep);
  1446. sock.Listen (1);
  1447. acc.Close ();
  1448. BADCalledBack.Reset ();
  1449. try {
  1450. sock.BeginAccept (acc, 256, BADCallback, null);
  1451. Assert.Fail ("BeginAcceptSocketAccClosed #1");
  1452. } catch (ObjectDisposedException) {
  1453. } catch {
  1454. Assert.Fail ("BeginAcceptSocketAccClosed #2");
  1455. } finally {
  1456. sock.Close ();
  1457. }
  1458. }
  1459. static bool BCConnected = false;
  1460. static ManualResetEvent BCCalledBack = new ManualResetEvent (false);
  1461. private static void BCCallback (IAsyncResult asyncResult)
  1462. {
  1463. Socket sock = (Socket)asyncResult.AsyncState;
  1464. sock.EndConnect (asyncResult);
  1465. BCConnected = true;
  1466. BCCalledBack.Set ();
  1467. }
  1468. [Test]
  1469. public void BeginConnectAddressPort ()
  1470. {
  1471. Socket sock = new Socket (AddressFamily.InterNetwork,
  1472. SocketType.Stream,
  1473. ProtocolType.Tcp);
  1474. Socket listen = new Socket (AddressFamily.InterNetwork,
  1475. SocketType.Stream,
  1476. ProtocolType.Tcp);
  1477. IPAddress ip = IPAddress.Loopback;
  1478. IPEndPoint ep = new IPEndPoint (ip, 1244);
  1479. listen.Bind (ep);
  1480. listen.Listen (1);
  1481. BCCalledBack.Reset ();
  1482. BCConnected = false;
  1483. sock.BeginConnect (ip, 1244, BCCallback, sock);
  1484. if (BCCalledBack.WaitOne (2000, false) == false) {
  1485. Assert.Fail ("BeginConnectAddressPort wait timed out");
  1486. }
  1487. Assertion.AssertEquals ("BeginConnectAddressPort #1",
  1488. true, BCConnected);
  1489. sock.Close ();
  1490. listen.Close ();
  1491. }
  1492. [Test]
  1493. public void BeginConnectAddressPortNull ()
  1494. {
  1495. Socket sock = new Socket (AddressFamily.InterNetwork,
  1496. SocketType.Stream,
  1497. ProtocolType.Tcp);
  1498. IPAddress ip = null;
  1499. try {
  1500. sock.BeginConnect (ip, 1244, BCCallback,
  1501. sock);
  1502. Assert.Fail ("BeginConnectAddressPortNull #1");
  1503. } catch (ArgumentNullException) {
  1504. } catch {
  1505. Assert.Fail ("BeginConnectAddressPortNull #2");
  1506. } finally {
  1507. sock.Close ();
  1508. }
  1509. }
  1510. [Test]
  1511. public void BeginConnectAddressPortListen ()
  1512. {
  1513. Socket sock = new Socket (AddressFamily.InterNetwork,
  1514. SocketType.Stream,
  1515. ProtocolType.Tcp);
  1516. IPAddress ip = IPAddress.Loopback;
  1517. IPEndPoint ep = new IPEndPoint (ip, 1245);
  1518. sock.Bind (ep);
  1519. sock.Listen (1);
  1520. try {
  1521. sock.BeginConnect (ip, 1245, BCCallback, sock);
  1522. Assert.Fail ("BeginConnectAddressPortListen #1");
  1523. } catch (InvalidOperationException) {
  1524. } catch {
  1525. Assert.Fail ("BeginConnectAddressPortListen #2");
  1526. } finally {
  1527. sock.Close ();
  1528. }
  1529. }
  1530. [Test]
  1531. [ExpectedException (typeof(ObjectDisposedException))]
  1532. public void BeginConnectAddressPortClosed ()
  1533. {
  1534. Socket sock = new Socket (AddressFamily.InterNetwork,
  1535. SocketType.Stream,
  1536. ProtocolType.Tcp);
  1537. IPAddress ip = IPAddress.Loopback;
  1538. sock.Close ();
  1539. sock.BeginConnect (ip, 1244, BCCallback, sock);
  1540. }
  1541. [Test]
  1542. [Category ("NotOnMac")] // MacOSX will block attempting to connect to 127.0.0.4 causing the test to fail
  1543. public void BeginConnectMultiple ()
  1544. {
  1545. Socket sock = new Socket (AddressFamily.InterNetwork,
  1546. SocketType.Stream,
  1547. ProtocolType.Tcp);
  1548. Socket listen = new Socket (AddressFamily.InterNetwork,
  1549. SocketType.Stream,
  1550. ProtocolType.Tcp);
  1551. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1552. 1246);
  1553. IPAddress[] ips = new IPAddress[4];
  1554. ips[0] = IPAddress.Parse ("127.0.0.4");
  1555. ips[1] = IPAddress.Parse ("127.0.0.3");
  1556. ips[2] = IPAddress.Parse ("127.0.0.2");
  1557. ips[3] = IPAddress.Parse ("127.0.0.1");
  1558. listen.Bind (ep);
  1559. listen.Listen (1);
  1560. BCCalledBack.Reset ();
  1561. BCConnected = false;
  1562. sock.BeginConnect (ips, 1246, BCCallback, sock);
  1563. /* Longer wait here, because the ms runtime
  1564. * takes a lot longer to not connect
  1565. */
  1566. if (BCCalledBack.WaitOne (10000, false) == false) {
  1567. Assert.Fail ("BeginConnectMultiple wait failed");
  1568. }
  1569. Assertion.AssertEquals ("BeginConnectMultiple #1",
  1570. true, BCConnected);
  1571. Assertion.AssertEquals ("BeginConnectMultiple #2",
  1572. AddressFamily.InterNetwork,
  1573. sock.RemoteEndPoint.AddressFamily);
  1574. IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
  1575. Assertion.AssertEquals ("BeginConnectMultiple #2",
  1576. IPAddress.Loopback,
  1577. remep.Address);
  1578. sock.Close ();
  1579. listen.Close ();
  1580. }
  1581. [Test]
  1582. public void BeginConnectMultipleNull ()
  1583. {
  1584. Socket sock = new Socket (AddressFamily.InterNetwork,
  1585. SocketType.Stream,
  1586. ProtocolType.Tcp);
  1587. IPAddress[] ips = null;
  1588. try {
  1589. sock.BeginConnect (ips, 1246, BCCallback,
  1590. sock);
  1591. Assert.Fail ("BeginConnectMultipleNull #1");
  1592. } catch (ArgumentNullException) {
  1593. } catch {
  1594. Assert.Fail ("BeginConnectMultipleNull #2");
  1595. } finally {
  1596. sock.Close ();
  1597. }
  1598. }
  1599. [Test]
  1600. public void BeginConnectMultipleListen ()
  1601. {
  1602. Socket sock = new Socket (AddressFamily.InterNetwork,
  1603. SocketType.Stream,
  1604. ProtocolType.Tcp);
  1605. IPAddress[] ips = new IPAddress[4];
  1606. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1607. 1247);
  1608. ips[0] = IPAddress.Parse ("127.0.0.4");
  1609. ips[1] = IPAddress.Parse ("127.0.0.3");
  1610. ips[2] = IPAddress.Parse ("127.0.0.2");
  1611. ips[3] = IPAddress.Parse ("127.0.0.1");
  1612. sock.Bind (ep);
  1613. sock.Listen (1);
  1614. try {
  1615. sock.BeginConnect (ips, 1247, BCCallback,
  1616. sock);
  1617. Assert.Fail ("BeginConnectMultipleListen #1");
  1618. } catch (InvalidOperationException) {
  1619. } catch {
  1620. Assert.Fail ("BeginConnectMultipleListen #2");
  1621. } finally {
  1622. sock.Close ();
  1623. }
  1624. }
  1625. [Test]
  1626. [ExpectedException (typeof(ObjectDisposedException))]
  1627. public void BeginConnectMultipleClosed ()
  1628. {
  1629. Socket sock = new Socket (AddressFamily.InterNetwork,
  1630. SocketType.Stream,
  1631. ProtocolType.Tcp);
  1632. IPAddress[] ips = new IPAddress[4];
  1633. ips[0] = IPAddress.Parse ("127.0.0.4");
  1634. ips[1] = IPAddress.Parse ("127.0.0.3");
  1635. ips[2] = IPAddress.Parse ("127.0.0.2");
  1636. ips[3] = IPAddress.Parse ("127.0.0.1");
  1637. sock.Close ();
  1638. sock.BeginConnect (ips, 1247, BCCallback, sock);
  1639. }
  1640. [Test]
  1641. public void BeginConnectHostPortNull ()
  1642. {
  1643. Socket sock = new Socket (AddressFamily.InterNetwork,
  1644. SocketType.Stream,
  1645. ProtocolType.Tcp);
  1646. try {
  1647. sock.BeginConnect ((string)null, 0,
  1648. BCCallback, sock);
  1649. Assert.Fail ("BeginConnectHostPort #1");
  1650. } catch (ArgumentNullException) {
  1651. } catch {
  1652. Assert.Fail ("BeginConnectHostPort #2");
  1653. } finally {
  1654. sock.Close ();
  1655. }
  1656. }
  1657. [Test]
  1658. public void BeginConnectHostPortListen ()
  1659. {
  1660. Socket sock = new Socket (AddressFamily.InterNetwork,
  1661. SocketType.Stream,
  1662. ProtocolType.Tcp);
  1663. IPAddress ip = IPAddress.Loopback;
  1664. IPEndPoint ep = new IPEndPoint (ip, 1248);
  1665. sock.Bind (ep);
  1666. sock.Listen (1);
  1667. try {
  1668. sock.BeginConnect ("localhost", 1248,
  1669. BCCallback, sock);
  1670. Assert.Fail ("BeginConnectHostPortListen #1");
  1671. } catch (InvalidOperationException) {
  1672. } catch {
  1673. Assert.Fail ("BeginConnectHostPortListen #2");
  1674. } finally {
  1675. sock.Close ();
  1676. }
  1677. }
  1678. [Test]
  1679. [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
  1680. public void BeginConnectHostPortNotIP ()
  1681. {
  1682. Socket sock = new Socket (AddressFamily.NetBios,
  1683. SocketType.Seqpacket,
  1684. ProtocolType.Unspecified);
  1685. try {
  1686. sock.BeginConnect ("localhost", 0, BCCallback,
  1687. sock);
  1688. Assert.Fail ("BeginConnectHostPortNotIP #1");
  1689. } catch (NotSupportedException) {
  1690. } catch {
  1691. Assert.Fail ("BeginConnectHostPortNotIP #2");
  1692. } finally {
  1693. sock.Close ();
  1694. }
  1695. }
  1696. [Test]
  1697. [ExpectedException (typeof(ObjectDisposedException))]
  1698. public void BeginConnectHostPortClosed ()
  1699. {
  1700. Socket sock = new Socket (AddressFamily.InterNetwork,
  1701. SocketType.Stream,
  1702. ProtocolType.Tcp);
  1703. sock.Close ();
  1704. sock.BeginConnect ("localhost", 0, BCCallback, sock);
  1705. }
  1706. static bool BDDisconnected = false;
  1707. static ManualResetEvent BDCalledBack = new ManualResetEvent (false);
  1708. private static void BDCallback (IAsyncResult asyncResult)
  1709. {
  1710. Socket sock = (Socket)asyncResult.AsyncState;
  1711. sock.EndDisconnect (asyncResult);
  1712. BDDisconnected = true;
  1713. BDCalledBack.Set ();
  1714. }
  1715. [Test]
  1716. [Category ("NotDotNet")] // "Needs XP or later"
  1717. public void BeginDisconnect ()
  1718. {
  1719. Socket sock = new Socket (AddressFamily.InterNetwork,
  1720. SocketType.Stream,
  1721. ProtocolType.Tcp);
  1722. Socket listen = new Socket (AddressFamily.InterNetwork,
  1723. SocketType.Stream,
  1724. ProtocolType.Tcp);
  1725. IPAddress ip = IPAddress.Loopback;
  1726. IPEndPoint ep = new IPEndPoint (ip, 1254);
  1727. listen.Bind (ep);
  1728. listen.Listen (1);
  1729. sock.Connect (ip, 1254);
  1730. Assertion.AssertEquals ("BeginDisconnect #1", true,
  1731. sock.Connected);
  1732. sock.Shutdown (SocketShutdown.Both);
  1733. BDCalledBack.Reset ();
  1734. BDDisconnected = false;
  1735. sock.BeginDisconnect (false, BDCallback, sock);
  1736. if (BDCalledBack.WaitOne (2000, false) == false) {
  1737. Assert.Fail ("BeginDisconnect wait timed out");
  1738. }
  1739. Assertion.AssertEquals ("BeginDisconnect #2", true,
  1740. BDDisconnected);
  1741. Assertion.AssertEquals ("BeginDisconnect #3", false,
  1742. sock.Connected);
  1743. sock.Close ();
  1744. listen.Close ();
  1745. }
  1746. [Test]
  1747. public void BeginReceiveSocketError ()
  1748. {
  1749. }
  1750. [Test]
  1751. public void BeginReceiveGeneric ()
  1752. {
  1753. }
  1754. [Test]
  1755. public void BeginReceiveGenericSocketError ()
  1756. {
  1757. }
  1758. private static void BSCallback (IAsyncResult asyncResult)
  1759. {
  1760. Socket sock = (Socket)asyncResult.AsyncState;
  1761. sock.EndSend (asyncResult);
  1762. }
  1763. [Test]
  1764. public void BeginSendNotConnected ()
  1765. {
  1766. Socket sock = new Socket (AddressFamily.InterNetwork,
  1767. SocketType.Stream,
  1768. ProtocolType.Tcp);
  1769. byte[] send_bytes = new byte[] {10, 11, 12, 13};
  1770. try {
  1771. sock.BeginSend (send_bytes, 0,
  1772. send_bytes.Length,
  1773. SocketFlags.None, BSCallback,
  1774. sock);
  1775. Assert.Fail ("BeginSendNotConnected #1");
  1776. } catch (SocketException ex) {
  1777. Assertion.AssertEquals ("BeginSendNotConnected #2", 10057, ex.ErrorCode);
  1778. } catch {
  1779. Assert.Fail ("BeginSendNotConnected #3");
  1780. } finally {
  1781. sock.Close ();
  1782. }
  1783. }
  1784. [Test]
  1785. public void BeginSendSocketError ()
  1786. {
  1787. }
  1788. [Test]
  1789. public void BeginSendGeneric ()
  1790. {
  1791. }
  1792. [Test]
  1793. public void BeginSendGenericSocketError ()
  1794. {
  1795. }
  1796. [Test]
  1797. public void BindTwice ()
  1798. {
  1799. Socket sock = new Socket (AddressFamily.InterNetwork,
  1800. SocketType.Stream,
  1801. ProtocolType.Tcp);
  1802. IPEndPoint ep1 = new IPEndPoint (IPAddress.Loopback,
  1803. 1256);
  1804. IPEndPoint ep2 = new IPEndPoint (IPAddress.Loopback,
  1805. 1257);
  1806. sock.Bind (ep1);
  1807. try {
  1808. sock.Bind (ep2);
  1809. Assert.Fail ("BindTwice #1");
  1810. } catch (SocketException ex) {
  1811. Assertion.AssertEquals ("BindTwice #2",
  1812. 10022, ex.ErrorCode);
  1813. } catch {
  1814. Assert.Fail ("BindTwice #3");
  1815. } finally {
  1816. sock.Close ();
  1817. }
  1818. }
  1819. [Test]
  1820. public void Close ()
  1821. {
  1822. Socket sock = new Socket (AddressFamily.InterNetwork,
  1823. SocketType.Stream,
  1824. ProtocolType.Tcp);
  1825. Socket listen = new Socket (AddressFamily.InterNetwork,
  1826. SocketType.Stream,
  1827. ProtocolType.Tcp);
  1828. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1829. 1258);
  1830. listen.Bind (ep);
  1831. listen.Listen (1);
  1832. sock.Connect (ep);
  1833. Assertion.AssertEquals ("Close #1", true,
  1834. sock.Connected);
  1835. sock.Close (2);
  1836. Thread.Sleep (3000);
  1837. Assertion.AssertEquals ("Close #2", false,
  1838. sock.Connected);
  1839. listen.Close ();
  1840. }
  1841. [Test]
  1842. public void ConnectAddressPort ()
  1843. {
  1844. Socket sock = new Socket (AddressFamily.InterNetwork,
  1845. SocketType.Stream,
  1846. ProtocolType.Tcp);
  1847. Socket listen = new Socket (AddressFamily.InterNetwork,
  1848. SocketType.Stream,
  1849. ProtocolType.Tcp);
  1850. IPAddress ip = IPAddress.Loopback;
  1851. IPEndPoint ep = new IPEndPoint (ip, 1249);
  1852. listen.Bind (ep);
  1853. listen.Listen (1);
  1854. sock.Connect (ip, 1249);
  1855. Assertion.AssertEquals ("ConnectAddressPort #1",
  1856. true, sock.Connected);
  1857. sock.Close ();
  1858. listen.Close ();
  1859. }
  1860. [Test]
  1861. public void ConnectAddressPortNull ()
  1862. {
  1863. Socket sock = new Socket (AddressFamily.InterNetwork,
  1864. SocketType.Stream,
  1865. ProtocolType.Tcp);
  1866. IPAddress ip = null;
  1867. try {
  1868. sock.Connect (ip, 1249);
  1869. Assert.Fail ("ConnectAddressPortNull #1");
  1870. } catch (ArgumentNullException) {
  1871. } catch {
  1872. Assert.Fail ("ConnectAddressPortNull #2");
  1873. } finally {
  1874. sock.Close ();
  1875. }
  1876. }
  1877. [Test]
  1878. public void ConnectAddressPortListen ()
  1879. {
  1880. Socket sock = new Socket (AddressFamily.InterNetwork,
  1881. SocketType.Stream,
  1882. ProtocolType.Tcp);
  1883. IPAddress ip = IPAddress.Loopback;
  1884. IPEndPoint ep = new IPEndPoint (ip, 1250);
  1885. sock.Bind (ep);
  1886. sock.Listen (1);
  1887. try {
  1888. sock.Connect (ip, 1250);
  1889. Assert.Fail ("ConnectAddressPortListen #1");
  1890. } catch (InvalidOperationException) {
  1891. } catch {
  1892. Assert.Fail ("ConnectAddressPortListen #2");
  1893. } finally {
  1894. sock.Close ();
  1895. }
  1896. }
  1897. [Test]
  1898. [ExpectedException (typeof(ObjectDisposedException))]
  1899. public void ConnectAddressPortClosed ()
  1900. {
  1901. Socket sock = new Socket (AddressFamily.InterNetwork,
  1902. SocketType.Stream,
  1903. ProtocolType.Tcp);
  1904. IPAddress ip = IPAddress.Loopback;
  1905. sock.Close ();
  1906. sock.Connect (ip, 1250);
  1907. }
  1908. [Test]
  1909. [Category ("NotOnMac")] // MacOSX trashes the fd after the failed connect attempt to 127.0.0.4
  1910. public void ConnectMultiple ()
  1911. {
  1912. Socket sock = new Socket (AddressFamily.InterNetwork,
  1913. SocketType.Stream,
  1914. ProtocolType.Tcp);
  1915. Socket listen = new Socket (AddressFamily.InterNetwork,
  1916. SocketType.Stream,
  1917. ProtocolType.Tcp);
  1918. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1919. 1251);
  1920. IPAddress[] ips = new IPAddress[4];
  1921. ips[0] = IPAddress.Parse ("127.0.0.4");
  1922. ips[1] = IPAddress.Parse ("127.0.0.3");
  1923. ips[2] = IPAddress.Parse ("127.0.0.2");
  1924. ips[3] = IPAddress.Parse ("127.0.0.1");
  1925. listen.Bind (ep);
  1926. listen.Listen (1);
  1927. sock.Connect (ips, 1251);
  1928. Assertion.AssertEquals ("ConnectMultiple #1",
  1929. true, sock.Connected);
  1930. Assertion.AssertEquals ("ConnectMultiple #2",
  1931. AddressFamily.InterNetwork,
  1932. sock.RemoteEndPoint.AddressFamily);
  1933. IPEndPoint remep = (IPEndPoint)sock.RemoteEndPoint;
  1934. Assertion.AssertEquals ("ConnectMultiple #2",
  1935. IPAddress.Loopback,
  1936. remep.Address);
  1937. sock.Close ();
  1938. listen.Close ();
  1939. }
  1940. [Test]
  1941. public void ConnectMultipleNull ()
  1942. {
  1943. Socket sock = new Socket (AddressFamily.InterNetwork,
  1944. SocketType.Stream,
  1945. ProtocolType.Tcp);
  1946. IPAddress[] ips = null;
  1947. try {
  1948. sock.Connect (ips, 1251);
  1949. Assert.Fail ("ConnectMultipleNull #1");
  1950. } catch (ArgumentNullException) {
  1951. } catch {
  1952. Assert.Fail ("ConnectMultipleNull #2");
  1953. } finally {
  1954. sock.Close ();
  1955. }
  1956. }
  1957. [Test]
  1958. public void ConnectMultipleListen ()
  1959. {
  1960. Socket sock = new Socket (AddressFamily.InterNetwork,
  1961. SocketType.Stream,
  1962. ProtocolType.Tcp);
  1963. IPAddress[] ips = new IPAddress[4];
  1964. IPEndPoint ep = new IPEndPoint (IPAddress.Loopback,
  1965. 1252);
  1966. ips[0] = IPAddress.Parse ("127.0.0.4");
  1967. ips[1] = IPAddress.Parse ("127.0.0.3");
  1968. ips[2] = IPAddress.Parse ("127.0.0.2");
  1969. ips[3] = IPAddress.Parse ("127.0.0.1");
  1970. sock.Bind (ep);
  1971. sock.Listen (1);
  1972. try {
  1973. sock.Connect (ips, 1252);
  1974. Assert.Fail ("ConnectMultipleListen #1");
  1975. } catch (InvalidOperationException) {
  1976. } catch {
  1977. Assert.Fail ("ConnectMultipleListen #2");
  1978. } finally {
  1979. sock.Close ();
  1980. }
  1981. }
  1982. [Test]
  1983. [ExpectedException (typeof(ObjectDisposedException))]
  1984. public void ConnectMultipleClosed ()
  1985. {
  1986. Socket sock = new Socket (AddressFamily.InterNetwork,
  1987. SocketType.Stream,
  1988. ProtocolType.Tcp);
  1989. IPAddress[] ips = new IPAddress[4];
  1990. ips[0] = IPAddress.Parse ("127.0.0.4");
  1991. ips[1] = IPAddress.Parse ("127.0.0.3");
  1992. ips[2] = IPAddress.Parse ("127.0.0.2");
  1993. ips[3] = IPAddress.Parse ("127.0.0.1");
  1994. sock.Close ();
  1995. sock.Connect (ips, 1252);
  1996. }
  1997. [Test]
  1998. public void ConnectHostPortNull ()
  1999. {
  2000. Socket sock = new Socket (AddressFamily.InterNetwork,
  2001. SocketType.Stream,
  2002. ProtocolType.Tcp);
  2003. try {
  2004. sock.Connect ((string)null, 0);
  2005. Assert.Fail ("ConnectHostPort #1");
  2006. } catch (ArgumentNullException) {
  2007. } catch {
  2008. Assert.Fail ("ConnectHostPort #2");
  2009. } finally {
  2010. sock.Close ();
  2011. }
  2012. }
  2013. [Test]
  2014. public void ConnectHostPortListen ()
  2015. {
  2016. Socket sock = new Socket (AddressFamily.InterNetwork,
  2017. SocketType.Stream,
  2018. ProtocolType.Tcp);
  2019. IPAddress ip = IPAddress.Loopback;
  2020. IPEndPoint ep = new IPEndPoint (ip, 1253);
  2021. sock.Bind (ep);
  2022. sock.Listen (1);
  2023. try {
  2024. sock.Connect ("localhost", 1253);
  2025. Assert.Fail ("ConnectHostPortListen #1");
  2026. } catch (InvalidOperationException) {
  2027. } catch {
  2028. Assert.Fail ("ConnectHostPortListen #2");
  2029. } finally {
  2030. sock.Close ();
  2031. }
  2032. }
  2033. [Test]
  2034. [Category ("NotWorking")] // Need to pick a non-IP AddressFamily that "works" on both mono and ms, this one only works on ms
  2035. public void ConnectHostPortNotIP ()
  2036. {
  2037. Socket sock = new Socket (AddressFamily.NetBios,
  2038. SocketType.Seqpacket,
  2039. ProtocolType.Unspecified);
  2040. try {
  2041. sock.Connect ("localhost", 0);
  2042. Assert.Fail ("ConnectHostPortNotIP #1");
  2043. } catch (NotSupportedException) {
  2044. } catch {
  2045. Assert.Fail ("ConnectHostPortNotIP #2");
  2046. } finally {
  2047. sock.Close ();
  2048. }
  2049. }
  2050. [Test]
  2051. [ExpectedException (typeof(ObjectDisposedException))]
  2052. public void ConnectHostPortClosed ()
  2053. {
  2054. Socket sock = new Socket (AddressFamily.InterNetwork,
  2055. SocketType.Stream,
  2056. ProtocolType.Tcp);
  2057. sock.Close ();
  2058. sock.Connect ("localhost", 0);
  2059. }
  2060. [Test]
  2061. [Category ("NotDotNet")] // "Needs XP or later"
  2062. public void Disconnect ()
  2063. {
  2064. Socket sock = new Socket (AddressFamily.InterNetwork,
  2065. SocketType.Stream,
  2066. ProtocolType.Tcp);
  2067. Socket listen = new Socket (AddressFamily.InterNetwork,
  2068. SocketType.Stream,
  2069. ProtocolType.Tcp);
  2070. IPAddress ip = IPAddress.Loopback;
  2071. IPEndPoint ep = new IPEndPoint (ip, 1255);
  2072. listen.Bind (ep);
  2073. listen.Listen (1);
  2074. sock.Connect (ip, 1255);
  2075. Assertion.AssertEquals ("Disconnect #1", true,
  2076. sock.Connected);
  2077. sock.Shutdown (SocketShutdown.Both);
  2078. sock.Disconnect (false);
  2079. Assertion.AssertEquals ("BeginDisconnect #3", false,
  2080. sock.Connected);
  2081. sock.Close ();
  2082. listen.Close ();
  2083. }
  2084. [Test]
  2085. public void DuplicateAndClose ()
  2086. {
  2087. }
  2088. [Test]
  2089. public void IOControl ()
  2090. {
  2091. }
  2092. [Test]
  2093. public void ReceiveGeneric ()
  2094. {
  2095. int i;
  2096. IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 1258);
  2097. Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  2098. listensock.Bind (endpoint);
  2099. listensock.Listen(1);
  2100. Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  2101. sendsock.Connect(endpoint);
  2102. Socket clientsock = listensock.Accept();
  2103. byte[] sendbuf = new byte[256];
  2104. for(i = 0; i < 256; i++) {
  2105. sendbuf[i] = (byte)i;
  2106. }
  2107. for (i = 4; i < 6; i++) {
  2108. Assert.AreEqual (sendbuf[i], (byte)i,
  2109. "#1/" + i.ToString());
  2110. }
  2111. SocketError err;
  2112. sendsock.Send (sendbuf, 0, 256, SocketFlags.None,
  2113. out err);
  2114. byte[] recvbuf = new byte[256];
  2115. List<ArraySegment<byte>> recvbuflist = new List<ArraySegment<byte>>(2);
  2116. recvbuflist.Add(new ArraySegment<byte>(recvbuf, 4, 2));
  2117. recvbuflist.Add(new ArraySegment<byte>(recvbuf, 20, 230));
  2118. clientsock.Receive (recvbuflist);
  2119. /* recvbuf should now hold the first 2 bytes
  2120. * of sendbuf from pos 4, and the next 230
  2121. * bytes of sendbuf from pos 20
  2122. */
  2123. for (i = 0; i < 2; i++) {
  2124. Assert.AreEqual (sendbuf[i], recvbuf[i + 4],
  2125. "#2/" + i.ToString());
  2126. }
  2127. for (i = 2; i < 232; i++) {
  2128. Assert.AreEqual (sendbuf[i], recvbuf[i + 18],
  2129. "#2/" + i.ToString());
  2130. }
  2131. sendsock.Close ();
  2132. clientsock.Close ();
  2133. listensock.Close ();
  2134. }
  2135. [Test]
  2136. public void SendGeneric ()
  2137. {
  2138. int i;
  2139. IPEndPoint endpoint = new IPEndPoint(IPAddress.Loopback, 1259);
  2140. Socket listensock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  2141. listensock.Bind (endpoint);
  2142. listensock.Listen(1);
  2143. Socket sendsock = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  2144. sendsock.Connect(endpoint);
  2145. Socket clientsock = listensock.Accept();
  2146. byte[] sendbuf = new byte[256];
  2147. List<ArraySegment<byte>> sendbuflist = new List<ArraySegment<byte>>(2);
  2148. sendbuflist.Add(new ArraySegment<byte>(sendbuf, 4, 2));
  2149. sendbuflist.Add(new ArraySegment<byte>(sendbuf, 20, 230));
  2150. for(i = 0; i < 256; i++) {
  2151. sendbuf[i] = (byte)i;
  2152. }
  2153. for (i = 4; i < 6; i++) {
  2154. Assert.AreEqual (sendbuf[i], (byte)i,
  2155. "#1/" + i.ToString());
  2156. }
  2157. SocketError err;
  2158. sendsock.Send (sendbuflist, SocketFlags.None, out err);
  2159. byte[] recvbuf = new byte[256];
  2160. clientsock.Receive (recvbuf);
  2161. /* The first 2 bytes of recvbuf should now
  2162. * hold 2 bytes of sendbuf from pos 4, and the
  2163. * next 230 bytes of recvbuf should be sendbuf
  2164. * from pos 20
  2165. */
  2166. for (i = 0; i < 2; i++) {
  2167. Assert.AreEqual (recvbuf[i], sendbuf[i + 4],
  2168. "#2/" + i.ToString());
  2169. }
  2170. for (i = 2; i < 232; i++) {
  2171. Assert.AreEqual (recvbuf[i], sendbuf[i + 18],
  2172. "#2/" + i.ToString());
  2173. }
  2174. sendsock.Close ();
  2175. clientsock.Close ();
  2176. listensock.Close ();
  2177. }
  2178. [Test]
  2179. public void ListenNotBound ()
  2180. {
  2181. Socket sock = new Socket (AddressFamily.InterNetwork,
  2182. SocketType.Stream,
  2183. ProtocolType.Tcp);
  2184. try {
  2185. sock.Listen (1);
  2186. Assert.Fail ("ListenNotBound #1");
  2187. } catch (SocketException ex) {
  2188. Assertion.AssertEquals ("ListenNotBound #2",
  2189. 10022, ex.ErrorCode);
  2190. } catch {
  2191. Assert.Fail ("ListenNotBound #3");
  2192. } finally {
  2193. sock.Close ();
  2194. }
  2195. }
  2196. #endif
  2197. static Socket CWRSocket;
  2198. static bool CWRReceiving = true;
  2199. static ManualResetEvent CWRReady = new ManualResetEvent (false);
  2200. private static void CWRReceiveThread ()
  2201. {
  2202. byte[] buf = new byte[256];
  2203. try {
  2204. CWRSocket.Receive (buf);
  2205. } catch (SocketException) {
  2206. CWRReceiving = false;
  2207. }
  2208. CWRReady.Set ();
  2209. }
  2210. [Test]
  2211. public void CloseWhileReceiving ()
  2212. {
  2213. CWRSocket = new Socket (AddressFamily.InterNetwork,
  2214. SocketType.Dgram,
  2215. ProtocolType.Udp);
  2216. CWRSocket.Bind (new IPEndPoint (IPAddress.Loopback,
  2217. 1256));
  2218. Thread recv_thread = new Thread (new ThreadStart (CWRReceiveThread));
  2219. CWRReady.Reset ();
  2220. recv_thread.Start ();
  2221. Thread.Sleep (250); /* Wait for the thread to be already receiving */
  2222. CWRSocket.Close ();
  2223. if (CWRReady.WaitOne (1000, false) == false) {
  2224. Assert.Fail ("CloseWhileReceiving wait timed out");
  2225. }
  2226. Assert.IsFalse (CWRReceiving);
  2227. }
  2228. static bool RRCLastRead = false;
  2229. static ManualResetEvent RRCReady = new ManualResetEvent (false);
  2230. private static void RRCClientThread ()
  2231. {
  2232. byte[] bytes = new byte[8];
  2233. int readbyte;
  2234. Socket sock = new Socket (AddressFamily.InterNetwork,
  2235. SocketType.Stream,
  2236. ProtocolType.Tcp);
  2237. sock.Connect (new IPEndPoint (IPAddress.Loopback,
  2238. 1257));
  2239. NetworkStream stream = new NetworkStream (sock);
  2240. readbyte = stream.ReadByte ();
  2241. Assertion.AssertEquals ("ReceiveRemoteClosed #1",
  2242. 0, readbyte);
  2243. stream.Read (bytes, 0, 0);
  2244. readbyte = stream.ReadByte ();
  2245. Assertion.AssertEquals ("ReceiveRemoteClosed #2",
  2246. 0, readbyte);
  2247. stream.Read (bytes, 0, 0);
  2248. readbyte = stream.ReadByte ();
  2249. Assertion.AssertEquals ("ReceiveRemoteClosed #3",
  2250. -1, readbyte);
  2251. sock.Close ();
  2252. RRCLastRead = true;
  2253. RRCReady.Set ();
  2254. }
  2255. [Test]
  2256. public void ReceiveRemoteClosed ()
  2257. {
  2258. Socket sock = new Socket (AddressFamily.InterNetwork,
  2259. SocketType.Stream,
  2260. ProtocolType.Tcp);
  2261. sock.Bind (new IPEndPoint (IPAddress.Loopback, 1257));
  2262. sock.Listen (1);
  2263. RRCReady.Reset ();
  2264. Thread client_thread = new Thread (new ThreadStart (RRCClientThread));
  2265. client_thread.Start ();
  2266. Socket client = sock.Accept ();
  2267. NetworkStream stream = new NetworkStream (client);
  2268. stream.WriteByte (0x00);
  2269. stream.WriteByte (0x00);
  2270. client.Close ();
  2271. sock.Close ();
  2272. RRCReady.WaitOne (1000, false);
  2273. Assert.IsTrue (RRCLastRead);
  2274. }
  2275. #if NET_2_0
  2276. [Test]
  2277. public void ConnectedProperty ()
  2278. {
  2279. TcpListener listener = new TcpListener (IPAddress.Loopback, 23456);
  2280. listener.Start();
  2281. Socket client = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  2282. client.Connect (IPAddress.Loopback, 23456);
  2283. Socket server = listener.AcceptSocket ();
  2284. try {
  2285. server.EndSend(server.BeginSend (new byte[10], 0, 10, SocketFlags.None, null, null));
  2286. client.Close ();
  2287. try {
  2288. server.EndReceive (server.BeginReceive (new byte[10], 0, 10, SocketFlags.None, null, null));
  2289. } catch {
  2290. }
  2291. Assert.IsTrue (!client.Connected);
  2292. Assert.IsTrue (!server.Connected);
  2293. } finally {
  2294. listener.Stop ();
  2295. client.Close ();
  2296. server.Close ();
  2297. }
  2298. }
  2299. #endif
  2300. }
  2301. }