TcpClientTest.cs 4.9 KB

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