1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
// System.Net.Sockets.TcpClientTest.cs
//
// Authors:
// Phillip Pearson (pp@myelin.co.nz)
// Martin Willemoes Hansen (mwh@sysrq.dk)
//
// (C) Copyright 2001 Phillip Pearson (http://www.myelin.co.nz)
// (C) Copyright 2003 Martin Willemoes Hansen
//
using System;
using System.Net;
using System.Net.Sockets;
using NUnit.Framework;
namespace MonoTests.System.Net.Sockets {
/// <summary>
/// Tests System.Net.Sockets.TcpClient
/// </summary>
[TestFixture]
public class TcpClientTest {
/// <summary>
/// Tests the TcpClient object
/// (from System.Net.Sockets)
/// </summary>
[Test]
public void TcpClient()
{
// set up a listening Socket
Socket lSock = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
lSock.Bind(new IPEndPoint(IPAddress.Any, 8765));
lSock.Listen(-1);
// connect to it with a TcpClient
TcpClient outClient = new TcpClient("localhost", 8765);
Socket inSock = lSock.Accept();
// now try exchanging data
NetworkStream stream = outClient.GetStream();
const int len = 1024;
byte[] outBuf = new Byte[len];
for (int i=0; i<len; i++)
{
outBuf[i] = (byte)(i % 256);
}
// send it
stream.Write(outBuf,0,len);
// and see if it comes back
byte[] inBuf = new Byte[len];
int ret = inSock.Receive(inBuf, 0, len, 0);
Assertion.Assert(ret != 0);
for (int i=0; i<len; i++)
{
Assertion.Assert(inBuf[i] == outBuf[i]);
}
// tidy up
inSock.Close();
outClient.Close();
lSock.Close();
}
}
}
|