* AllTests.cs: Fixed Lawrence's e-mail address.
[mono.git] / mcs / class / System / Test / System.Net.Sockets / TcpListenerTest.cs
1 // System.Net.Sockets.TcpListenerTest.cs
2 //
3 // Author:
4 //    Phillip Pearson (pp@myelin.co.nz)
5 //
6 // Copyright (C) 2001, Phillip Pearson
7 //    http://www.myelin.co.nz
8 //
9
10 using System;
11 using System.Net;
12 using System.Net.Sockets;
13 using NUnit.Framework;
14
15 namespace MonoTests.System.Net.Sockets {
16
17         /// <summary>
18         /// Tests System.Net.Sockets.TcpListener
19         /// </summary>
20         public class TcpListenerTest : TestCase {
21                 
22                 public TcpListenerTest(string name) : base(name) {}
23
24                 public static ITest Suite {
25                         get {
26                                 return new TestSuite(typeof (TcpListenerTest));
27                         }
28                 }
29
30                 /// <summary>
31                 /// Tests the TcpListener object
32                 /// (from System.Net.Sockets)
33                 /// </summary>
34                 public void test_TcpListener()
35                 {
36                         // listen with a new listener
37                         TcpListener inListener = new TcpListener(1234);
38                         inListener.Start();
39                         
40
41                         // connect to it from a new socket
42                         Socket outSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
43                                 ProtocolType.IP);
44                         IPHostEntry hostent = Dns.GetHostByAddress("127.0.0.1");
45                         IPEndPoint remote = new IPEndPoint(hostent.AddressList[0], 1234);
46                         outSock.Connect(remote);
47
48                         
49                         // make sure the connection arrives
50                         Assert(inListener.Pending());
51                         Socket inSock = inListener.AcceptSocket();
52
53
54                         // now send some data and see if it comes out the other end
55                         const int len = 1024;
56                         byte[] outBuf = new Byte[len];
57                         for (int i=0; i<len; i++) 
58                         {
59                                 outBuf[i] = (byte)(i % 256);
60                         }
61
62                         outSock.Send(outBuf, 0, len, 0);
63
64                         byte[] inBuf = new Byte[len];
65                         int ret = inSock.Receive(inBuf, 0, len, 0);
66
67
68                         // let's see if it arrived OK
69                         Assert(ret != 0);
70                         for (int i=0; i<len; i++) 
71                         {
72                                 Assert(inBuf[i] == outBuf[i]);
73                         }
74
75
76                         // tidy up after ourselves
77                         inSock.Close();
78
79                         inListener.Stop();      
80                 }
81         
82                 
83         }
84
85 }