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. 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. Assert.AreEqual (0, tcpClient.Available, "#A2");
  70. Assert.IsTrue (tcpClient.Connected, "#A3");
  71. // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#A4");
  72. tcpClient.Close ();
  73. Assert.IsNotNull (tcpClient.Client, "#A5");
  74. try {
  75. int available = tcpClient.Available;
  76. Assert.Fail ("#A6: " + available);
  77. } catch (ObjectDisposedException) {
  78. }
  79. Assert.IsFalse (tcpClient.Connected, "#A7");
  80. // not supported on linux
  81. /*
  82. try {
  83. bool exclusive = tcpClient.ExclusiveAddressUse;
  84. Assert.Fail ("#A8: " + exclusive);
  85. } catch (ObjectDisposedException) {
  86. }
  87. */
  88. }
  89. using (SocketResponder sr = new SocketResponder (localEP, new SocketRequestHandler (CloseRequestHandler))) {
  90. sr.Start ();
  91. TcpClient tcpClient = new TcpClient (IPAddress.Loopback.ToString (), 8765);
  92. Assert.AreEqual (0, tcpClient.Available, "#B1");
  93. Assert.IsTrue (tcpClient.Connected, "#B2");
  94. // Assert.IsFalse (tcpClient.ExclusiveAddressUse, "#B3");
  95. tcpClient.Close ();
  96. Assert.IsNull (tcpClient.Client, "#B4");
  97. try {
  98. int available = tcpClient.Available;
  99. Assert.Fail ("#B5: " + available);
  100. } catch (NullReferenceException) {
  101. }
  102. try {
  103. bool connected = tcpClient.Connected;
  104. Assert.Fail ("#B6: " + connected);
  105. } catch (NullReferenceException) {
  106. }
  107. // not supported on linux
  108. /*
  109. try {
  110. bool exclusive = tcpClient.ExclusiveAddressUse;
  111. Assert.Fail ("#B7: " + exclusive);
  112. } catch (NullReferenceException) {
  113. }
  114. */
  115. }
  116. }
  117. byte [] CloseRequestHandler (Socket socket)
  118. {
  119. return new byte [0];
  120. }
  121. [Test]
  122. [ExpectedException (typeof(ArgumentNullException))]
  123. public void ConnectMultiNull ()
  124. {
  125. TcpClient client = new TcpClient ();
  126. IPAddress[] ipAddresses = null;
  127. client.Connect (ipAddresses, 1234);
  128. }
  129. [Test]
  130. public void ConnectMultiAny ()
  131. {
  132. TcpClient client = new TcpClient ();
  133. IPAddress[] ipAddresses = new IPAddress[1];
  134. ipAddresses[0] = IPAddress.Any;
  135. try {
  136. client.Connect (ipAddresses, 1234);
  137. Assert.Fail ("ConnectMultiAny #1");
  138. } catch (SocketException ex) {
  139. Assert.AreEqual (10049, ex.ErrorCode, "ConnectMultiAny #2");
  140. } catch {
  141. Assert.Fail ("ConnectMultiAny #3");
  142. }
  143. }
  144. [Test]
  145. public void ConnectMultiRefused ()
  146. {
  147. TcpClient client = new TcpClient ();
  148. IPAddress[] ipAddresses = new IPAddress[1];
  149. ipAddresses[0] = IPAddress.Loopback;
  150. try {
  151. client.Connect (ipAddresses, 1234);
  152. Assert.Fail ("ConnectMultiRefused #1");
  153. } catch (SocketException ex) {
  154. Assert.AreEqual (10061, ex.ErrorCode, "ConnectMultiRefused #2");
  155. } catch {
  156. Assert.Fail ("ConnectMultiRefused #3");
  157. }
  158. }
  159. }
  160. }