TcpClientTest.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // System.Net.Sockets.TcpClientTest.cs
  2. //
  3. // Authors:
  4. // Phillip Pearson ([email protected])
  5. // Martin Willemoes Hansen ([email protected])
  6. //
  7. // (C) Copyright 2001 Phillip Pearson (http://www.myelin.co.nz)
  8. // (C) Copyright 2003 Martin Willemoes Hansen
  9. //
  10. using System;
  11. using System.Net;
  12. using System.Net.Sockets;
  13. using NUnit.Framework;
  14. using MonoTests.Helpers;
  15. namespace MonoTests.System.Net.Sockets
  16. {
  17. /// <summary>
  18. /// Tests System.Net.Sockets.TcpClient
  19. /// </summary>
  20. [TestFixture]
  21. public class TcpClientTest
  22. {
  23. /// <summary>
  24. /// Tests the TcpClient object
  25. /// (from System.Net.Sockets)
  26. /// </summary>
  27. [Test]
  28. public void TcpClient()
  29. {
  30. // set up a listening Socket
  31. Socket lSock = new Socket(AddressFamily.InterNetwork,
  32. SocketType.Stream, ProtocolType.Tcp);
  33. var port = NetworkHelpers.FindFreePort ();
  34. lSock.Bind(new IPEndPoint(IPAddress.Any, port));
  35. lSock.Listen(-1);
  36. // connect to it with a TcpClient
  37. TcpClient outClient = new TcpClient("localhost", port);
  38. Socket inSock = lSock.Accept();
  39. // now try exchanging data
  40. NetworkStream stream = outClient.GetStream();
  41. const int len = 1024;
  42. byte[] outBuf = new Byte[len];
  43. for (int i=0; i<len; i++)
  44. {
  45. outBuf[i] = (byte)(i % 256);
  46. }
  47. // send it
  48. stream.Write(outBuf,0,len);
  49. // and see if it comes back
  50. byte[] inBuf = new Byte[len];
  51. int ret = inSock.Receive(inBuf, 0, len, 0);
  52. Assert.IsTrue (ret != 0);
  53. for (int i=0; i<len; i++)
  54. {
  55. Assert.IsTrue (inBuf[i] == outBuf[i]);
  56. }
  57. // tidy up
  58. inSock.Close();
  59. outClient.Close();
  60. lSock.Close();
  61. }
  62. [Test] // bug #81105
  63. public void CloseTest ()
  64. {
  65. var port = NetworkHelpers.FindFreePort ();
  66. IPEndPoint localEP = new IPEndPoint (IPAddress.Loopback, port);
  67. using (SocketResponder sr = new SocketResponder (localEP, s => CloseRequestHandler (s))) {
  68. TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), port);
  69. NetworkStream ns = tcpClient.GetStream ();
  70. Assert.IsNotNull (ns, "#A1");
  71. Assert.AreEqual (0, tcpClient.Available, "#A2");
  72. Assert.IsTrue (tcpClient.Connected, "#A3");
  73. // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#A4");
  74. tcpClient.Close ();
  75. Assert.IsNotNull (tcpClient.Client, "#A5");
  76. try {
  77. int available = tcpClient.Available;
  78. Assert.Fail ("#A6: " + available);
  79. } catch (ObjectDisposedException) {
  80. }
  81. Assert.IsFalse (tcpClient.Connected, "#A7");
  82. // not supported on linux
  83. /*
  84. try {
  85. bool exclusive = tcpClient.ExclusiveAddressUse;
  86. Assert.Fail ("#A8: " + exclusive);
  87. } catch (ObjectDisposedException) {
  88. }
  89. */
  90. }
  91. using (SocketResponder sr = new SocketResponder (localEP, s => CloseRequestHandler (s))) {
  92. TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), port);
  93. Assert.AreEqual (0, tcpClient.Available, "#B1");
  94. Assert.IsTrue (tcpClient.Connected, "#B2");
  95. // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#B3");
  96. tcpClient.Close ();
  97. Assert.IsNull (tcpClient.Client, "#B4");
  98. try {
  99. int available = tcpClient.Available;
  100. Assert.Fail ("#B5: " + available);
  101. } catch (NullReferenceException) {
  102. }
  103. try {
  104. bool connected = tcpClient.Connected;
  105. Assert.Fail ("#B6: " + connected);
  106. } catch (NullReferenceException) {
  107. }
  108. // not supported on linux
  109. /*
  110. try {
  111. bool exclusive = tcpClient.ExclusiveAddressUse;
  112. Assert.Fail ("#B7: " + exclusive);
  113. } catch (NullReferenceException) {
  114. }
  115. */
  116. }
  117. }
  118. byte [] CloseRequestHandler (Socket socket)
  119. {
  120. return new byte [0];
  121. }
  122. [Test]
  123. [ExpectedException (typeof(ArgumentNullException))]
  124. public void ConnectMultiNull ()
  125. {
  126. TcpClient client = new TcpClient ();
  127. IPAddress[] ipAddresses = null;
  128. client.Connect (ipAddresses, 1234);
  129. }
  130. [Test]
  131. public void ConnectMultiAny ()
  132. {
  133. TcpClient client = new TcpClient ();
  134. IPAddress[] ipAddresses = new IPAddress[1];
  135. ipAddresses[0] = IPAddress.Any;
  136. try {
  137. client.Connect (ipAddresses, 1234);
  138. Assert.Fail ("ConnectMultiAny #1");
  139. } catch (SocketException ex) {
  140. Assert.AreEqual (10049, ex.ErrorCode, "ConnectMultiAny #2");
  141. } catch {
  142. Assert.Fail ("ConnectMultiAny #3");
  143. }
  144. }
  145. [Test]
  146. public void ConnectMultiRefused ()
  147. {
  148. TcpClient client = new TcpClient ();
  149. IPAddress[] ipAddresses = new IPAddress[1];
  150. ipAddresses[0] = IPAddress.Loopback;
  151. try {
  152. client.Connect (ipAddresses, 1234);
  153. Assert.Fail ("ConnectMultiRefused #1");
  154. } catch (SocketException ex) {
  155. Assert.AreEqual (10061, ex.ErrorCode, "ConnectMultiRefused #2");
  156. } catch {
  157. Assert.Fail ("ConnectMultiRefused #3");
  158. }
  159. }
  160. }
  161. }