TcpClientTest.cs 4.7 KB

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