1 // System.Net.Sockets.TcpListenerTest.cs
4 // Phillip Pearson (pp@myelin.co.nz)
5 // Martin Willemoes Hansen (mwh@sysrq.dk)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (C) Copyright 2001 Phillip Pearson (http://www.myelin.co.nz)
9 // (C) Copyright 2003 Martin Willemoes Hansen (mwh@sysrq.dk)
10 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
15 using System.Net.Sockets;
16 using NUnit.Framework;
18 namespace MonoTests.System.Net.Sockets
21 public class TcpListenerTest : Assertion
24 public void TcpListener ()
26 // listen with a new listener (IPv4 is the default)
27 TcpListener inListener = new TcpListener (8766);
31 // connect to it from a new socket
32 IPHostEntry hostent = Dns.GetHostByAddress (IPAddress.Loopback);
33 Socket outSock = null;
35 foreach (IPAddress address in hostent.AddressList) {
36 if (address.AddressFamily == AddressFamily.InterNetwork) {
37 /// Only keep IPv4 addresses, our Server is in IPv4 only mode.
38 outSock = new Socket (address.AddressFamily, SocketType.Stream,
40 IPEndPoint remote = new IPEndPoint (address, 8766);
41 outSock.Connect (remote);
46 // make sure the connection arrives
47 Assert (inListener.Pending ());
48 Socket inSock = inListener.AcceptSocket ();
50 // now send some data and see if it comes out the other end
52 byte[] outBuf = new Byte [len];
53 for (int i=0; i<len; i++)
54 outBuf [i] = (byte) (i % 256);
56 outSock.Send (outBuf, 0, len, 0);
58 byte[] inBuf = new Byte[len];
59 int ret = inSock.Receive (inBuf, 0, len, 0);
62 // let's see if it arrived OK
64 for (int i=0; i<len; i++)
65 Assert (inBuf[i] == outBuf [i]);
67 // tidy up after ourselves
74 public void CtorInt1 ()
77 try { new TcpListener (-1); } catch { nex++; }
79 new TcpListener (65535);
80 try { new TcpListener (65536); } catch { nex++; }
81 try { new TcpListener (100000); } catch { nex++; }
86 [ExpectedException (typeof (ArgumentNullException))]
87 public void CtorIPEndPoint ()
89 new TcpListener (null);
93 [ExpectedException (typeof (ArgumentNullException))]
94 public void CtorIPAddressInt1 ()
96 new TcpListener (null, 100000);
100 [ExpectedException (typeof (ArgumentOutOfRangeException))]
101 public void CtorIPAddressInt2 ()
103 new TcpListener (IPAddress.Any, 100000);
106 class MyListener : TcpListener
109 : base (IPAddress.Loopback, 5000)
113 public Socket GetSocket ()
118 public bool IsActive {
119 get { return Active; }
124 public void PreStartStatus ()
126 MyListener listener = new MyListener ();
127 AssertEquals ("#01", false, listener.IsActive);
128 Assert ("#02", null != listener.GetSocket ());
130 listener.AcceptSocket ();
131 Fail ("Exception not thrown");
132 } catch (InvalidOperationException) {
136 listener.AcceptTcpClient ();
137 Fail ("Exception not thrown");
138 } catch (InvalidOperationException) {
143 Fail ("Exception not thrown");
144 } catch (InvalidOperationException) {
151 public void PostStartStatus ()
153 MyListener listener = new MyListener ();
155 AssertEquals ("#01", true, listener.IsActive);
156 Assert ("#02", null != listener.GetSocket ());
158 Socket sock = listener.GetSocket ();
159 listener.Start (); // Start called twice
160 AssertEquals ("#03", true, listener.IsActive);
161 Assert ("#04", null != listener.GetSocket ());
163 AssertEquals ("#05", false, listener.Pending ());
166 AssertEquals ("#06", false, listener.IsActive);
167 Assert ("#07", null != listener.GetSocket ());
168 Assert ("#08", sock != listener.GetSocket ());
173 public void StartListenMoreThan5 ()
175 TcpListener listen = new TcpListener (IPAddress.Loopback, 1234);
186 listen.Start (32768);
189 listen.Start (65536);