TcpClientTest.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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, 1234));
  31. lSock.Listen(-1);
  32. // connect to it with a TcpClient
  33. TcpClient outClient = new TcpClient("localhost", 1234);
  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. }
  59. }