TcpClientTest.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // System.Net.Sockets.TcpClientTest.cs
  2. //
  3. // Author:
  4. // Phillip Pearson ([email protected])
  5. //
  6. // Copyright (C) 2001, Phillip Pearson
  7. // http://www.myelin.co.nz
  8. //
  9. using System;
  10. using System.Net;
  11. using System.Net.Sockets;
  12. using NUnit.Framework;
  13. namespace MonoTests.System.Net.Sockets {
  14. /// <summary>
  15. /// Tests System.Net.Sockets.TcpClient
  16. /// </summary>
  17. public class TcpClientTest : TestCase {
  18. public TcpClientTest(string name) : base(name) {}
  19. public static ITest Suite {
  20. get {
  21. return new TestSuite(typeof (TcpClientTest));
  22. }
  23. }
  24. /// <summary>
  25. /// Tests the TcpClient object
  26. /// (from System.Net.Sockets)
  27. /// </summary>
  28. public void test_TcpClient()
  29. {
  30. // set up a listening Socket
  31. Socket lSock = new Socket(AddressFamily.InterNetwork,
  32. SocketType.Stream, ProtocolType.Tcp);
  33. lSock.Bind(new IPEndPoint(IPAddress.Any, 1234));
  34. lSock.Listen(-1);
  35. // connect to it with a TcpClient
  36. TcpClient outClient = new TcpClient("localhost", 1234);
  37. Socket inSock = lSock.Accept();
  38. // now try exchanging data
  39. NetworkStream stream = outClient.GetStream();
  40. const int len = 1024;
  41. byte[] outBuf = new Byte[len];
  42. for (int i=0; i<len; i++)
  43. {
  44. outBuf[i] = (byte)(i % 256);
  45. }
  46. // send it
  47. stream.Write(outBuf,0,len);
  48. // and see if it comes back
  49. byte[] inBuf = new Byte[len];
  50. int ret = inSock.Receive(inBuf, 0, len, 0);
  51. Assert(ret != 0);
  52. for (int i=0; i<len; i++)
  53. {
  54. Assert(inBuf[i] == outBuf[i]);
  55. }
  56. // tidy up
  57. inSock.Close();
  58. outClient.Close();
  59. lSock.Close();
  60. }
  61. }
  62. }